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);
}
}
Aggregations