Skip to content

Commit

Permalink
boostification of proxy session (#47866)
Browse files Browse the repository at this point in the history
* boostification of proxy session
  • Loading branch information
sima-fastly authored Jan 9, 2024
1 parent 1ecf52a commit b821a6d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
21 changes: 12 additions & 9 deletions src/cpp/proxy/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class Engine::Private : public QObject
Connection requestReadyConnection;
Connection socketReadyConnection;
Connection iRequestReadyConnection;
map<ProxySession*, Connection> addNotAllowedConnection;
map<ProxySession*, Connection> finishedConnection;
map<ProxySession*, Connection> reqSessionDestroyedConnection;

Private(Engine *_q) :
QObject(_q),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/proxy/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include "jwt.h"
#include "xffrule.h"
#include <boost/signals2.hpp>
#include <map>

using std::map;
using Connection = boost::signals2::scoped_connection;

class StatsManager;
Expand Down
14 changes: 7 additions & 7 deletions src/cpp/proxy/proxysession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -890,7 +890,7 @@ class ProxySession::Private : public QObject

if(wasAllowed && !addAllowed)
{
emit q->addNotAllowed();
q->addNotAllowed();
if(!self)
return;
}
Expand Down Expand Up @@ -921,7 +921,7 @@ class ProxySession::Private : public QObject
if(addAllowed)
{
addAllowed = false;
emit q->addNotAllowed();
q->addNotAllowed();
if(!self)
return;
}
Expand Down Expand Up @@ -1200,7 +1200,7 @@ public slots:
logFinished(si);

QPointer<QObject> self = this;
emit q->requestSessionDestroyed(si->rs, false);
q->requestSessionDestroyed(si->rs, false);
if(!self)
return;

Expand All @@ -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)
{
Expand Down Expand Up @@ -1403,15 +1403,15 @@ public slots:
QPointer<QObject> self = this;
foreach(RequestSession *rs, toDestroy)
{
emit q->requestSessionDestroyed(rs, true);
q->requestSessionDestroyed(rs, true);
delete rs;
if(!self)
return;
}

log_debug("proxysession: %p finished for accept", q);
cleanup();
emit q->finished();
q->finished();
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/cpp/proxy/proxysession.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class XffRule;
class RequestSession;
#include <boost/signals2.hpp>

using Signal = boost::signals2::signal<void()>;
using Connection = boost::signals2::scoped_connection;

class ProxySession : public QObject
Expand All @@ -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<void(RequestSession*, bool)> requestSessionDestroyed;

private:
class Private;
Expand Down
3 changes: 1 addition & 2 deletions src/cpp/simplehttpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit b821a6d

Please sign in to comment.