Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/core.pri
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ HEADERS += \
$$PWD/timerwheel.h \
$$PWD/jwt.h \
$$PWD/rtimer.h \
$$PWD/defercall.h \
$$PWD/logutil.h \
$$PWD/uuidutil.h \
$$PWD/zutil.h \
Expand All @@ -66,6 +67,7 @@ SOURCES += \
$$PWD/timerwheel.cpp \
$$PWD/jwt.cpp \
$$PWD/rtimer.cpp \
$$PWD/defercall.cpp \
$$PWD/logutil.cpp \
$$PWD/uuidutil.cpp \
$$PWD/zutil.cpp \
Expand Down
65 changes: 65 additions & 0 deletions src/core/defercall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2025 Fastly, Inc.
*
* This file is part of Pushpin.
*
* $FANOUT_BEGIN_LICENSE:APACHE2$
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $FANOUT_END_LICENSE$
*/

#include "defercall.h"
#include <assert.h>

static thread_local DeferCall *g_instance = nullptr;

DeferCall::DeferCall() = default;

DeferCall::~DeferCall() = default;

void DeferCall::defer(std::function<void ()> handler)
{
Call c;
c.handler = handler;

deferredCalls_.push_back(c);

QMetaObject::invokeMethod(this, "callNext", Qt::QueuedConnection);
}

DeferCall *DeferCall::global()
{
if(!g_instance)
g_instance = new DeferCall;

return g_instance;
}

void DeferCall::cleanup()
{
delete g_instance;
g_instance = nullptr;
}

void DeferCall::callNext()
{
// there can't be more invokeMethod resolutions than queued calls
assert(!deferredCalls_.empty());

Call c = deferredCalls_.front();
deferredCalls_.pop_front();
Comment on lines +61 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will execute a list of deferred calls in First-In-First-Out order, which seems to make sense to me. But defer in go executes in reverse order. I can't recall the specifics of any other languages right now, but I wonder if there's an expectation for one or other that might cause problems in the future, especially when using the deleteLater feature with complex data structures.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point regarding deleteLater. Will double-check Qt's behavior before landing this, since the goal is for it to work similarly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed the deletions are in-order.

There is one exception: if there are nested event loops, deferred deletions only occur within the event loop that they were originally queued. So processing of deletions within an event loop is FIFO, but processing of the loops themselves is LIFO. In any case, we don't use nested event loops.


c.handler();
}
67 changes: 67 additions & 0 deletions src/core/defercall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2025 Fastly, Inc.
*
* This file is part of Pushpin.
*
* $FANOUT_BEGIN_LICENSE:APACHE2$
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $FANOUT_END_LICENSE$
*/

#ifndef DEFERCALL_H
#define DEFERCALL_H

#include <QObject>

// queues calls to be run after returning to the event loop
class DeferCall : public QObject
{
Q_OBJECT

public:
DeferCall();
~DeferCall();

// queue handler to be called after returning to the event loop. if
// handler contains references, they must outlive DeferCall. the
// recommended usage is for each object needing to perform deferred calls
// to keep a DeferCall as a member variable, and only refer to the
// object's own data in the handler. that way, any references are
// guaranteed to live long enough.
void defer(std::function<void ()> handler);

static DeferCall *global();
static void cleanup();

template <typename T>
static void deleteLater(T *p)
{
global()->defer([=]() { delete p; });
}

private slots:
void callNext();

private:
class Call
{
public:
std::function<void ()> handler;
};

std::list<Call> deferredCalls_;
};

#endif
12 changes: 6 additions & 6 deletions src/core/zhttprequest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2012-2021 Fanout, Inc.
* Copyright (C) 2023 Fastly, Inc.
* Copyright (C) 2023-2025 Fastly, Inc.
*
* This file is part of Pushpin.
*
Expand Down Expand Up @@ -30,6 +30,7 @@
#include "bufferlist.h"
#include "log.h"
#include "rtimer.h"
#include "defercall.h"
#include "zhttpmanager.h"
#include "uuidutil.h"

Expand Down Expand Up @@ -105,6 +106,7 @@ class ZhttpRequest::Private : public QObject
bool quiet;
Connection expTimerConnection;
Connection keepAliveTimerConnection;
DeferCall deferCall;

Private(ZhttpRequest *_q) :
QObject(_q),
Expand Down Expand Up @@ -163,15 +165,15 @@ class ZhttpRequest::Private : public QObject
{
expTimerConnection.disconnect();
expireTimer->setParent(0);
expireTimer->deleteLater();
DeferCall::deleteLater(expireTimer);
expireTimer = 0;
}

if(keepAliveTimer)
{
keepAliveTimerConnection.disconnect();
keepAliveTimer->setParent(0);
keepAliveTimer->deleteLater();
DeferCall::deleteLater(keepAliveTimer);
keepAliveTimer = 0;
}

Expand Down Expand Up @@ -367,7 +369,7 @@ class ZhttpRequest::Private : public QObject
if(!pendingUpdate)
{
pendingUpdate = true;
QMetaObject::invokeMethod(this, "doUpdate", Qt::QueuedConnection);
deferCall.defer([&]() { doUpdate(); });
}
}

Expand Down Expand Up @@ -935,7 +937,6 @@ class ZhttpRequest::Private : public QObject
return ErrorGeneric;
}

public slots:
void doUpdate()
{
pendingUpdate = false;
Expand Down Expand Up @@ -1157,7 +1158,6 @@ public slots:
}
}

public:
void expire_timeout()
{
state = Stopped;
Expand Down
2 changes: 2 additions & 0 deletions src/handler/handlerenginetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "zhttpresponsepacket.h"
#include "packet/httpresponsedata.h"
#include "rtimer.h"
#include "defercall.h"
#include "handlerengine.h"

namespace {
Expand Down Expand Up @@ -309,6 +310,7 @@ private slots:
delete wrapper;

RTimer::deinit();
DeferCall::cleanup();
}

void acceptNoHold()
Expand Down
2 changes: 2 additions & 0 deletions src/handler/handlermain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QCoreApplication>
#include <QTimer>
#include "rtimer.h"
#include "defercall.h"
#include "handlerapp.h"

class HandlerAppMain
Expand Down Expand Up @@ -60,6 +61,7 @@ int handler_main(int argc, char **argv)

// deinit here, after all event loop activity has completed
RTimer::deinit();
DeferCall::cleanup();

return ret;
}
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QFileInfo>
#include "processquit.h"
#include "rtimer.h"
#include "defercall.h"
#include "log.h"
#include "settings.h"
#include "xffrule.h"
Expand Down Expand Up @@ -299,6 +300,7 @@ class EngineThread : public QThread

// deinit here, after all event loop activity has completed
RTimer::deinit();
DeferCall::cleanup();
}

private:
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/domainmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <QFileSystemWatcher>
#include "log.h"
#include "rtimer.h"
#include "defercall.h"
#include "routesfile.h"

#define WORKER_THREAD_TIMERS 1
Expand Down Expand Up @@ -756,6 +757,7 @@ class DomainMap::Thread : public QThread
delete worker;

RTimer::deinit();
DeferCall::cleanup();
}

public:
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/proxyenginetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "packet/httpresponsedata.h"
#include "packet/statspacket.h"
#include "rtimer.h"
#include "defercall.h"
#include "zhttpmanager.h"
#include "statsmanager.h"
#include "domainmap.h"
Expand Down Expand Up @@ -636,6 +637,7 @@ private slots:

QCoreApplication::instance()->sendPostedEvents();
RTimer::deinit();
DeferCall::cleanup();
}

void passthrough()
Expand Down
Loading