List of usage examples for com.google.common.util.concurrent RateLimiter tryAcquire
public boolean tryAcquire(int permits, long timeout, TimeUnit unit)
From source file:org.apache.samza.util.EmbeddedTaggedRateLimiter.java
@Override public Map<String, Integer> acquire(Map<String, Integer> tagToCreditsMap, long timeout, TimeUnit unit) { ensureTagsAreValid(tagToCreditsMap); long timeoutInNanos = NANOSECONDS.convert(timeout, unit); Stopwatch stopwatch = Stopwatch.createStarted(); return tagToCreditsMap.entrySet().stream().map(e -> { String tag = e.getKey();//from w w w . j a v a 2 s .c o m int requiredCredits = e.getValue(); long remainingTimeoutInNanos = Math.max(0L, timeoutInNanos - stopwatch.elapsed(NANOSECONDS)); com.google.common.util.concurrent.RateLimiter rateLimiter = tagToRateLimiterMap.get(tag); int availableCredits = rateLimiter.tryAcquire(requiredCredits, remainingTimeoutInNanos, NANOSECONDS) ? requiredCredits : 0; return new ImmutablePair<>(tag, availableCredits); }).collect(Collectors.toMap(ImmutablePair::getKey, ImmutablePair::getValue)); }
From source file:io.warp10.continuum.ThrottlingManager.java
/** * Validate the ingestion of datapoints against the DDP limit * // www. ja v a2s . c om * We tolerate inputs only if they wouldn't incur a wait greater than 2 seconds * * @param producer * @param owner * @param application * @param count * @param maxwait Max wait per datapoint */ public static void checkDDP(Metadata metadata, String producer, String owner, String application, int count, long maxwait) throws WarpException { if (!loaded) { return; } // // Extract RateLimiter // RateLimiter producerLimiter = producerRateLimiters.get(producer); RateLimiter applicationLimiter = applicationRateLimiters.get(application); // // TODO(hbs): store per producer/per app maxwait values? Extract them from the throttling file? // long appMaxWait = maxwait; long producerMaxWait = maxwait; // -1.0 as the default rate means do not enforce DDP limit if (null == producerLimiter && null == applicationLimiter && -1.0D == DEFAULT_RATE_PRODUCER) { return; } else if (null == producerLimiter) { // Create a rate limiter with the default rate producerLimiter = RateLimiter.create(Math.max(MINIMUM_RATE_LIMIT, DEFAULT_RATE_PRODUCER)); producerRateLimiters.put(producer, producerLimiter); } // Check per application limiter if (null != applicationLimiter) { synchronized (applicationLimiter) { if (!applicationLimiter.tryAcquire(count, appMaxWait * count, TimeUnit.MILLISECONDS)) { StringBuilder sb = new StringBuilder(); sb.append("Storing data for "); if (null != metadata) { GTSHelper.metadataToString(sb, metadata.getName(), metadata.getLabels()); } sb.append(" would incur a wait greater than "); sb.append(appMaxWait); sb.append( " ms per datapoint due to your Daily Data Points limit being already exceeded for application '" + application + "'. Current max rate is " + applicationLimiter.getRate() + " datapoints/s."); Map<String, String> labels = new HashMap<String, String>(); labels.put(SensisionConstants.SENSISION_LABEL_APPLICATION, application); Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE_PER_APP, labels, 1); Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE_PER_APP_GLOBAL, Sensision.EMPTY_LABELS, 1); throw new WarpException(sb.toString()); } } } synchronized (producerLimiter) { if (!producerLimiter.tryAcquire(count, producerMaxWait * count, TimeUnit.MILLISECONDS)) { StringBuilder sb = new StringBuilder(); sb.append("Storing data for "); if (null != metadata) { GTSHelper.metadataToString(sb, metadata.getName(), metadata.getLabels()); } sb.append(" would incur a wait greater than "); sb.append(producerMaxWait); sb.append( " ms per datapoint due to your Daily Data Points limit being already exceeded. Current maximum rate is " + producerLimiter.getRate() + " datapoints/s."); Map<String, String> labels = new HashMap<String, String>(); labels.put(SensisionConstants.SENSISION_LABEL_PRODUCER, producer); Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE, labels, 1); Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE_GLOBAL, Sensision.EMPTY_LABELS, 1); throw new WarpException(sb.toString()); } } }