RequestServer: Make the ThreadPool global

Previously we made one thread pool per ipc client, which is excessive
and can eat up all the available fds very fast.
Fixes #760.
This commit is contained in:
Ali Mohammad Pur
2024-07-22 22:52:21 +02:00
committed by Andreas Kling
parent c396054c87
commit 18499c4eac
2 changed files with 35 additions and 28 deletions

View File

@@ -21,12 +21,27 @@
namespace RequestServer {
template<typename Pool>
struct Looper : public Threading::ThreadPoolLooper<Pool> {
IterationDecision next(Pool& pool, bool wait);
Core::EventLoop event_loop;
};
struct ThreadPoolEntry {
NonnullRefPtr<ConnectionFromClient> client;
ConnectionFromClient::Work work;
};
static Threading::ThreadPool<ThreadPoolEntry, Looper> s_thread_pool {
[](ThreadPoolEntry entry) {
entry.client->worker_do_work(move(entry.work));
}
};
static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
static IDAllocator s_client_ids;
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionFromClient<RequestClientEndpoint, RequestServerEndpoint>(*this, move(socket), s_client_ids.allocate())
, m_thread_pool([this](Work work) { worker_do_work(move(work)); })
{
s_connections.set(client_id(), *this);
}
@@ -74,7 +89,7 @@ private:
};
template<typename Pool>
IterationDecision ConnectionFromClient::Looper<Pool>::next(Pool& pool, bool wait)
IterationDecision Looper<Pool>::next(Pool& pool, bool wait)
{
bool should_exit = false;
auto timer = Core::Timer::create_repeating(100, [&] {
@@ -187,7 +202,7 @@ Messages::RequestServer::ConnectNewClientResponse ConnectionFromClient::connect_
void ConnectionFromClient::enqueue(Work work)
{
m_thread_pool.submit(move(work));
s_thread_pool.submit({ *this, move(work) });
}
Messages::RequestServer::IsSupportedProtocolResponse ConnectionFromClient::is_supported_protocol(ByteString const& protocol)