使用resilience4j实现节流/去抖动

时间:2021-04-14 14:03:19

标签: java throttling debounce debouncing resilience4j

我的问题是如何使用resilience4j 库实现(配置)throttlig。 我尝试在下一个配置中使用 RateLimiter:

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);
}

但它不会阻止(忽略)后续的方法调用。 我需要实现的是,无论任何可能的异常,我的特定方法都不能在 10 秒内被多次调用一次。

请多多指教!

谢谢!

1 个答案:

答案 0 :(得分:1)

解决办法是设置超时时间为0:

timeoutDuration(Duration.ofSeconds(0))

回答https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/RunLambdaSchedule.html