How to limit the request/second with WebClient?

前端 未结 4 1671
日久生厌
日久生厌 2020-12-10 05:50

I\'m using a WebClient object to send Http Post request to a server. It\'s sending a huge amount of requests quite rapidly (there is about 4000 messages in a

4条回答
  •  隐瞒了意图╮
    2020-12-10 06:18

    Question Limiting rate of requests with Reactor provides two answrers (one in comment)

    zipWith another flux that acts as rate limiter

    .zipWith(Flux.interval(Duration.of(1, ChronoUnit.SECONDS)))

    just delay each web request

    use delayElements function

    edit: answer below is valid for blocking RestTemplate but do not really fit well into reactive pattern.

    WebClient does not have ability to limit request, but you could easily add this feature using composition.

    You may throttle your client externally using RateLimiter from Guava/ (https://google.github.io/guava/releases/19.0/api/docs/index.html?com/google/common/util/concurrent/RateLimiter.html)

    In this tutorial http://www.baeldung.com/guava-rate-limiter you will find how to use Rate limiter in blocking way, or with timeouts.

    I would decorate all calls that need to be throttled in separate class that

    1. limits number of calls per second
    2. performs actual web call using WebClient

提交回复
热议问题