use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.
the class EsRateLimiterComponent method accept.
/**
* @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
*/
@Override
public void accept(final String bucketId, final RateBucketPeriod period, final long limit, final long increment, final IAsyncResultHandler<RateLimitResponse> handler) {
final String id = id(bucketId);
try {
GetResponse response = getClient().get(new GetRequest(getFullIndexName()).id(id), RequestOptions.DEFAULT);
RateLimiterBucket bucket;
long version;
if (response.isExists()) {
// use the existing bucket
version = response.getVersion();
String sourceAsString = response.getSourceAsString();
bucket = JSON_MAPPER.readValue(sourceAsString, RateLimiterBucket.class);
} else {
// make a new bucket
version = 0;
bucket = new RateLimiterBucket();
}
bucket.resetIfNecessary(period);
final RateLimitResponse rlr = new RateLimitResponse();
if (bucket.getCount() > limit) {
rlr.setAccepted(false);
} else {
rlr.setAccepted(bucket.getCount() < limit);
bucket.setCount(bucket.getCount() + increment);
bucket.setLast(System.currentTimeMillis());
}
int reset = (int) (bucket.getResetMillis(period) / 1000L);
rlr.setReset(reset);
rlr.setRemaining(limit - bucket.getCount());
updateBucketAndReturn(id, bucket, rlr, version, bucketId, period, limit, increment, handler);
} catch (Throwable e) {
handler.handle(AsyncResultImpl.create(e, RateLimitResponse.class));
}
}
use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.
the class SingleNodeRateLimiterComponent method accept.
/**
* @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
*/
@Override
public void accept(String bucketId, RateBucketPeriod period, long limit, long increment, IAsyncResultHandler<RateLimitResponse> handler) {
RateLimiterBucket bucket;
RateLimitResponse response = new RateLimitResponse();
synchronized (buckets) {
bucket = buckets.get(bucketId);
if (bucket == null) {
bucket = new RateLimiterBucket();
buckets.put(bucketId, bucket);
}
boolean rateModified = bucket.resetIfNecessary(period);
if (bucket.getCount() > limit) {
response.setAccepted(false);
} else {
response.setAccepted(bucket.getCount() < limit);
bucket.setCount(bucket.getCount() + increment);
bucket.setLast(System.currentTimeMillis());
rateModified = true;
}
int reset = (int) (bucket.getResetMillis(period) / 1000L);
response.setReset(reset);
if (rateModified) {
lastModifiedOn = System.currentTimeMillis();
}
}
response.setRemaining(limit - bucket.getCount());
handler.handle(AsyncResultImpl.create(response));
}
use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.
the class SingleNodeRateLimiterComponent method saveBuckets.
/**
* Saves the current list of buckets into a file.
*/
private void saveBuckets() {
if (lastModifiedOn > lastSavedOn) {
// $NON-NLS-1$
LOGGER.info("Persisting current rates to: {0}", savedRates);
Properties props = new Properties();
for (Entry<String, RateLimiterBucket> entry : buckets.entrySet()) {
// $NON-NLS-1$
String value = entry.getValue().getCount() + "|" + entry.getValue().getLast();
props.setProperty(entry.getKey(), value);
}
try (FileWriter writer = new FileWriter(savedRates)) {
// $NON-NLS-1$
props.store(writer, "All Apiman rate limits");
} catch (IOException e) {
e.printStackTrace();
}
lastSavedOn = System.currentTimeMillis();
}
}
use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.
the class SingleNodeRateLimiterComponent method loadBuckets.
/**
* Loads the saved rates from a file. Done only on startup.
*/
private void loadBuckets() {
Properties props = new Properties();
try (FileReader reader = new FileReader(savedRates)) {
props.load(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
for (Entry<Object, Object> entry : props.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
// $NON-NLS-1$
String[] split = value.split("\\|");
long count = Long.parseLong(split[0]);
long last = Long.parseLong(split[1]);
RateLimiterBucket bucket = new RateLimiterBucket();
bucket.setCount(count);
bucket.setLast(last);
buckets.put(key, bucket);
}
}
use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.
the class InMemoryRateLimiterComponent method accept.
/**
* @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
*/
@Override
public void accept(String bucketId, RateBucketPeriod period, long limit, long increment, IAsyncResultHandler<RateLimitResponse> handler) {
RateLimiterBucket bucket;
RateLimitResponse response = new RateLimitResponse();
synchronized (buckets) {
bucket = buckets.get(bucketId);
if (bucket == null) {
bucket = new RateLimiterBucket();
buckets.put(bucketId, bucket);
}
bucket.resetIfNecessary(period);
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));
}
Aggregations