Search in sources :

Example 1 with BridgeEventType

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);
    }
}
Also used : Bucket(io.github.bucket4j.Bucket) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) StartService(in.erail.glue.annotation.StartService)

Example 2 with BridgeEventType

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());
        }
    }
}
Also used : BridgeEventType(io.vertx.ext.bridge.BridgeEventType)

Example 3 with BridgeEventType

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());
        }
    }
}
Also used : BridgeEventType(io.vertx.ext.bridge.BridgeEventType)

Example 4 with BridgeEventType

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;
    });
}
Also used : Bucket(io.github.bucket4j.Bucket) Bandwidth(io.github.bucket4j.Bandwidth) Refill(io.github.bucket4j.Refill) BridgeEventType(io.vertx.ext.bridge.BridgeEventType)

Example 5 with BridgeEventType

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());
        }
    }
}
Also used : BridgeEventType(io.vertx.ext.bridge.BridgeEventType)

Aggregations

BridgeEventType (io.vertx.ext.bridge.BridgeEventType)6 Bucket (io.github.bucket4j.Bucket)2 StartService (in.erail.glue.annotation.StartService)1 Bandwidth (io.github.bucket4j.Bandwidth)1 Refill (io.github.bucket4j.Refill)1 JsonObject (io.vertx.core.json.JsonObject)1 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1