Search in sources :

Example 6 with RateLimiterBucket

use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.

the class AbstractRateLimiterComponent method accept.

/**
 * @see IRateLimiterComponent#accept(String, RateBucketPeriod, long, long, IAsyncResultHandler)
 */
@Override
public void accept(final String bucketId, final RateBucketPeriod period, final long limit, final long increment, final IAsyncResultHandler<RateLimitResponse> handler) {
    RateLimiterBucket bucket;
    synchronized (mutex) {
        bucket = getStore().get(bucketId, RateLimiterBucket.class);
        if (bucket == null) {
            bucket = new RateLimiterBucket();
            getStore().put(bucketId, bucket);
        }
        bucket.resetIfNecessary(period);
        RateLimitResponse response = new RateLimitResponse();
        if (bucket.getCount() > limit) {
            response.setAccepted(false);
        } else {
            response.setAccepted(bucket.getCount() < limit);
            bucket.setCount(bucket.getCount() + increment);
            bucket.setLast(System.currentTimeMillis());
        }
        int reset = (int) (bucket.getResetMillis(period) / 1000L);
        response.setReset(reset);
        response.setRemaining(limit - bucket.getCount());
        handler.handle(AsyncResultImpl.create(response));
        getStore().put(bucketId, bucket);
    }
}
Also used : RateLimiterBucket(io.apiman.gateway.engine.rates.RateLimiterBucket) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse)

Aggregations

RateLimiterBucket (io.apiman.gateway.engine.rates.RateLimiterBucket)6 RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)4 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Properties (java.util.Properties)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 GetRequest (org.elasticsearch.action.get.GetRequest)1 GetResponse (org.elasticsearch.action.get.GetResponse)1