3

We are using Spring WebClient for calling web services using the same.

However, i don't know how to create/manage connection pool in Spring WebClient.

I got to know that we have use 'ReactorClientHttpConnector' but just don't get any sample code.

Basically, i want to have WebClient pool with maxTotal, maxWaitMillis etc.

S Atah Ahmed Khan
  • 1,313
  • 3
  • 14
  • 22

1 Answers1

0

Spring WebClient is a No-Blocking IO http client while ReactorClientHttpConnector is a Reactor-Netty based implementation. Said that I can suggest to do not warry about connection pool but focus on a complete no blocking service call. The key of succes using this kind of technology is all on focus on a complete no blocking service call chain, the model do not involve a thread per request, it is like a browser or node js development if you has something that block your code you block anything. I know that it is so not usual but the base implementation on a event-loop model force you to think about a completely different model. I can comfort you by telling you that typically in a Netty based implementation you have a number of event loop that is the same of the number of your core, it is configurable of course but is think that it is enough, remember the power of reactive and no blocking IO programming is to embrace the no blockin io in all pieces of your code and add more event loop per processor will bring you to add some of concurrency while having one event loop per processor will enabling you to a fully parallel usage of your processor.

I hoe that this reflection can help you

TIP. for timeout on your http service call you can add a timeout on your like in the test below:

@Test
@WithMockUser(username = "user")
fun `read basic personal details data`() {
    personalDetailsRepository.save("RESUME_ID", TestCase.personalDetails()).toMono().block();

    val expectedJson = TestCase.readFileAsString("personal-details.json")
    webClient.get()
            .uri("/resume/RESUME_ID/personal-details")
            .accept(MediaType.APPLICATION_JSON)
            .exchange().toMono().timeout(Duration.ofMinutes(1))

} 

Update

Considering the request of restrict on application level the webclient of course it is possible use the Backpressure feature in order to deal with a data stream that may be too large at times to be reliably processed or in case if a stream response like a Flux with the Flux limitRate() operator can be useful taking the official documentation:

/**
     * Ensure that backpressure signals from downstream subscribers are split into batches
     * capped at the provided {@code prefetchRate} when propagated upstream, effectively
     * rate limiting the upstream {@link Publisher}.
     * <p>
     * Note that this is an upper bound, and that this operator uses a prefetch-and-replenish
     * strategy, requesting a replenishing amount when 75% of the prefetch amount has been
     * emitted.
     * <p>
     * Typically used for scenarios where consumer(s) request a large amount of data
     * (eg. {@code Long.MAX_VALUE}) but the data source behaves better or can be optimized
     * with smaller requests (eg. database paging, etc...). All data is still processed,
     * unlike with {@link #limitRequest(long)} which will cap the grand total request
     * amount.
     * <p>
     * Equivalent to {@code flux.publishOn(Schedulers.immediate(), prefetchRate).subscribe() }.
     * Note that the {@code prefetchRate} is an upper bound, and that this operator uses a
     * prefetch-and-replenish strategy, requesting a replenishing amount when 75% of the
     * prefetch amount has been emitted.
     *
     * @param prefetchRate the limit to apply to downstream's backpressure
     *
     * @return a {@link Flux} limiting downstream's backpressure
     * @see #publishOn(Scheduler, int)
     * @see #limitRequest(long)
     */
    public final Flux<T> limitRate(int prefetchRate) {
        return onAssembly(this.publishOn(Schedulers.immediate(), prefetchRate));
    }

said that I suggest to use this features and do not attempt to limit the consuming of data in a forced way like a connection limit. One of the point of strength of reactive programming and No blocking IO is on the incredible efficiency to use the resource and limit the resource usage appear like a against sense of the spirit of paradigm

Valerio Vaudi
  • 4,199
  • 2
  • 24
  • 23