use of io.vertx.ext.bridge.BridgeEventType 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.vertx.ext.bridge.BridgeEventType in project api-framework by vinscom.
the class RateLimiterProcessor method setTokenBucketSize.
public void setTokenBucketSize(Map<String, String> pTokenBucketSize) {
for (BridgeEventType type : BridgeEventType.values()) {
if (pTokenBucketSize.containsKey(type.toString())) {
String value = pTokenBucketSize.get(type.toString());
mTokenBucketSize.put(type, Integer.parseInt(value));
} else {
mTokenBucketSize.put(type, getDefaultTokenBucketSize());
}
}
}
use of io.vertx.ext.bridge.BridgeEventType in project api-framework by vinscom.
the class RateLimiterProcessor method setRateOfTokenFillDuration.
public void setRateOfTokenFillDuration(Map<String, String> pRateOfTokenFillDuration) {
for (BridgeEventType type : BridgeEventType.values()) {
if (pRateOfTokenFillDuration.containsKey(type.toString())) {
String value = pRateOfTokenFillDuration.get(type.toString());
mRateOfTokenFillDuration.put(type, Integer.parseInt(value));
} else {
mRateOfTokenFillDuration.put(type, getDefaultRateOfTokenFillDuration());
}
}
}
use of io.vertx.ext.bridge.BridgeEventType 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;
});
}
use of io.vertx.ext.bridge.BridgeEventType in project api-framework by vinscom.
the class RateLimiterProcessor method setRateOfTokenFill.
public void setRateOfTokenFill(Map<String, String> pRateOfTokenFill) {
for (BridgeEventType type : BridgeEventType.values()) {
if (pRateOfTokenFill.containsKey(type.toString())) {
String value = pRateOfTokenFill.get(type.toString());
mRateOfTokenFill.put(type, Integer.parseInt(value));
} else {
mRateOfTokenFill.put(type, getDefaultRateOfTokenFill());
}
}
}
Aggregations