handler: make client persistent

This commit is contained in:
Vaxry
2025-04-12 23:50:45 +01:00
parent 7e348baf8c
commit 2e48d32a4d
3 changed files with 24 additions and 8 deletions

View File

@@ -13,9 +13,6 @@
#include <random>
#include <sstream>
#define private public
#include <pistache/client.h>
#undef private
#include <tinylates/tinylates.hpp>
#include <fmt/format.h>
#include <glaze/glaze.hpp>
@@ -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<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) {
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<Pistache::Http::Response> b(resp);
b.wait_for(std::chrono::seconds(g_pConfig->m_config.proxy_timeout_sec));
client.shutdown();
}

View File

@@ -2,10 +2,18 @@
#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 {
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;
};

View File

@@ -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<CServerHandler>();
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;