Search code examples
rx-javaresilience4jrx-java3

Slowing down RxJava3 Flowable with Resilience4j RateLimiter


I've tried the Resilience4J example, but the rate limiter does not seem to obey my demands:

@Test
public void simpleReactiveWithRateLimiter() {
    RateLimiterConfig rateLimiterConfig = RateLimiterConfig.custom()
        .limitForPeriod(5)
        .limitRefreshPeriod(Duration.ofSeconds(1))
        .timeoutDuration(Duration.ofMillis(100))
        .build();
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.of(rateLimiterConfig);
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("tenPerSec", rateLimiterConfig);
    Flowable.interval(1, TimeUnit.MILLISECONDS)
        .takeWhile(f -> f < 100)
        .compose(RateLimiterOperator.of(rateLimiter))
        .blockingSubscribe(i -> log.info("Received {}", i));
}

It still processes 100 items in less then 200ms. I must be doing something wrong, not sure what. Could someone help?


Solution

  • the Resilience4j RateLimiter does not limit the rate of items consumed by one subscription (or emitted by one Flowable). The RateLimiter limits the number of (concurrent) subscriptions on one Flowable. For example if many clients want to subscribe to the same upstream Flowable, e.g. HTTP Call.