From 2e48d32a4d22657119a430e5f646b08043e21caf Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 12 Apr 2025 23:50:45 +0100 Subject: [PATCH] handler: make client persistent --- src/core/Handler.cpp | 20 ++++++++++++-------- src/core/Handler.hpp | 10 ++++++++++ src/main.cpp | 2 ++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/core/Handler.cpp b/src/core/Handler.cpp index c870e21..3fbef3c 100644 --- a/src/core/Handler.cpp +++ b/src/core/Handler.cpp @@ -13,9 +13,6 @@ #include #include -#define private public -#include -#undef private #include #include #include @@ -88,6 +85,17 @@ static std::string sha256(const std::string& string) { return ss.str(); } +void CServerHandler::init() { + m_client = new Pistache::Http::Experimental::Client(); + m_client->init(Pistache::Http::Experimental::Client::options().threads(1).maxConnectionsPerHost(8)); +} + +void CServerHandler::finish() { + m_client->shutdown(); + delete m_client; + m_client = nullptr; +} + void CServerHandler::onRequest(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter response) { const auto HEADERS = req.headers(); std::shared_ptr hostHeader; @@ -274,13 +282,11 @@ void CServerHandler::serveStop(const Pistache::Http::Request& req, Pistache::Htt } void CServerHandler::proxyPass(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter& response) { - Pistache::Http::Experimental::Client client; - client.init(Pistache::Http::Experimental::Client::options().threads(1).maxConnectionsPerHost(8)); const std::string FORWARD_ADDR = g_pConfig->m_config.forward_address; Debug::log(LOG, "Method ({}): Forwarding to {}", (uint32_t)req.method(), FORWARD_ADDR + req.resource()); - auto builder = client.prepareRequest(FORWARD_ADDR + req.resource(), req.method()); + auto builder = m_client->prepareRequest(FORWARD_ADDR + req.resource(), req.method()); builder.body(req.body()); for (auto it = req.cookies().begin(); it != req.cookies().end(); ++it) { builder.cookie(*it); @@ -330,6 +336,4 @@ void CServerHandler::proxyPass(const Pistache::Http::Request& req, Pistache::Htt }); Pistache::Async::Barrier b(resp); b.wait_for(std::chrono::seconds(g_pConfig->m_config.proxy_timeout_sec)); - - client.shutdown(); } \ No newline at end of file diff --git a/src/core/Handler.hpp b/src/core/Handler.hpp index 6c438f3..edd98de 100644 --- a/src/core/Handler.hpp +++ b/src/core/Handler.hpp @@ -2,10 +2,18 @@ #include +// Giga hack, but we need it cuz the API is quite awkward and incomplete +#define private public +#include +#undef private + class CServerHandler : public Pistache::Http::Handler { HTTP_PROTOTYPE(CServerHandler) + void init(); + void finish(); + void onRequest(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter response); void onTimeout(const Pistache::Http::Request& request, Pistache::Http::ResponseWriter response); @@ -25,4 +33,6 @@ class CServerHandler : public Pistache::Http::Handler { std::string token = ""; std::string error = ""; }; + + Pistache::Http::Experimental::Client* m_client = nullptr; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4ca03ac..a26e654 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,6 +82,7 @@ int main(int argc, char** argv, char** envp) { opts.maxRequestSize(g_pConfig->m_config.max_request_size); // 150MB TODO: configurable endpoint->init(opts); auto handler = Pistache::Http::make_handler(); + handler->init(); endpoint->setHandler(handler); endpoint->serveThreaded(); @@ -110,6 +111,7 @@ int main(int argc, char** argv, char** envp) { Debug::log(LOG, "Shutting down, bye!"); + handler->finish(); endpoint->shutdown(); endpoint = nullptr;