yhirose/cpp-httplib

Max Server-Sent Events client count ?

reicrof opened this issue · 2 comments

Hello !

I have been tinkering with the library and really like the ease of use and simplicity of it, thanks for all the good work !

I am facing an issue with Server-Sent Events but am unclear if I am misusing the library or if there is something more to it. The issue I am seeing is that after a certain amount of client listening to an event source, the server becomes unresponsive.

The easiest repro for the issue is simply to use the Server side example of the SSE from the documentation and spawn a few clients listening to the events. I simply run the server code, and then spawn few terminals and run curl http://localhost:8080/event1. After 11 instances, the server becomes unresponsive and no other clients can listen to the events (or even send GET/POST request).

I don't think that number is a coincidence since std::thread::hardware_concurrency() returns 12 on my machine. Looking at the stacktrace, it looks like all the threads fromthe threadpool are associated with a single client, and no other thread is left to process incoming requests.

I did notice the message in the documentation that says This library uses 'blocking' socket I/O, but I think this is not the issue here since we are not blocked on IO, but rather on the cond_var. (I am terrible at network stuff, so I might be very wrong here).

Is there something I am missing ? Any suggestion on how to address this ?

Thanks a lot !

@reicrof thanks for the report. The number of worker threads of the default thread pool used by cpp-httplib is CPPHTTPLIB_THREAD_POOL_COUNT , and the number of the SSE clients cannot exceed the number. So first, you have to determine the expected max client connections, and increase the number of worker threads with CPPHTTPLIB_THREAD_POOL_COUNT. Or you can create your own thread pool class with which you can fully configure the multi-thread activity.

https://github.com/yhirose/cpp-httplib?tab=readme-ov-file#default-thread-pool-support

cpp-httplib/httplib.h

Lines 126 to 129 in 5c1a34e

#define CPPHTTPLIB_THREAD_POOL_COUNT \
((std::max)(8u, std::thread::hardware_concurrency() > 0 \
? std::thread::hardware_concurrency() - 1 \
: 0))

Ok thanks for the fast answer. I'll take a deeper look at how the task scheduling is done on the server and probably have my own thread pool then