WebFlux uses only one thread

Viewed 200

I have a client application, using WebFlux, and it seems to be using only a single thread, named "t-reactor-http-epoll-2".

This is the code that starts the connection provider:

ConnectionProvider connectionProvider = ConnectionProvider.builder("some name")
  .maxConnections(parallelism)
  .pendingAcquireMaxCount(-1)
  .build();

and I've verified parralelism is not 1.

I'm printing logs at the onNext() and they all appear to be coming from this single thread.

This runs on a Linux, inside a docker image (managed by kubernetes).

When I run a demo program using WebFlux, I see multiple threads.

Any help would be appreciated.

1 Answers

Well, I think I know the answer to my question. It has to do with better understanding of the threading model in WebFlux, as explained in Concurrency in Spring WebFlux (www.baeldung.com):

The thread that sends the request does not block. It just receives a Publisher (from the underlying infrastructure) and adds a Subscriber to it, to handle the response.

When data is available for a certain request, it would be picked up by the infrastructure and published via the Publisher supplied for that request.

The fact that we've set the number of connections to a certain number, only means the number of connections is limited. It is not necessarily related to the number of threads the infrastructure would chose to use or to the way it would distribute the responses between the threads.

I'm not sure why in some scenarios we see all the responses being processed by a single infrastructure thread and in other scenarios we see them served by different threads

However, it is not relevant, and not under user's control (at least not in the main path).

What we actually need to do is to create a Scheduler (thread pool) of our own, with number of threads equal to the maximum number of connections we specify, and set "publishOn" with this scheduler on all the Fluxes (and Monos) which come out of WebClient.get() calls.

Related