implement throttling/debouncing using resilience4j

Viewed 175

My question is how to implement (configure) throttlig using resilience4j library. I tried to use RateLimiter with next configuration:

RateLimiter rateLimiter = RateLimiterRegistry.of(
                RateLimiterConfig.custom()
                        .limitRefreshPeriod(Duration.ofSeconds(10))
                        .limitForPeriod(1)
                        .timeoutDuration(Duration.ofSeconds(5))
                        .build()
        ).rateLimiter("default");

Runnable ratedCall = RateLimiter.decorateRunnable(rateLimiter, () -> { /*business logic here*/ });

// and then

try {
    ratedCall.run();
} catch (Exception e) {
    LOG.warn(e);
}

but it does not prevent (ignore) subsequent method calls. What I need to achieve is that my particular method cannot be invoked more that once in 10 seconds regardless of any possible exceptions.

Please, advice!

Thanx!

1 Answers

The solution is to set timeout duration to 0:

timeoutDuration(Duration.ofSeconds(0))

Answered here

Related