Search code examples
javaspring-bootreactive-programmingspring-webflux

Maximum Subscribers per Sinks.Many in Spring Boot WebFlux


I have a simple spring boot WebFlux application that streams some server sent events to clients. The code is like the following:

Sinks.Many<String> sink = Sinks.many().multicast().onBackpressureBuffer();

@GetMapping(value = "/stream/{id}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<String>> getStream(@PathVariable String id) {
    return sink.asFlux().map(e -> ServerSentEvent.builder(e).build());
}

I found that when subscriber count is above 50, the Sinks begins to automatically cancel connected subscribers to keep the max count at 50. I don't know how to increase this limit. Is this a setting in WebFlux, reactive or spring boot?

Thank you.


Solution

  • Figured this out. I have a ThreadPoolTaskExecutor configured for the spring boot. And I set threadPoolTaskExecutor.setQueueCapacity(50). It seems this value limits the max number of subscribers for sinks.many. The default value for setQueueCapacity is Integer.MAX_VALUE. Guess I will just leave it to default value.