Phase C: run RPC per-client handlers on a Void ThreadPool

Replace the one-detached-std::thread-per-client model and its manual
bookkeeping (the clientThreads_ vector, finished-thread sweep, and
detach-after-timeout shutdown) with a Void::Concurrency::ThreadPool:

- start() builds clientPool_ (CLIENT_POOL_SIZE workers); the accept loop
  submits each accepted connection's handleClient + cleanup as a task.
- stop() closes the client sockets (so each handler's read() returns) then
  resets the pool, whose destructor drains and joins every handler.

The accept loop stays on its own dedicated thread — a long-lived listen
loop is not pool work, and its cancellable/timeout-guarded shutdown is
unchanged. CLIENT_POOL_SIZE (64) caps concurrent long-lived client handlers
far above any realistic count for this bot's RPC surface.

Build green. Runtime behavior (live RPC sessions) is unverified here.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-23 13:26:16 +03:00
1 parent ef094c0d78
commit 3af9aecbec
2 files changed
+32 -54

No files matched your search

+9 -3
View File
@@ -4,10 +4,12 @@
#include <RpcTypes.hpp>
#include <TrackTypes.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <functional>
#include <memory>
#include <mutex>
#include <set>
#include <thread>
@@ -81,9 +83,13 @@ namespace Echoion::Core
std::mutex clientsMutex_;
std::atomic<uint64_t> nextClientId_{1};
// Client threads (for proper cleanup)
std::vector<std::thread> clientThreads_;
std::mutex clientThreadsMutex_;
// Per-client handlers run on a Void worker pool instead of one
// detached std::thread each; ~ThreadPool drains and joins them on
// stop. Sized well above any realistic concurrent-client count
// (each long-lived handler holds a worker for the connection's
// lifetime).
static constexpr std::size_t CLIENT_POOL_SIZE = 64;
std::unique_ptr<Void::Concurrency::ThreadPool> clientPool_;
// Authenticated clients
std::set<uint64_t> authenticatedClients_;
+23 -51
View File
@@ -29,6 +29,8 @@ namespace Echoion::Core
acceptor_->bind(endpoint);
acceptor_->listen();
clientPool_ = std::make_unique<Void::Concurrency::ThreadPool>(
CLIENT_POOL_SIZE);
serverThread_ = std::thread(&RpcServer::acceptLoop, this);
ECHOION_LOG_INFO(
@@ -69,27 +71,10 @@ namespace Echoion::Core
clients_.clear();
}
// Wait for all client threads to finish (with timeout)
{
std::lock_guard<std::mutex> lock(clientThreadsMutex_);
for (auto& thread : clientThreads_) {
if (thread.joinable()) {
// Give each thread a short time to finish
auto start = std::chrono::steady_clock::now();
while (thread.joinable() &&
std::chrono::steady_clock::now() - start <
std::chrono::milliseconds(100)) {
std::this_thread::sleep_for(
std::chrono::milliseconds(10));
}
if (thread.joinable()) {
thread.detach(); // Only detach if still running
// after timeout
}
}
}
clientThreads_.clear();
}
// Drain and join the client handlers. Their sockets were just
// closed, so each handleClient read() fails and the task returns;
// ~ThreadPool then joins every worker.
clientPool_.reset();
// Wait for server thread with timeout
if (serverThread_.joinable()) {
@@ -175,37 +160,24 @@ namespace Echoion::Core
ECHOION_LOG_DEBUG(
std::format("RPC client {} connected", clientId));
// Handle client in separate thread
std::thread clientThread([this, clientId, ws]() {
handleClient(ws);
// Handle the client on the worker pool (drained/joined by
// ~ThreadPool on stop).
if (clientPool_) {
clientPool_->submit([this, clientId, ws]() {
handleClient(ws);
// Cleanup
{
std::lock_guard<std::mutex> lock(clientsMutex_);
clients_.erase(clientId);
}
{
std::lock_guard<std::mutex> lock(authMutex_);
authenticatedClients_.erase(clientId);
}
ECHOION_LOG_DEBUG(
std::format("RPC client {} disconnected", clientId));
});
// Track the thread for proper cleanup
{
std::lock_guard<std::mutex> lock(clientThreadsMutex_);
// Clean up any finished threads first
clientThreads_.erase(std::remove_if(clientThreads_.begin(),
clientThreads_.end(),
[](std::thread& t) {
if (!t.joinable()) {
return true;
}
return false;
}),
clientThreads_.end());
clientThreads_.push_back(std::move(clientThread));
// Cleanup
{
std::lock_guard<std::mutex> lock(clientsMutex_);
clients_.erase(clientId);
}
{
std::lock_guard<std::mutex> lock(authMutex_);
authenticatedClients_.erase(clientId);
}
ECHOION_LOG_DEBUG(std::format(
"RPC client {} disconnected", clientId));
});
}
} catch (const std::exception& e) {