handler: make client persistent
This commit is contained in:
@@ -13,9 +13,6 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#define private public
|
|
||||||
#include <pistache/client.h>
|
|
||||||
#undef private
|
|
||||||
#include <tinylates/tinylates.hpp>
|
#include <tinylates/tinylates.hpp>
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include <glaze/glaze.hpp>
|
#include <glaze/glaze.hpp>
|
||||||
@@ -88,6 +85,17 @@ static std::string sha256(const std::string& string) {
|
|||||||
return ss.str();
|
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) {
|
void CServerHandler::onRequest(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter response) {
|
||||||
const auto HEADERS = req.headers();
|
const auto HEADERS = req.headers();
|
||||||
std::shared_ptr<const Pistache::Http::Header::Host> hostHeader;
|
std::shared_ptr<const Pistache::Http::Header::Host> 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) {
|
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;
|
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());
|
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());
|
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);
|
||||||
@@ -330,6 +336,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();
|
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,18 @@
|
|||||||
|
|
||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
|
|
||||||
|
// Giga hack, but we need it cuz the API is quite awkward and incomplete
|
||||||
|
#define private public
|
||||||
|
#include <pistache/client.h>
|
||||||
|
#undef private
|
||||||
|
|
||||||
class CServerHandler : public Pistache::Http::Handler {
|
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);
|
||||||
@@ -25,4 +33,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;
|
||||||
};
|
};
|
||||||
@@ -82,6 +82,7 @@ int main(int argc, char** argv, char** envp) {
|
|||||||
opts.maxRequestSize(g_pConfig->m_config.max_request_size); // 150MB TODO: configurable
|
opts.maxRequestSize(g_pConfig->m_config.max_request_size); // 150MB TODO: configurable
|
||||||
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();
|
||||||
@@ -110,6 +111,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