handler: make a single client instance
This commit is contained in:
@@ -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<const Pistache::Http::Header::AcceptEncoding> 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<Pistache::Http::Response> b(resp);
|
||||
b.wait_for(std::chrono::seconds(g_pConfig->m_config.proxy_timeout_sec));
|
||||
|
||||
client.shutdown();
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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<CServerHandler>();
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user