Skip to content

Commit d0dff96

Browse files
authored
core, proxy, handler: use DeferCall::defer everywhere (#48103)
1 parent f0036eb commit d0dff96

File tree

14 files changed

+78
-59
lines changed

14 files changed

+78
-59
lines changed

src/core/qzmq/src/qzmqvalve.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2012-2020 Justin Karneges
3+
* Copyright (C) 2025 Fastly, Inc.
34
*
45
* Permission is hereby granted, free of charge, to any person obtaining a
56
* copy of this software and associated documentation files (the
@@ -25,6 +26,7 @@
2526

2627
#include <QPointer>
2728
#include "qzmqsocket.h"
29+
#include "defercall.h"
2830

2931
namespace QZmq {
3032

@@ -39,6 +41,7 @@ class Valve::Private : public QObject
3941
bool pendingRead;
4042
int maxReadsPerEvent;
4143
boost::signals2::scoped_connection rrConnection;
44+
DeferCall deferCall;
4245

4346
Private(Valve *_q) :
4447
QObject(_q),
@@ -62,7 +65,7 @@ class Valve::Private : public QObject
6265
return;
6366

6467
pendingRead = true;
65-
QMetaObject::invokeMethod(this, "queuedRead", Qt::QueuedConnection);
68+
deferCall.defer([=] { queuedRead(); });
6669
}
6770

6871
void tryRead()
@@ -99,7 +102,6 @@ class Valve::Private : public QObject
99102
tryRead();
100103
}
101104

102-
private slots:
103105
void queuedRead()
104106
{
105107
pendingRead = false;

src/core/zrpcrequest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2014-2015 Fanout, Inc.
3-
* Copyright (C) 2024 Fastly, Inc.
3+
* Copyright (C) 2024-2025 Fastly, Inc.
44
*
55
* This file is part of Pushpin.
66
*
@@ -31,6 +31,7 @@
3131
#include "uuidutil.h"
3232
#include "log.h"
3333
#include "rtimer.h"
34+
#include "defercall.h"
3435

3536
using Connection = boost::signals2::scoped_connection;
3637

@@ -52,6 +53,7 @@ class ZrpcRequest::Private : public QObject
5253
QByteArray conditionString;
5354
std::unique_ptr<RTimer> timer;
5455
Connection timerConnection;
56+
DeferCall deferCall;
5557

5658
Private(ZrpcRequest *_q) :
5759
QObject(_q),
@@ -136,7 +138,6 @@ class ZrpcRequest::Private : public QObject
136138
q->finished();
137139
}
138140

139-
private slots:
140141
void doStart()
141142
{
142143
if(!manager->canWriteImmediately())
@@ -238,7 +239,7 @@ void ZrpcRequest::start(const QString &method, const QVariantHash &args)
238239
{
239240
d->method = method;
240241
d->args = args;
241-
QMetaObject::invokeMethod(d, "doStart", Qt::QueuedConnection);
242+
d->deferCall.defer([=] { d->doStart(); });
242243
}
243244

244245
void ZrpcRequest::respond(const QVariant &result)

src/core/zwebsocket.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class ZWebSocket::Private : public QObject
9696
bool multi;
9797
Connection expireTimerConnection;
9898
Connection keepAliveTimerConnection;
99+
DeferCall deferCall;
99100

100101
Private(ZWebSocket *_q) :
101102
QObject(_q),
@@ -266,7 +267,7 @@ class ZWebSocket::Private : public QObject
266267
if(!pendingUpdate)
267268
{
268269
pendingUpdate = true;
269-
QMetaObject::invokeMethod(this, "doUpdate", Qt::QueuedConnection);
270+
deferCall.defer([=] { doUpdate(); });
270271
}
271272
}
272273

@@ -298,7 +299,7 @@ class ZWebSocket::Private : public QObject
298299

299300
state = Idle;
300301
cleanup();
301-
QMetaObject::invokeMethod(this, "doClosed", Qt::QueuedConnection);
302+
deferCall.defer([=] { doClosed(); });
302303
}
303304

304305
Frame readFrame()
@@ -338,7 +339,7 @@ class ZWebSocket::Private : public QObject
338339
// if peer was already closed, then we're done!
339340
state = Idle;
340341
cleanup();
341-
QMetaObject::invokeMethod(this, "doClosed", Qt::QueuedConnection);
342+
deferCall.defer([=] { doClosed(); });
342343
}
343344
else
344345
{
@@ -977,7 +978,6 @@ class ZWebSocket::Private : public QObject
977978
return ErrorGeneric;
978979
}
979980

980-
public slots:
981981
void doClosed()
982982
{
983983
q->closed();
@@ -1067,7 +1067,6 @@ public slots:
10671067
}
10681068
}
10691069

1070-
public:
10711070
void expire_timeout()
10721071
{
10731072
state = Idle;

src/handler/deferred.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015 Fanout, Inc.
3+
* Copyright (C) 2025 Fastly, Inc.
34
*
45
* This file is part of Pushpin.
56
*
@@ -42,7 +43,7 @@ void Deferred::setFinished(bool ok, const QVariant &value)
4243
result_.success = ok;
4344
result_.value = value;
4445

45-
QMetaObject::invokeMethod(this, "doFinish", Qt::QueuedConnection);
46+
deferCall_.defer([=] { doFinish(); });
4647
}
4748

4849
void Deferred::doFinish()

src/handler/deferred.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015 Fanout, Inc.
3+
* Copyright (C) 2025 Fastly, Inc.
34
*
45
* This file is part of Pushpin.
56
*
@@ -26,6 +27,7 @@
2627
#include <QVariant>
2728
#include <QObject>
2829
#include <boost/signals2.hpp>
30+
#include "defercall.h"
2931

3032
class DeferredResult
3133
{
@@ -63,11 +65,11 @@ class Deferred : public QObject
6365

6466
void setFinished(bool ok, const QVariant &value = QVariant());
6567

66-
private slots:
67-
void doFinish();
68-
6968
private:
7069
DeferredResult result_;
70+
DeferCall deferCall_;
71+
72+
void doFinish();
7173
};
7274

7375
#endif

src/handler/handlermain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2016 Fanout, Inc.
3-
* Copyright (C) 2024 Fastly, Inc.
3+
* Copyright (C) 2024-2025 Fastly, Inc.
44
*
55
* This file is part of Pushpin.
66
*
@@ -22,7 +22,6 @@
2222
*/
2323

2424
#include <QCoreApplication>
25-
#include <QTimer>
2625
#include "rtimer.h"
2726
#include "defercall.h"
2827
#include "handlerapp.h"
@@ -53,7 +52,8 @@ int handler_main(int argc, char **argv)
5352
QCoreApplication qapp(argc, argv);
5453

5554
HandlerAppMain appMain;
56-
QTimer::singleShot(0, [&appMain]() {appMain.start();});
55+
DeferCall deferCall;
56+
deferCall.defer([&] { appMain.start(); });
5757
int ret = qapp.exec();
5858

5959
// ensure deferred deletes are processed

src/handler/httpsession.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class HttpSession::Private : public QObject
206206
Connection timerConnection;
207207
Connection retryTimerConnection;
208208
Connection messageFiltersFinishedConnection;
209+
DeferCall deferCall;
209210

210211
Private(HttpSession *_q, ZhttpRequest *_req, const HttpSession::AcceptData &_adata, const Instruct &_instruct, ZhttpManager *_outZhttp, StatsManager *_stats, RateLimiter *_updateLimiter, PublishLastIds *_publishLastIds, HttpSessionUpdateManager *_updateManager, int _connectionSubscriptionMax) :
211212
QObject(_q),
@@ -1164,7 +1165,7 @@ class HttpSession::Private : public QObject
11641165
if(!outZhttp)
11651166
{
11661167
errorMessage = "Instruct contained link, but handler not configured for outbound requests.";
1167-
QMetaObject::invokeMethod(this, "doError", Qt::QueuedConnection);
1168+
deferCall.defer([=] { doError(); });
11681169
return;
11691170
}
11701171

@@ -1457,7 +1458,6 @@ class HttpSession::Private : public QObject
14571458
}
14581459
}
14591460

1460-
private slots:
14611461
void doError()
14621462
{
14631463
if(instruct.holdMode == Instruct::ResponseHold)
@@ -1621,7 +1621,6 @@ private slots:
16211621
}
16221622
}
16231623

1624-
private:
16251624
void timer_timeout()
16261625
{
16271626
if(instruct.holdMode == Instruct::ResponseHold)

src/proxy/domainmap.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2012-2022 Fanout, Inc.
3-
* Copyright (C) 2023-2024 Fastly, Inc.
3+
* Copyright (C) 2023-2025 Fastly, Inc.
44
*
55
* This file is part of Pushpin.
66
*
@@ -205,6 +205,7 @@ class DomainMap::Worker : public QObject
205205
RTimer t;
206206
Connection tConnection;
207207
QFileSystemWatcher watcher;
208+
DeferCall deferCall;
208209

209210
Worker() :
210211
watcher(this)
@@ -292,7 +293,7 @@ class DomainMap::Worker : public QObject
292293

293294
log_info("routes loaded with %d entries", allRules.count());
294295

295-
QMetaObject::invokeMethod(this, "doChanged", Qt::QueuedConnection);
296+
deferCall.defer([=] { doChanged(); });
296297
}
297298

298299
// mutex must be locked when calling this method
@@ -312,11 +313,6 @@ class DomainMap::Worker : public QObject
312313
Signal changed;
313314

314315
public slots:
315-
void doChanged()
316-
{
317-
changed();
318-
}
319-
320316
void start()
321317
{
322318
if(!fileName.isEmpty())
@@ -719,6 +715,11 @@ public slots:
719715

720716
return AddRuleOk;
721717
}
718+
719+
void doChanged()
720+
{
721+
changed();
722+
}
722723
};
723724

724725
class DomainMap::Thread : public QThread

src/proxy/main.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2016 Fanout, Inc.
3+
* Copyright (C) 2025 Fastly, Inc.
34
*
45
* This file is part of Pushpin.
56
*
@@ -22,15 +23,13 @@
2223

2324
#include <QCoreApplication>
2425
#include "app.h"
26+
#include "defercall.h"
2527

26-
class AppMain : public QObject
28+
class AppMain
2729
{
28-
Q_OBJECT
29-
3030
public:
3131
App *app;
3232

33-
public slots:
3433
void start()
3534
{
3635
app = new App;
@@ -53,10 +52,17 @@ int proxy_main(int argc, char **argv)
5352
QCoreApplication qapp(argc, argv);
5453

5554
AppMain appMain;
56-
QMetaObject::invokeMethod(&appMain, "start", Qt::QueuedConnection);
57-
return qapp.exec();
58-
}
55+
DeferCall deferCall;
56+
deferCall.defer([&] { appMain.start(); });
57+
int ret = qapp.exec();
58+
59+
// ensure deferred deletes are processed
60+
QCoreApplication::instance()->sendPostedEvents();
5961

62+
// deinit here, after all event loop activity has completed
63+
DeferCall::cleanup();
64+
65+
return ret;
6066
}
6167

62-
#include "main.moc"
68+
}

src/proxy/requestsession.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2012-2023 Fanout, Inc.
3-
* Copyright (C) 2024 Fastly, Inc.
3+
* Copyright (C) 2024-2025 Fastly, Inc.
44
*
55
* This file is part of Pushpin.
66
*
@@ -36,6 +36,7 @@
3636
#include "qtcompat.h"
3737
#include "bufferlist.h"
3838
#include "log.h"
39+
#include "defercall.h"
3940
#include "layertracker.h"
4041
#include "sockjsmanager.h"
4142
#include "inspectdata.h"
@@ -200,6 +201,7 @@ class RequestSession::Private : public QObject
200201
ZhttpReqConnections zhttpReqConnections;
201202
Connection inspectFinishedConnection;
202203
Connection acceptFinishedConnection;
204+
DeferCall deferCall;
203205

204206
Private(RequestSession *_q, int _workerId, DomainMap *_domainMap = 0, SockJsManager *_sockJsManager = 0, ZrpcManager *_inspectManager = 0, ZrpcChecker *_inspectChecker = 0, ZrpcManager *_acceptManager = 0, StatsManager *_stats = 0) :
205207
QObject(_q),
@@ -308,7 +310,7 @@ class RequestSession::Private : public QObject
308310
isSockJs = true;
309311
sockJsManager->giveRequest(zhttpRequest, route.sockJsPath.length(), route.sockJsAsPath, route);
310312
zhttpRequest = 0;
311-
QMetaObject::invokeMethod(this, "doFinished", Qt::QueuedConnection);
313+
deferCall.defer([=] { doFinished(); });
312314
return;
313315
}
314316
}
@@ -480,7 +482,7 @@ class RequestSession::Private : public QObject
480482
if(!inspectRequest)
481483
{
482484
log_debug("inspect not available");
483-
QMetaObject::invokeMethod(this, "doInspectError", Qt::QueuedConnection);
485+
deferCall.defer([=] { doInspectError(); });
484486
}
485487
}
486488
}
@@ -559,7 +561,7 @@ class RequestSession::Private : public QObject
559561
if(!pendingResponseUpdate)
560562
{
561563
pendingResponseUpdate = true;
562-
QMetaObject::invokeMethod(this, "doResponseUpdate", Qt::QueuedConnection);
564+
deferCall.defer([=] { doResponseUpdate(); });
563565
}
564566
}
565567

@@ -973,7 +975,6 @@ class RequestSession::Private : public QObject
973975
}
974976
}
975977

976-
public slots:
977978
void doResponseUpdate()
978979
{
979980
pendingResponseUpdate = false;

0 commit comments

Comments
 (0)