}
}
+/**
+ * Returns the API thread pool.
+ *
+ * @returns The API thread pool.
+ */
+ThreadPool& ApiListener::GetTP()
+{
+ static ThreadPool tp;
+ return tp;
+}
+
+void ApiListener::EnqueueAsyncCallback(const std::function<void ()>& callback, SchedulerPolicy policy)
+{
+ GetTP().Post(callback, policy);
+}
+
void ApiListener::OnConfigLoaded()
{
if (m_Instance)
for (;;) {
try {
Socket::Ptr client = server->Accept();
- std::thread thread(std::bind(&ApiListener::NewClientHandler, this, client, String(), RoleServer));
- thread.detach();
+
+ /* Use dynamic thread pool with additional on demand resources with fast throughput. */
+ EnqueueAsyncCallback(std::bind(&ApiListener::NewClientHandler, this, client, String(), RoleServer), LowLatencyScheduler);
} catch (const std::exception&) {
Log(LogCritical, "ApiListener", "Cannot accept new connection.");
}
TcpSocket::Ptr client = new TcpSocket();
try {
- endpoint->SetConnecting(true);
client->Connect(host, port);
+
NewClientHandler(client, endpoint->GetName(), RoleClient);
+
endpoint->SetConnecting(false);
} catch (const std::exception& ex) {
endpoint->SetConnecting(false);
continue;
}
- std::thread thread(std::bind(&ApiListener::AddConnection, this, endpoint));
- thread.detach();
+ /* Set connecting state to prevent duplicated queue inserts later. */
+ endpoint->SetConnecting(true);
+
+ /* Use dynamic thread pool with additional on demand resources with fast throughput. */
+ EnqueueAsyncCallback(std::bind(&ApiListener::AddConnection, this, endpoint), LowLatencyScheduler);
}
}
#include "base/workqueue.hpp"
#include "base/tcpsocket.hpp"
#include "base/tlsstream.hpp"
+#include "base/threadpool.hpp"
#include <set>
namespace icinga
void NewClientHandlerInternal(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
void ListenerThreadProc(const Socket::Ptr& server);
+ static ThreadPool& GetTP();
+ static void EnqueueAsyncCallback(const std::function<void ()>& callback, SchedulerPolicy policy = DefaultScheduler);
+
WorkQueue m_RelayQueue;
WorkQueue m_SyncQueue{0, 4};