Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure timer preallocation counts timers used by filters #48115

Merged
merged 2 commits into from
Feb 13, 2025
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/handler/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ Filter::Targets Filter::targets(const QString &name)

Filter::MessageFilterStack::MessageFilterStack(const QStringList &filterNames)
{
assert(filterNames.count() <= MESSAGEFILTERSTACK_SIZE_MAX);

foreach(const QString &name, filterNames)
{
MessageFilter *f = createMessageFilter(name);
Expand Down
5 changes: 5 additions & 0 deletions src/handler/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include <boost/signals2.hpp>
#include "ratelimiter.h"

#define MESSAGEFILTERSTACK_SIZE_MAX 5

// 2 timers per zhttprequest
#define TIMERS_PER_MESSAGEFILTERSTACK (2 * MESSAGEFILTERSTACK_SIZE_MAX)

class ZhttpManager;

class Filter
Expand Down
20 changes: 11 additions & 9 deletions src/handler/handlerengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@
#define INSPECT_WORKERS_MAX 10
#define ACCEPT_WORKERS_MAX 10

// each session can have a bunch of timers:
// 2 per incoming zhttprequest
// 2 per outgoing zhttprequest
// 2 per httpsession
#define TIMERS_PER_SESSION 10

using namespace VariantUtil;

static QList<PublishItem> parseItems(const QVariantList &vitems, bool *ok = 0, QString *errorMessage = 0)
Expand Down Expand Up @@ -1341,8 +1335,10 @@ class HandlerEngine::Private : public QObject
{
config = _config;

int timersPerSession = qMax(TIMERS_PER_HTTPSESSION, TIMERS_PER_WSSESSION);

// enough timers for sessions, plus an extra 100 for misc
RTimer::init((config.connectionsMax * TIMERS_PER_SESSION) + 100);
RTimer::init((config.connectionsMax * timersPerSession) + 100);

publishLimiter->setRate(config.messageRate);
publishLimiter->setHwm(config.messageHwm);
Expand Down Expand Up @@ -2673,7 +2669,7 @@ class HandlerEngine::Private : public QObject
if(e.error != QJsonParseError::NoError || (!doc.isObject() && !doc.isArray()))
{
log_debug("grip control message is not valid json");
return;
continue;
}

if(doc.isObject())
Expand All @@ -2686,13 +2682,19 @@ class HandlerEngine::Private : public QObject
if(!ok)
{
log_debug("failed to parse grip control message: %s", qPrintable(errorMessage));
return;
continue;
}

if(cm.type == WsControlMessage::Subscribe)
{
if(s->channels.count() < config.connectionSubscriptionMax)
{
if(cm.filters.count() > MESSAGEFILTERSTACK_SIZE_MAX)
{
s->sendCloseError(QString("too many filters for channel '%1'").arg(cm.channel));
continue;
}

QString channel = s->channelPrefix + cm.channel;
s->channels += channel;
s->channelFilters[channel] = cm.filters;
Expand Down
8 changes: 8 additions & 0 deletions src/handler/httpsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@
#include "inspectdata.h"
#include "zhttprequest.h"
#include "instruct.h"
#include "filter.h"
#include <boost/signals2.hpp>

// each session can have a bunch of timers:
// 2 per incoming zhttprequest
// 2 per outgoing zhttprequest
// 2 additional timers
// filter timers
#define TIMERS_PER_HTTPSESSION (10 + TIMERS_PER_MESSAGEFILTERSTACK)

using Connection = boost::signals2::scoped_connection;

class QTimer;
Expand Down
7 changes: 7 additions & 0 deletions src/handler/instruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "qtcompat.h"
#include "variantutil.h"
#include "statusreasons.h"
#include "filter.h"

#define DEFAULT_RESPONSE_TIMEOUT 55
#define MINIMUM_RESPONSE_TIMEOUT 5
Expand Down Expand Up @@ -147,6 +148,12 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
c.filters += QString::fromUtf8(param.second);
}

if(c.filters.count() > MESSAGEFILTERSTACK_SIZE_MAX)
{
setError(ok, errorMessage, QString("too many filters for channel '%1'").arg(c.name));
return Instruct();
}

channels += c;
}

Expand Down
4 changes: 3 additions & 1 deletion src/handler/wssession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ void WsSession::sendCloseError(const QString &message)
i.cid = cid.toUtf8();
i.type = WsControlPacket::Item::Close;
i.code = 1011;
i.reason = message.toUtf8();

if(debug)
i.reason = message.toUtf8();

send(i);
}
Expand Down
6 changes: 5 additions & 1 deletion src/handler/wssession.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "filter.h"
#include <boost/signals2.hpp>

// each session can have a bunch of timers:
// filter timers
#define TIMERS_PER_WSSESSION (0 + TIMERS_PER_MESSAGEFILTERSTACK)

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

Expand Down Expand Up @@ -86,6 +90,7 @@ class WsSession : public QObject
void sendDelayed(const QByteArray &type, const QByteArray &message, int timeout);
void ack(int reqId);
void publish(const PublishItem &item);
void sendCloseError(const QString &message);

boost::signals2::signal<void(const WsControlPacket::Item&)> send;
Signal expired;
Expand All @@ -95,7 +100,6 @@ class WsSession : public QObject
void processPublishQueue();
void filtersFinished(const Filter::MessageFilter::Result &result);
void afterFilters(const PublishItem &item, Filter::SendAction sendAction, const QByteArray &content);
void sendCloseError(const QString &message);
void setupRequestTimer();

private slots:
Expand Down
Loading