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
@@ -4,10 +4,12 @@
|
|||||||
#include <RpcTypes.hpp>
|
#include <RpcTypes.hpp>
|
||||||
#include <TrackTypes.hpp>
|
#include <TrackTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
|
#include <Void/Async/ThreadPool.hpp>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
#include <boost/beast/websocket.hpp>
|
#include <boost/beast/websocket.hpp>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -81,9 +83,13 @@ namespace Echoion::Core
|
|||||||
std::mutex clientsMutex_;
|
std::mutex clientsMutex_;
|
||||||
std::atomic<uint64_t> nextClientId_{1};
|
std::atomic<uint64_t> nextClientId_{1};
|
||||||
|
|
||||||
// Client threads (for proper cleanup)
|
// Per-client handlers run on a Void worker pool instead of one
|
||||||
std::vector<std::thread> clientThreads_;
|
// detached std::thread each; ~ThreadPool drains and joins them on
|
||||||
std::mutex clientThreadsMutex_;
|
// 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
|
// Authenticated clients
|
||||||
std::set<uint64_t> authenticatedClients_;
|
std::set<uint64_t> authenticatedClients_;
|
||||||
|
|||||||
+23
-51
@@ -29,6 +29,8 @@ namespace Echoion::Core
|
|||||||
acceptor_->bind(endpoint);
|
acceptor_->bind(endpoint);
|
||||||
acceptor_->listen();
|
acceptor_->listen();
|
||||||
|
|
||||||
|
clientPool_ = std::make_unique<Void::Concurrency::ThreadPool>(
|
||||||
|
CLIENT_POOL_SIZE);
|
||||||
serverThread_ = std::thread(&RpcServer::acceptLoop, this);
|
serverThread_ = std::thread(&RpcServer::acceptLoop, this);
|
||||||
|
|
||||||
ECHOION_LOG_INFO(
|
ECHOION_LOG_INFO(
|
||||||
@@ -69,27 +71,10 @@ namespace Echoion::Core
|
|||||||
clients_.clear();
|
clients_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all client threads to finish (with timeout)
|
// Drain and join the client handlers. Their sockets were just
|
||||||
{
|
// closed, so each handleClient read() fails and the task returns;
|
||||||
std::lock_guard<std::mutex> lock(clientThreadsMutex_);
|
// ~ThreadPool then joins every worker.
|
||||||
for (auto& thread : clientThreads_) {
|
clientPool_.reset();
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for server thread with timeout
|
// Wait for server thread with timeout
|
||||||
if (serverThread_.joinable()) {
|
if (serverThread_.joinable()) {
|
||||||
@@ -175,37 +160,24 @@ namespace Echoion::Core
|
|||||||
ECHOION_LOG_DEBUG(
|
ECHOION_LOG_DEBUG(
|
||||||
std::format("RPC client {} connected", clientId));
|
std::format("RPC client {} connected", clientId));
|
||||||
|
|
||||||
// Handle client in separate thread
|
// Handle the client on the worker pool (drained/joined by
|
||||||
std::thread clientThread([this, clientId, ws]() {
|
// ~ThreadPool on stop).
|
||||||
handleClient(ws);
|
if (clientPool_) {
|
||||||
|
clientPool_->submit([this, clientId, ws]() {
|
||||||
|
handleClient(ws);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||||
clients_.erase(clientId);
|
clients_.erase(clientId);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(authMutex_);
|
std::lock_guard<std::mutex> lock(authMutex_);
|
||||||
authenticatedClients_.erase(clientId);
|
authenticatedClients_.erase(clientId);
|
||||||
}
|
}
|
||||||
ECHOION_LOG_DEBUG(
|
ECHOION_LOG_DEBUG(std::format(
|
||||||
std::format("RPC client {} disconnected", clientId));
|
"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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
|
|||||||
Reference in new issue
Block a user