mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
A kept-alive connection holds a worker thread while it waits for its next request, so connections beyond the thread count wait for one to be closed, and with a pool smaller than the number of clients some clients get nothing until the keep-alive timeout or the max count ends another connection. With keep-alive parking, listen() runs an event loop on the detail::Poller instead of the blocking accept loop. A connection waiting for its next request is parked there, and goes back to a worker through the TaskQueue when the request arrives. The handlers, content providers and WebSocket keep their blocking API and still hold a thread while they run. It is opt-in and off by default. With it on: - consecutive requests on a connection may run on different threads; - when the queue is full, a connection that has not been served yet is closed as before, but an established one waits in a backlog instead of losing its request; - the loop owns the listening socket while it watches it, and stop() hands it over instead of closing it under the poller; - SSLServer ignores the setting for now, and an override of process_and_close_socket() is not called. On two CPUs with two workers, 50 keep-alive clients of a 2.7 ms handler were all served evenly (the slowest response took 70 ms instead of 5 s with 10 clients getting nothing), light requests from 50 clients went from 82k to 248k per second, and 10 clients were served after 10000 idle connections that had stopped the old loop. With no more clients than workers, handing the connection over costs about half of the requests per second of a very light handler. The old accept loop moved unchanged into accept_loop(). process_server_socket_core() now takes the stream, and drain_and_close_socket() takes how long to wait for the peer.