Skip to content

Commit

Permalink
proxy: use rtimer instead of qtimer for everything (#48028)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkarneges authored Jun 26, 2024
1 parent d16d545 commit 311780f
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 97 deletions.
6 changes: 5 additions & 1 deletion src/core/rtimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,14 @@ void RTimer::setSingleShot(bool singleShot)
singleShot_ = singleShot;
}

void RTimer::start(int msec)
void RTimer::setInterval(int msec)
{
interval_ = msec;
}

void RTimer::start(int msec)
{
setInterval(msec);
start();
}

Expand Down
1 change: 1 addition & 0 deletions src/core/rtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RTimer : public QObject
bool isActive() const;

void setSingleShot(bool singleShot);
void setInterval(int msec);
void start(int msec);
void start();
void stop();
Expand Down
8 changes: 4 additions & 4 deletions src/proxy/domainmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <assert.h>
#include <QStringList>
#include <QHash>
#include <QTimer>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
Expand All @@ -35,6 +34,7 @@
#include <QTextStream>
#include <QFileSystemWatcher>
#include "log.h"
#include "rtimer.h"
#include "routesfile.h"

class DomainMap::Worker : public QObject
Expand Down Expand Up @@ -196,14 +196,14 @@ class DomainMap::Worker : public QObject
QList<Rule> allRules;
QHash< QString, QList<Rule> > rulesByDomain;
QHash<QString, Rule> rulesById;
QTimer t;
RTimer t;
Connection tConnection;
QFileSystemWatcher watcher;

Worker() :
t(this),
watcher(this)
{
connect(&t, &QTimer::timeout, this, &Worker::doReload);
tConnection = t.timeout.connect(boost::bind(&Worker::doReload, this));
t.setSingleShot(true);
}

Expand Down
11 changes: 8 additions & 3 deletions src/proxy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@
*/

#include <QCoreApplication>
#include <QTimer>
#include "app.h"

class AppMain
class AppMain : public QObject
{
Q_OBJECT

public:
App *app;

public slots:
void start()
{
app = new App;
app->quit.connect(boost::bind(&AppMain::app_quit, this, boost::placeholders::_1));
app->start();
}

private:
void app_quit(int returnCode)
{
delete app;
Expand All @@ -50,8 +53,10 @@ int proxy_main(int argc, char **argv)
QCoreApplication qapp(argc, argv);

AppMain appMain;
QTimer::singleShot(0, [&appMain]() {appMain.start();});
QMetaObject::invokeMethod(&appMain, "start", Qt::QueuedConnection);
return qapp.exec();
}

}

#include "main.moc"
30 changes: 10 additions & 20 deletions src/proxy/sockjsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <assert.h>
#include <QtGlobal>
#include <QTimer>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
Expand All @@ -35,6 +34,7 @@
#include "qtcompat.h"
#include "log.h"
#include "bufferlist.h"
#include "rtimer.h"
#include "zhttprequest.h"
#include "zwebsocket.h"
#include "sockjssession.h"
Expand Down Expand Up @@ -99,16 +99,16 @@ class SockJsManager::Private : public QObject
QByteArray lastPart;
bool pending;
SockJsSession *ext;
QTimer *timer;
std::unique_ptr<RTimer> timer;
QVariant closeValue;
Connection timerConnection;

Session(Private *_owner) :
owner(_owner),
req(0),
sock(0),
pending(false),
ext(0),
timer(0)
ext(0)
{
}

Expand All @@ -121,13 +121,6 @@ class SockJsManager::Private : public QObject
}
owner->wsConnectionMap.erase(sock);
delete sock;

if(timer)
{
timer->disconnect(owner);
timer->setParent(0);
timer->deleteLater();
}
}
};

Expand All @@ -148,7 +141,7 @@ class SockJsManager::Private : public QObject
QHash<ZWebSocket*, Session*> sessionsBySocket;
QHash<QByteArray, Session*> sessionsById;
QHash<SockJsSession*, Session*> sessionsByExt;
QHash<QTimer*, Session*> sessionsByTimer;
QHash<RTimer*, Session*> sessionsByTimer;
QList<Session*> pendingSessions;
QByteArray iframeHtml;
QByteArray iframeHtmlEtag;
Expand Down Expand Up @@ -190,7 +183,7 @@ class SockJsManager::Private : public QObject
if(s->ext)
sessionsByExt.remove(s->ext);
if(s->timer)
sessionsByTimer.remove(s->timer);
sessionsByTimer.remove(s->timer.get());
sessions.remove(s);
delete s;
}
Expand All @@ -206,13 +199,10 @@ class SockJsManager::Private : public QObject
if(s->closeValue.isValid())
{
// if there's a close value, hang around for a little bit
s->timer = new QTimer(this);
QObject::connect(s->timer, &QTimer::timeout, [this, timer=s->timer]() {
this->timer_timeout(timer);
});

s->timer = std::make_unique<RTimer>();
s->timerConnection = s->timer->timeout.connect(boost::bind(&Private::timer_timeout, this, s->timer.get()));
s->timer->setSingleShot(true);
sessionsByTimer.insert(s->timer, s);
sessionsByTimer.insert(s->timer.get(), s);
s->timer->start(5000);
}
else
Expand Down Expand Up @@ -684,7 +674,7 @@ class SockJsManager::Private : public QObject
}

private:
void timer_timeout(QTimer *timer)
void timer_timeout(RTimer *timer)
{
Session *s = sessionsByTimer.value(timer);
assert(s);
Expand Down
13 changes: 5 additions & 8 deletions src/proxy/sockjssession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <assert.h>
#include <QPointer>
#include <QTimer>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
Expand All @@ -34,6 +33,7 @@
#include "log.h"
#include "bufferlist.h"
#include "packet/httprequestdata.h"
#include "rtimer.h"
#include "zhttprequest.h"
#include "zwebsocket.h"
#include "sockjsmanager.h"
Expand Down Expand Up @@ -155,7 +155,7 @@ class SockJsSession::Private : public QObject
int pendingWrittenBytes;
QList<WriteItem> pendingWrites;
QHash<ZhttpRequest*, RequestItem*> requests;
QTimer *keepAliveTimer;
std::unique_ptr<RTimer> keepAliveTimer;
int closeCode;
QString closeReason;
bool closeSent;
Expand All @@ -165,6 +165,7 @@ class SockJsSession::Private : public QObject
bool updating;
map<ZhttpRequest*, ReqConnections> reqConnectionMap;
WSConnections wsConnection;
Connection keepAliveTimerConnection;

Private(SockJsSession *_q) :
QObject(_q),
Expand All @@ -186,16 +187,12 @@ class SockJsSession::Private : public QObject
peerCloseCode(-1),
updating(false)
{
keepAliveTimer = new QTimer(this);
connect(keepAliveTimer, &QTimer::timeout, this, &Private::keepAliveTimer_timeout);
keepAliveTimer = std::make_unique<RTimer>();
keepAliveTimerConnection = keepAliveTimer->timeout.connect(boost::bind(&Private::keepAliveTimer_timeout, this));
}

~Private()
{
keepAliveTimer->disconnect(this);
keepAliveTimer->setParent(0);
keepAliveTimer->deleteLater();

cleanup();
}

Expand Down
16 changes: 5 additions & 11 deletions src/proxy/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

#include <QSysInfo>
#include <QDateTime>
#include <QTimer>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QCryptographicHash>
#include <QHostInfo>
#include "qtcompat.h"
#include "log.h"
#include "rtimer.h"
#include "httpheaders.h"
#include "zhttpmanager.h"
#include "zhttprequest.h"
Expand Down Expand Up @@ -83,11 +83,12 @@ class Updater::Private : public QObject
QString currentVersion;
QString org;
ZhttpManager *zhttpManager;
QTimer *timer;
std::unique_ptr<RTimer> timer;
ZhttpRequest *req;
Report report;
QDateTime lastLogTime;
ReqConnections reqConnections;
Connection timerConnection;

Private(Updater *_q, Mode _mode, bool _quiet, const QString &_currentVersion, const QString &_org, ZhttpManager *zhttp) :
QObject(_q),
Expand All @@ -99,21 +100,14 @@ class Updater::Private : public QObject
zhttpManager(zhttp),
req(0)
{
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Private::timer_timeout);
timer = std::make_unique<RTimer>();
timerConnection = timer->timeout.connect(boost::bind(&Private::timer_timeout, this));
timer->setInterval(mode == ReportMode ? REPORT_INTERVAL : CHECK_INTERVAL);
timer->start();

report.connectionsMax = -1; // stale
}

~Private()
{
timer->disconnect(this);
timer->setParent(0);
timer->deleteLater();
}

void cleanupRequest()
{
reqConnections = ReqConnections();
Expand Down
27 changes: 9 additions & 18 deletions src/proxy/websocketoverhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "websocketoverhttp.h"

#include <assert.h>
#include <QTimer>
#include <QPointer>
#include <QRandomGenerator>
#include "log.h"
Expand All @@ -34,6 +33,7 @@
#include "zhttprequest.h"
#include "zhttpmanager.h"
#include "uuidutil.h"
#include "rtimer.h"

#define BUFFER_SIZE 200000
#define FRAME_SIZE_MAX 16384
Expand Down Expand Up @@ -233,11 +233,13 @@ class WebSocketOverHttp::Private : public QObject
bool disconnecting;
bool disconnectSent;
bool updateQueued;
QTimer *keepAliveTimer;
QTimer *retryTimer;
std::unique_ptr<RTimer> keepAliveTimer;
std::unique_ptr<RTimer> retryTimer;
int retries;
int maxEvents;
ReqConnections reqConnections;
Connection keepAliveTimerConnection;
Connection retryTimerConnection;

Private(WebSocketOverHttp *_q) :
QObject(_q),
Expand Down Expand Up @@ -269,26 +271,15 @@ class WebSocketOverHttp::Private : public QObject
if(!g_disconnectManager)
g_disconnectManager = new DisconnectManager;

keepAliveTimer = new QTimer(this);
connect(keepAliveTimer, &QTimer::timeout, this, &Private::keepAliveTimer_timeout);
keepAliveTimer = std::make_unique<RTimer>();
keepAliveTimerConnection = keepAliveTimer->timeout.connect(boost::bind(&Private::keepAliveTimer_timeout, this));
keepAliveTimer->setSingleShot(true);

retryTimer = new QTimer(this);
connect(retryTimer, &QTimer::timeout, this, &Private::retryTimer_timeout);
retryTimer = std::make_unique<RTimer>();
retryTimerConnection = retryTimer->timeout.connect(boost::bind(&Private::retryTimer_timeout, this));
retryTimer->setSingleShot(true);
}

~Private()
{
keepAliveTimer->disconnect(this);
keepAliveTimer->setParent(0);
keepAliveTimer->deleteLater();

retryTimer->disconnect(this);
retryTimer->setParent(0);
retryTimer->deleteLater();
}

void cleanup()
{
keepAliveTimer->stop();
Expand Down
Loading

0 comments on commit 311780f

Please sign in to comment.