Example usage for com.google.common.util.concurrent RateLimiter tryAcquire

List of usage examples for com.google.common.util.concurrent RateLimiter tryAcquire

Introduction

In this page you can find the example usage for com.google.common.util.concurrent RateLimiter tryAcquire.

Prototype

public boolean tryAcquire() 

Source Link

Document

Acquires a permit from this RateLimiter if it can be acquired immediately without delay.

Usage

From source file:com.magnet.mmx.server.plugin.mmxmgmt.monitoring.RateLimiterService.java

public static boolean isAllowed(RateLimiterDescriptor descriptor) {
    if (descriptor.getPermitsPerSecond() <= 0) {
        LOGGER.trace("isAllowed : rate limiting disabled");
        return true;
    }/* www.  j a v a  2s  . c  om*/
    try {
        LOGGER.trace("isAllowed : getting ratelimiter for descriptor={}", descriptor);
        RateLimiter r = getOrCreate(descriptor);
        LOGGER.trace("isAllowed : retireved ratelimiter={}, rate={}", r, r.getRate());
        return r.tryAcquire();
    } catch (ExecutionException e) {
        LOGGER.error("isAllowed : Caught exception getting RateLimiter for descriptor : {}", descriptor, e);
        return false;
    }
}

From source file:co.mitro.core.util.GuavaRequestRateLimiter.java

private boolean isRequestPermittedWithCount(String ip, String endpoint, int count) {
    CacheKey key = new CacheKey(ip, endpoint);
    RateLimiter limit = rateLimits.getUnchecked(key);
    if (limit.tryAcquire()) {
        return true;
    }// w w w.j a v a 2 s  . c  o  m

    // didn't get it right away, log, wait for 1 second, then fail request
    logger.warn("rate limited; waiting for up to 1 second (ip {}, endpoint {})", ip, endpoint);
    return limit.tryAcquire(count, TimeUnit.SECONDS);
}

From source file:com.linkedin.pinot.broker.queryquota.TableQueryQuotaManager.java

/**
 * Try to acquire token from rate limiter. Emit the utilization of the qps quota if broker metric isn't null.
 * @param tableName origin table name, which could be raw.
 * @param queryQuotaConfig query quota config for type-specific table.
 * @return true if there's no qps quota for that table, or a token is acquired successfully.
 *///from ww w  .  j a va  2 s .  c  o m
private boolean tryAcquireToken(String tableName, QueryQuotaConfig queryQuotaConfig) {
    // Use hit counter to count the number of hits.
    queryQuotaConfig.getHitCounter().hit();

    RateLimiter rateLimiter = queryQuotaConfig.getRateLimiter();
    double perBrokerRate = rateLimiter.getRate();
    if (!rateLimiter.tryAcquire()) {
        LOGGER.info("Quota is exceeded for table: {}. Per-broker rate: {}", tableName, perBrokerRate);
        return false;
    }

    // Emit the qps capacity utilization rate.
    if (_brokerMetrics != null) {
        int numHits = queryQuotaConfig.getHitCounter().getHitCount();
        int percentageOfCapacityUtilization = (int) (numHits * 100 / perBrokerRate);
        LOGGER.debug("The percentage of rate limit capacity utilization is {}",
                percentageOfCapacityUtilization);
        _brokerMetrics.setValueOfTableGauge(tableName, BrokerGauge.QUERY_QUOTA_CAPACITY_UTILIZATION_RATE,
                percentageOfCapacityUtilization);
    }

    // Token is successfully acquired.
    return true;
}