vivi
vivi

Reputation: 345

How to limit parallel execution in Spring-Webflux?

My rest client implementation is using Webflux from Project Reactor and is similar to the following:

Flux<Response1> request1(String uri) {
  return webClient
          .get()
          .uri(uri)
          .retrieve()
          .bodyToMono(Responses1.class)
          .map(r -> response1ToList(r))
          .flatMapMany(Flux::fromIterable);
}

Flux<Response2> request2(Response1 response1) {
  uri = f(response1);
  return webClient
          .get()
          .uri(uri)
          .retrieve
          .bodyToMono(Responses2.class)
          .map(r -> response1ToList(r))
          .flatMapMany(Flux::fromIterable);
}

Flux<Response2> res2 = res1.flatMap(request2).subscribe();

The initial request (request1) returns a list of elements used to send a series of requests (request2).

My problem is that the request2s are all sent in parallel which is too heavy for the server.

Is there a way to limit the number of request2s executed at the same time?

Upvotes: 1

Views: 1291

Answers (1)

Prashant Pandey
Prashant Pandey

Reputation: 4642

You can control the number of inner stream flatMap() subscribes to by passing in a concurrency factor.

Flux<Response1> request1(String uri) {
  return webClient
          .get()
          .uri(uri)
          .retrieve()
          .bodyToMono(Responses1.class)
          .map(r -> response1ToList(r))
          .flatMapMany(Flux::fromIterable);
}

Flux<Response2> request2(Response1 response1) {
  uri = f(response1);
  return webClient
          .get()
          .uri(uri)
          .retrieve
          .bodyToMono(Responses2.class)
          .map(r -> response1ToList(r))
          .flatMapMany(Flux::fromIterable);
}

Flux<Response2> res2 = res1.flatMap(request2, concurrencyFactor).subscribe();

If you want to do it one-by-one, you can use a concatMap().

Upvotes: 4

Related Questions