qchateau / packio

An asynchronous msgpack-RPC and JSON-RPC library built on top of Boost.Asio.
https://qchateau.github.io/packio/
Mozilla Public License 2.0
138 stars 20 forks source link

Connections handling by separate threads #78

Closed tomaszhlawiczka closed 2 months ago

tomaszhlawiczka commented 10 months ago

Is there a way to handle each connection by a separate thread?

Background: clients keep the connections alive (connection pool) for long period of the time to send many requests to one connection (one after another). Processing of a request, including reading it and writing it to the socket, is quite heavy (consumes a lot of CPU).

How can I start a new thread for each connection, right after accept() ? The new thread suppose to be created once for entire connection, not for each request.

Thank you in advance.

qchateau commented 10 months ago

Hi,

I've never thought about this too much but there is no intended mecanism for a procedure to identify the source connection, meaning you can't dispatch the work based on the connection. That said you have access to a fully async server so you don't need to have one thread per client to maximize CPU occupancy, you can just run the io_context on many threads. You can also have two thread pool, one for the io_context and one to process the CPU intensive part of your processing if you can to make sure the io_context will not starve.