feat: add thread pools

This commit is contained in:
2026-04-11 12:21:59 -03:00
parent 32525c1566
commit 642c1c80fa
2 changed files with 41 additions and 1 deletions
+32 -1
View File
@@ -205,6 +205,24 @@ bool Proxy::init()
listen(_listenSocket, SOMAXCONN);
_running = true;
for (int i = 0; i < 16; ++i)
{
_poolThreads.emplace_back([this]() {
while (_running)
{
SOCKET client;
{
std::unique_lock<std::mutex> lock(_queueMutex);
_queueCond.wait(lock, [this]() { return !_clientQueue.empty() || !_running; });
if (!_running && _clientQueue.empty()) return;
client = _clientQueue.front();
_clientQueue.pop();
}
this->handleClient(client);
}
});
}
_workerThread = std::thread(&Proxy::loop, this);
Log::verbose("Proxy active on 127.0.0.1:{}", PROXY_PORT);
@@ -222,6 +240,14 @@ void Proxy::shutdown()
_listenSocket = INVALID_SOCKET;
}
if (_workerThread.joinable()) _workerThread.join();
_queueCond.notify_all();
for (auto& t : _poolThreads)
{
if (t.joinable()) t.join();
}
_poolThreads.clear();
WSACleanup();
cleanupSSL();
}
@@ -237,7 +263,12 @@ void Proxy::loop()
break;
}
if (client == INVALID_SOCKET) continue;
std::thread([this, client]() { this->handleClient(client); }).detach();
{
std::lock_guard<std::mutex> lock(_queueMutex);
_clientQueue.push(client);
}
_queueCond.notify_one();
}
}
+9
View File
@@ -3,6 +3,10 @@
#include <thread>
#include <atomic>
#include <string>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "ssl.h"
@@ -39,6 +43,11 @@ class Proxy
std::thread _workerThread;
std::atomic<bool> _running = false;
std::vector<std::thread> _poolThreads;
std::queue<SOCKET> _clientQueue;
std::mutex _queueMutex;
std::condition_variable _queueCond;
CertManager _certManager;
SSL_CTX* _clientCtx = nullptr;
};