0

Given I create my WebClient using the builder() pattern, e.g. somewhat like this:

WebClient.builder()
    .uriBuilderFactory(defaultUriBuilderFactory)
    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
    .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
    .defaultHeader(HttpHeaders.USER_AGENT, USER_AGENT)
    .defaultHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate")
    .exchangeStrategies(ExchangeStrategies.builder()
            .codecs(clientCodecConfigurer -> clientCodecConfigurer
                    .defaultCodecs()
                    .maxInMemorySize(16 * 1024 * 1024))
            .build())
    .build();

Is there any possibility to do rate limiting within the WebClient itself?

I saw some answers, that I can do the rate limiting when I do the request. I would rather define it within the WebClient, because the same WebClient is used for different requests, so I need to set an overall rate limit for this WebClient.

By "rate limit" I mean: How many requests this WebClient is allowed to send per second. For example: I want to limit this WebClient to only send 5 requests per second.

If this is not possible using the WebClient, are there any alternatives that make sense?

meberhard
  • 1,797
  • 1
  • 19
  • 24
  • Probably not the most helpful answer - so a comment instead - if you use the Apache Httpclient implementation of client connector (as opposed to the default Netty) you can configure the connection pool to rate limit total connections or by route. https://stackoverflow.com/a/63982192/2071828 – Boris the Spider Dec 21 '21 at 08:58
  • You might try applying this solution (https://stackoverflow.com/questions/50387584/how-to-limit-the-request-second-with-webclient/65075755#65075755) with an ExchangeFilterFunction: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/reactive/function/client/WebClient.Builder.html#filter-org.springframework.web.reactive.function.client.ExchangeFilterFunction- Not sure if they work well together but it might be worth a try. – Martin Tarjányi Dec 21 '21 at 14:05

0 Answers0