handler: make a single client instance
This commit is contained in:
@@ -54,6 +54,19 @@ static std::string generateToken() {
|
|||||||
return ss.str();
|
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) {
|
std::string CServerHandler::fingerprintForRequest(const Pistache::Http::Request& req) {
|
||||||
const auto HEADERS = req.headers();
|
const auto HEADERS = req.headers();
|
||||||
std::shared_ptr<const Pistache::Http::Header::AcceptEncoding> acceptEncodingHeader;
|
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());
|
Debug::log(TRACE, "Method ({}): Forwarding to {}", (uint32_t)req.method(), FORWARD_ADDR + req.resource());
|
||||||
|
|
||||||
Pistache::Http::Experimental::Client client;
|
auto builder = m_client->prepareRequest(FORWARD_ADDR + req.resource(), req.method());
|
||||||
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());
|
|
||||||
builder.body(req.body());
|
builder.body(req.body());
|
||||||
for (auto it = req.cookies().begin(); it != req.cookies().end(); ++it) {
|
for (auto it = req.cookies().begin(); it != req.cookies().end(); ++it) {
|
||||||
builder.cookie(*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);
|
Pistache::Async::Barrier<Pistache::Http::Response> b(resp);
|
||||||
b.wait_for(std::chrono::seconds(g_pConfig->m_config.proxy_timeout_sec));
|
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)
|
HTTP_PROTOTYPE(CServerHandler)
|
||||||
|
|
||||||
|
void init();
|
||||||
|
void finish();
|
||||||
|
|
||||||
void onRequest(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter response);
|
void onRequest(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter response);
|
||||||
|
|
||||||
void onTimeout(const Pistache::Http::Request& request, 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 token = "";
|
||||||
std::string error = "";
|
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);
|
opts.maxRequestSize(g_pConfig->m_config.max_request_size);
|
||||||
endpoint->init(opts);
|
endpoint->init(opts);
|
||||||
auto handler = Pistache::Http::make_handler<CServerHandler>();
|
auto handler = Pistache::Http::make_handler<CServerHandler>();
|
||||||
|
handler->init();
|
||||||
endpoint->setHandler(handler);
|
endpoint->setHandler(handler);
|
||||||
|
|
||||||
endpoint->serveThreaded();
|
endpoint->serveThreaded();
|
||||||
@@ -116,6 +117,7 @@ int main(int argc, char** argv, char** envp) {
|
|||||||
|
|
||||||
Debug::log(LOG, "Shutting down, bye!");
|
Debug::log(LOG, "Shutting down, bye!");
|
||||||
|
|
||||||
|
handler->finish();
|
||||||
endpoint->shutdown();
|
endpoint->shutdown();
|
||||||
endpoint = nullptr;
|
endpoint = nullptr;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user