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:
1 parent
ef094c0d78
commit
3af9aecbec
2 files changed
+32
-54
No files matched your search
+23
-51
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user