ran client sessions in separated thread pool

This commit is contained in:
2025-11-26 18:01:34 +03:00
parent a42f6dbc98
commit 43638f383f
4 changed files with 128 additions and 62 deletions

View File

@@ -23,6 +23,7 @@
#include <asio/signal_set.hpp>
#include <asio/steady_timer.hpp>
#include <asio/streambuf.hpp>
#include <asio/thread_pool.hpp>
#include <asio/write.hpp>
#include <spdlog/sinks/null_sink.h>
@@ -181,7 +182,8 @@ public:
_asioContext(ctx),
_handleMessageFunc(func),
_stopSignal(ctx),
_restartSignal(ctx)
_restartSignal(ctx),
_sessionThreadPool(7)
{
std::stringstream st;
st << std::this_thread::get_id();
@@ -359,10 +361,21 @@ public:
// start accepting connections
for (;;) {
st.str("");
st << std::this_thread::get_id();
logDebug(std::format("Start accepting new connections (thread ID = {}) ...", st.str()));
auto sock = co_await acc.async_accept(asio::use_awaitable);
// start new client session
asio::co_spawn(_asioContext, startSession(std::move(sock)), asio::detached);
logDebug(std::format("session was spawned, start accepting new connections ..."));
logDebug("Spawn new user session ...");
// asio::co_spawn(_asioContext, startSession(std::move(sock)), asio::detached);
// use of sessions own thread pool
asio::co_spawn(_sessionThreadPool, startSession(std::move(sock)), asio::detached);
logDebug("The session was spawned");
}
@@ -621,6 +634,8 @@ protected:
std::mutex _serialPortsMutex, _tcpSocketsMutex, _localStreamSocketsMutex, _localSeqpackSocketsMutex;
asio::thread_pool _sessionThreadPool;
// helpers
template <typename OptT, typename... OptTs>
void setSerialOpts(asio::serial_port& s_port, OptT&& opt, OptTs&&... opts)