diff --git a/src/core/Handler.cpp b/src/core/Handler.cpp index 99592b0..d923c50 100644 --- a/src/core/Handler.cpp +++ b/src/core/Handler.cpp @@ -54,6 +54,19 @@ static std::string generateToken() { return ss.str(); } +void CServerHandler::init() { + m_client = new Pistache::Http::Experimental::Client(); + m_client->init(Pistache::Http::Experimental::Client::options().maxConnectionsPerHost(32).maxResponseSize(g_pConfig->m_config.max_request_size).threads(4)); +} + +void CServerHandler::finish() { + if (!m_client) + return; + m_client->shutdown(); + delete m_client; + m_client = nullptr; +} + std::string CServerHandler::fingerprintForRequest(const Pistache::Http::Request& req) { const auto HEADERS = req.headers(); std::shared_ptr acceptEncodingHeader; @@ -364,10 +377,7 @@ void CServerHandler::proxyPass(const Pistache::Http::Request& req, Pistache::Htt Debug::log(TRACE, "Method ({}): Forwarding to {}", (uint32_t)req.method(), FORWARD_ADDR + req.resource()); - Pistache::Http::Experimental::Client client; - client.init(Pistache::Http::Experimental::Client::options().maxConnectionsPerHost(8).maxResponseSize(g_pConfig->m_config.max_request_size).threads(1)); - - 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); @@ -428,6 +438,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 1bd7d56..46cc2c4 100644 --- a/src/core/Handler.hpp +++ b/src/core/Handler.hpp @@ -11,6 +11,9 @@ 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); @@ -34,4 +37,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 1270f18..492d067 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,6 +88,7 @@ int main(int argc, char** argv, char** envp) { opts.maxRequestSize(g_pConfig->m_config.max_request_size); endpoint->init(opts); auto handler = Pistache::Http::make_handler(); + handler->init(); endpoint->setHandler(handler); endpoint->serveThreaded(); @@ -116,6 +117,7 @@ int main(int argc, char** argv, char** envp) { Debug::log(LOG, "Shutting down, bye!"); + handler->finish(); endpoint->shutdown(); endpoint = nullptr;