From b821a6d4562bef1599f307e44562164ef5cd99e4 Mon Sep 17 00:00:00 2001 From: Sima <64804941+sima-fastly@users.noreply.github.com> Date: Mon, 8 Jan 2024 20:03:08 -0800 Subject: [PATCH] boostification of proxy session (#47866) * boostification of proxy session --- src/cpp/proxy/engine.cpp | 21 ++++++++++++--------- src/cpp/proxy/engine.h | 2 ++ src/cpp/proxy/proxysession.cpp | 14 +++++++------- src/cpp/proxy/proxysession.h | 8 ++++---- src/cpp/simplehttpserver.cpp | 3 +-- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/cpp/proxy/engine.cpp b/src/cpp/proxy/engine.cpp index d40b8da9..7de9ad7c 100644 --- a/src/cpp/proxy/engine.cpp +++ b/src/cpp/proxy/engine.cpp @@ -116,6 +116,9 @@ class Engine::Private : public QObject Connection requestReadyConnection; Connection socketReadyConnection; Connection iRequestReadyConnection; + map addNotAllowedConnection; + map finishedConnection; + map reqSessionDestroyedConnection; Private(Engine *_q) : QObject(_q), @@ -392,9 +395,9 @@ class Engine::Private : public QObject ps = new ProxySession(zroutes, accept, logConfig, stats); // TODO: use callbacks for performance - connect(ps, &ProxySession::addNotAllowed, this, &Private::ps_addNotAllowed); - connect(ps, &ProxySession::finished, this, &Private::ps_finished); - connect(ps, &ProxySession::requestSessionDestroyed, this, &Private::ps_requestSessionDestroyed); + addNotAllowedConnection[ps] = ps->addNotAllowed.connect(boost::bind(&Private::ps_addNotAllowed, this, ps)); + finishedConnection[ps] = ps->finished.connect(boost::bind(&Private::ps_finished, this, ps)); + reqSessionDestroyedConnection[ps] = ps->requestSessionDestroyed.connect(boost::bind(&Private::ps_requestSessionDestroyed, this, boost::placeholders::_1, boost::placeholders::_2)); ps->setRoute(route); ps->setDefaultSigKey(config.sigIss, config.sigKey); @@ -761,10 +764,8 @@ private slots: tryTakeNext(); } - void ps_addNotAllowed() + void ps_addNotAllowed(ProxySession *ps) { - ProxySession *ps = (ProxySession *)sender(); - ProxyItem *i = proxyItemsBySession.value(ps); assert(i); @@ -776,13 +777,15 @@ private slots: } } - void ps_finished() + void ps_finished(ProxySession *ps) { - ProxySession *ps = (ProxySession *)sender(); - ProxyItem *i = proxyItemsBySession.value(ps); assert(i); + addNotAllowedConnection.erase(ps); + finishedConnection.erase(ps); + reqSessionDestroyedConnection.erase(ps); + if(i->shared) proxyItemsByKey.remove(i->key); proxyItemsBySession.remove(i->ps); diff --git a/src/cpp/proxy/engine.h b/src/cpp/proxy/engine.h index eed2281a..152dd627 100644 --- a/src/cpp/proxy/engine.h +++ b/src/cpp/proxy/engine.h @@ -30,7 +30,9 @@ #include "jwt.h" #include "xffrule.h" #include +#include +using std::map; using Connection = boost::signals2::scoped_connection; class StatsManager; diff --git a/src/cpp/proxy/proxysession.cpp b/src/cpp/proxy/proxysession.cpp index 33a8a88e..d07f056b 100644 --- a/src/cpp/proxy/proxysession.cpp +++ b/src/cpp/proxy/proxysession.cpp @@ -198,7 +198,7 @@ class ProxySession::Private : public QObject foreach(SessionItem *si, sessionItems) { // emitting a signal here is gross, but this way the engine cleans up the request sessions - emit q->requestSessionDestroyed(si->rs, false); + q->requestSessionDestroyed(si->rs, false); delete si->rs; delete si; } @@ -890,7 +890,7 @@ class ProxySession::Private : public QObject if(wasAllowed && !addAllowed) { - emit q->addNotAllowed(); + q->addNotAllowed(); if(!self) return; } @@ -921,7 +921,7 @@ class ProxySession::Private : public QObject if(addAllowed) { addAllowed = false; - emit q->addNotAllowed(); + q->addNotAllowed(); if(!self) return; } @@ -1200,7 +1200,7 @@ public slots: logFinished(si); QPointer self = this; - emit q->requestSessionDestroyed(si->rs, false); + q->requestSessionDestroyed(si->rs, false); if(!self) return; @@ -1216,7 +1216,7 @@ public slots: if(sessionItems.isEmpty()) { log_debug("proxysession: %p finished by passthrough", q); - emit q->finished(); + q->finished(); } else if(wasInputRequest) { @@ -1403,7 +1403,7 @@ public slots: QPointer self = this; foreach(RequestSession *rs, toDestroy) { - emit q->requestSessionDestroyed(rs, true); + q->requestSessionDestroyed(rs, true); delete rs; if(!self) return; @@ -1411,7 +1411,7 @@ public slots: log_debug("proxysession: %p finished for accept", q); cleanup(); - emit q->finished(); + q->finished(); } else { diff --git a/src/cpp/proxy/proxysession.h b/src/cpp/proxy/proxysession.h index e70080bb..58a5194f 100644 --- a/src/cpp/proxy/proxysession.h +++ b/src/cpp/proxy/proxysession.h @@ -40,6 +40,7 @@ class XffRule; class RequestSession; #include +using Signal = boost::signals2::signal; using Connection = boost::signals2::scoped_connection; class ProxySession : public QObject @@ -65,10 +66,9 @@ class ProxySession : public QObject // takes ownership void add(RequestSession *rs); -signals: - void addNotAllowed(); // no more sharing, for whatever reason - void finished(); - void requestSessionDestroyed(RequestSession *rs, bool accept); + Signal addNotAllowed; // no more sharing, for whatever reason + Signal finished; + boost::signals2::signal requestSessionDestroyed; private: class Private; diff --git a/src/cpp/simplehttpserver.cpp b/src/cpp/simplehttpserver.cpp index f38d2080..bec9ba51 100644 --- a/src/cpp/simplehttpserver.cpp +++ b/src/cpp/simplehttpserver.cpp @@ -551,8 +551,7 @@ SimpleHttpRequest *SimpleHttpServer::takeNext() if(!d->pending.isEmpty()) { SimpleHttpRequest *req = d->pending.takeFirst(); - auto conn = d->finishedConnections.find(req); - d->finishedConnections.erase(conn); + d->finishedConnections.erase(req); return req; }