use of io.github.bucket4j.Bucket in project api-framework by vinscom.
the class RateLimiterProcessor method start.
@StartService
public void start() {
for (BridgeEventType type : BridgeEventType.values()) {
Cache<String, Bucket> cache = CacheBuilder.newBuilder().recordStats().expireAfterAccess(getExpireAfterAccess(), TimeUnit.SECONDS).maximumSize(getMaximumSize()).build();
getCache().put(type, cache);
}
}
use of io.github.bucket4j.Bucket in project snow-owl by b2ihealthcare.
the class Bucket4jRateLimiter method consume.
@Override
public RateLimitConsumption consume(String username) {
Bucket bucket = bucketByUser.get(username);
if (bucket == null) {
bucket = createNewBucket();
bucketByUser.put(username, bucket);
}
final ConsumptionProbe probe = bucket.tryConsumeAndReturnRemaining(1);
return new RateLimitConsumption(probe.isConsumed(), probe.getRemainingTokens(), TimeUnit.NANOSECONDS.toSeconds(probe.getNanosToWaitForRefill()));
}
use of io.github.bucket4j.Bucket in project bucket4j-spring-boot-starter by MarcGiffing.
the class Bucket4JBaseConfiguration method buildFilterConfig.
public FilterConfiguration buildFilterConfig(Bucket4JConfiguration config, CacheManager cacheManager, ExpressionParser expressionParser, BeanFactory beanFactory) {
FilterConfiguration filterConfig = new FilterConfiguration();
filterConfig.setUrl(config.getUrl());
filterConfig.setOrder(config.getFilterOrder());
filterConfig.setStrategy(config.getStrategy());
filterConfig.setHttpResponseBody(config.getHttpResponseBody());
ProxyManager<String> buckets = Bucket4j.extension(JCache.class).proxyManagerForCache(jCache(config.getCacheName(), cacheManager));
config.getRateLimits().forEach(rl -> {
ConfigurationBuilder<?> configBuilder = Bucket4j.configurationBuilder();
for (BandWidthConfig bandWidth : rl.getBandwidths()) {
configBuilder = configBuilder.addLimit(Bandwidth.simple(bandWidth.getCapacity(), Duration.of(bandWidth.getTime(), bandWidth.getUnit())));
}
;
final ConfigurationBuilder<?> configBuilderToUse = configBuilder;
RateLimitCheck rlc = (servletRequest) -> {
boolean skipRateLimit = false;
if (rl.getSkipCondition() != null) {
skipRateLimit = skipCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
}
if (rl.getExecuteCondition() != null && !skipRateLimit) {
skipRateLimit = !executeCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
}
if (!skipRateLimit) {
String key = getKeyFilter(filterConfig.getUrl(), rl, expressionParser, beanFactory).key(servletRequest);
Bucket bucket = buckets.getProxy(key, () -> configBuilderToUse.buildConfiguration());
ConsumptionProbe probe = bucket.tryConsumeAndReturnRemaining(1);
return probe;
}
return null;
};
filterConfig.getRateLimitChecks().add(rlc);
});
return filterConfig;
}
use of io.github.bucket4j.Bucket in project api-framework by vinscom.
the class RateLimiterProcessor method process.
@Override
public Single<BridgeEventContext> process(Single<BridgeEventContext> pContext) {
return pContext.map((ctx) -> {
if (ctx.getBridgeEvent().failed() || !isEnable()) {
return ctx;
}
BridgeEventType eventType = ctx.getBridgeEvent().type();
Bucket bucket = getCache().get(eventType).get(ctx.getBridgeEvent().socket().writeHandlerID(), () -> {
Refill refill = Refill.smooth(getRateOfTokenFill().getOrDefault(eventType, getDefaultRateOfTokenFill()), Duration.ofSeconds(getRateOfTokenFillDuration().getOrDefault(eventType, getDefaultRateOfTokenFillDuration())));
Bandwidth limit = Bandwidth.classic(getTokenBucketSize().getOrDefault(eventType, getDefaultTokenBucketSize()), refill);
return Bucket4j.builder().addLimit(limit).build();
});
if (!bucket.tryConsume(1)) {
getLog().debug(() -> String.format("[%s] Rate limit crossed for connection:[%s],event:[%s]", ctx.getId(), ctx.getBridgeEvent().socket().writeHandlerID(), eventType.toString()));
ctx.getBridgeEvent().fail("Rate Limit Crossed");
}
return ctx;
});
}
Aggregations