-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathhandlerapp.cpp
More file actions
409 lines (354 loc) · 14.1 KB
/
handlerapp.cpp
File metadata and controls
409 lines (354 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (C) 2015-2022 Fanout, Inc.
* Copyright (C) 2024-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 <assert.h>
#include <iostream>
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QStringList>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include "rust/bindings.h"
#include "timer.h"
#include "defercall.h"
#include "eventloop.h"
#include "processquit.h"
#include "log.h"
#include "simplehttpserver.h"
#include "httpsession.h"
#include "wssession.h"
#include "httpsessionupdatemanager.h"
#include "settings.h"
#include "config.h"
#include "handlerengine.h"
#include "handlerargsdata.h"
#define DEFAULT_HTTP_MAX_HEADERS_SIZE 10000
#define DEFAULT_HTTP_MAX_BODY_SIZE 1000000
static void trimlist(QStringList *list)
{
for(int n = 0; n < list->count(); ++n)
{
if((*list)[n].isEmpty())
{
list->removeAt(n);
--n; // adjust position
}
}
}
static QStringList expandSpecs(const QStringList &l, int peerCount)
{
if(l.count() == 1 && l[0].startsWith("ipc:") && peerCount > 1)
{
QString base = l[0];
QStringList out;
for(int i = 0; i < peerCount; ++i)
out += base + QString("-%1").arg(i);
return out;
}
return l;
}
static QString firstSpec(const QString &s, int peerCount)
{
if(s.startsWith("ipc:") && peerCount > 1)
return s + "-0";
return s;
}
class HandlerApp
{
public:
HandlerApp();
~HandlerApp();
int run(const ffi::HandlerCliArgs *argsFfi);
private:
class Private;
};
class HandlerApp::Private
{
public:
static int run(const ffi::HandlerCliArgs *argsFfi)
{
QCoreApplication::setApplicationName("pushpin-handler");
QCoreApplication::setApplicationVersion(Config::get().version);
HandlerArgsData args(argsFfi);
Settings settings(args.configFile);
if (!args.ipcPrefix.isEmpty()) settings.setIpcPrefix(args.ipcPrefix);
if (args.portOffset != -1) settings.setPortOffset(args.portOffset);
// Set the log level
if(args.logLevel != -1)
log_setOutputLevel(args.logLevel);
else
log_setOutputLevel(LOG_LEVEL_INFO);
// Set the log file if specified
if(!args.logFile.isEmpty())
{
if(!log_setFile(args.logFile))
{
log_error("failed to open log file: %s", qPrintable(args.logFile));
return 1;
}
}
log_debug("starting...");
// QSettings doesn't inform us if the config file can't be opened, so do that ourselves
{
QFile file(args.configFile);
if(!file.open(QIODevice::ReadOnly))
{
log_error("failed to open %s", qPrintable(args.configFile));
return 1;
}
}
QStringList services = settings.value("runner/services").toStringList();
QStringList connmgr_in_stream_specs = settings.value("proxy/connmgr_in_stream_specs").toStringList();
trimlist(&connmgr_in_stream_specs);
QStringList connmgr_out_specs = settings.value("proxy/connmgr_out_specs").toStringList();
trimlist(&connmgr_out_specs);
QStringList condure_in_stream_specs = settings.value("proxy/condure_in_stream_specs").toStringList();
trimlist(&condure_in_stream_specs);
connmgr_in_stream_specs += condure_in_stream_specs;
QStringList condure_out_specs = settings.value("proxy/condure_out_specs").toStringList();
trimlist(&condure_out_specs);
connmgr_out_specs += condure_out_specs;
int proxyWorkerCount = settings.value("proxy/workers", 1).toInt();
QStringList m2a_in_stream_specs = settings.value("handler/m2a_in_stream_specs").toStringList();
trimlist(&m2a_in_stream_specs);
QStringList m2a_out_specs = settings.value("handler/m2a_out_specs").toStringList();
trimlist(&m2a_out_specs);
QStringList intreq_out_specs = settings.value("handler/proxy_intreq_out_specs").toStringList();
trimlist(&intreq_out_specs);
QStringList intreq_out_stream_specs = settings.value("handler/proxy_intreq_out_stream_specs").toStringList();
trimlist(&intreq_out_stream_specs);
QStringList intreq_in_specs = settings.value("handler/proxy_intreq_in_specs").toStringList();
trimlist(&intreq_in_specs);
QStringList proxy_inspect_specs = settings.value("handler/proxy_inspect_specs").toStringList();
trimlist(&proxy_inspect_specs);
QString proxy_inspect_spec = settings.value("handler/proxy_inspect_spec").toString();
if(!proxy_inspect_spec.isEmpty())
proxy_inspect_specs += proxy_inspect_spec;
QStringList proxy_accept_specs = settings.value("handler/proxy_accept_specs").toStringList();
trimlist(&proxy_accept_specs);
QString proxy_accept_spec = settings.value("handler/proxy_accept_spec").toString();
if(!proxy_accept_spec.isEmpty())
proxy_accept_specs += proxy_accept_spec;
QStringList proxy_retry_out_specs = settings.value("handler/proxy_retry_out_specs").toStringList();
trimlist(&proxy_retry_out_specs);
QString proxy_retry_out_spec = settings.value("handler/proxy_retry_out_spec").toString();
if(!proxy_retry_out_spec.isEmpty())
proxy_retry_out_specs += proxy_retry_out_spec;
QStringList ws_control_init_specs = settings.value("handler/proxy_ws_control_init_specs").toStringList();
trimlist(&ws_control_init_specs);
QStringList ws_control_stream_specs = settings.value("handler/proxy_ws_control_stream_specs").toStringList();
trimlist(&ws_control_stream_specs);
QString stats_spec = settings.value("handler/stats_spec").toString();
QString command_spec = settings.value("handler/command_spec").toString();
QString state_spec = settings.value("handler/state_spec").toString();
QStringList proxy_stats_specs = settings.value("handler/proxy_stats_specs").toStringList();
trimlist(&proxy_stats_specs);
QString proxy_stats_spec = settings.value("handler/proxy_stats_spec").toString();
if(!proxy_stats_spec.isEmpty())
proxy_stats_specs += proxy_stats_spec;
QString proxy_command_spec = settings.value("handler/proxy_command_spec").toString();
QString push_in_spec = settings.value("handler/push_in_spec").toString();
QStringList push_in_sub_specs = settings.value("handler/push_in_sub_specs").toStringList();
trimlist(&push_in_sub_specs);
QString push_in_sub_spec = settings.value("handler/push_in_sub_spec").toString();
if(!push_in_sub_spec.isEmpty())
push_in_sub_specs += push_in_sub_spec;
bool push_in_sub_connect = settings.value("handler/push_in_sub_connect").toBool();
QString push_in_http_addr = settings.value("handler/push_in_http_addr").toString();
int push_in_http_port = settings.adjustedPort("handler/push_in_http_port");
int push_in_http_max_headers_size = settings.value("handler/push_in_http_max_headers_size", DEFAULT_HTTP_MAX_HEADERS_SIZE).toInt();
int push_in_http_max_body_size = settings.value("handler/push_in_http_max_body_size", DEFAULT_HTTP_MAX_BODY_SIZE).toInt();
bool ok;
int ipcFileMode = settings.value("handler/ipc_file_mode", -1).toString().toInt(&ok, 8);
bool shareAll = settings.value("handler/share_all").toBool();
int messageRate = settings.value("handler/message_rate", -1).toInt();
int messageHwm = settings.value("handler/message_hwm", -1).toInt();
int messageBlockSize = settings.value("handler/message_block_size", -1).toInt();
int messageWait = settings.value("handler/message_wait", 5000).toInt();
int idCacheTtl = settings.value("handler/id_cache_ttl", 0).toInt();
bool updateOnFirstSubscription = settings.value("handler/update_on_first_subscription", true).toBool();
int clientMaxconn = settings.value("runner/client_maxconn", 50000).toInt();
int connectionSubscriptionMax = settings.value("handler/connection_subscription_max", 20).toInt();
int subscriptionLinger = settings.value("handler/subscription_linger", 60).toInt();
int statsConnectionSend = settings.value("global/stats_connection_send", true).toBool();
int statsConnectionTtl = settings.value("global/stats_connection_ttl", 120).toInt();
int statsSubscriptionTtl = settings.value("handler/stats_subscription_ttl", 60).toInt();
int statsReportInterval = settings.value("handler/stats_report_interval", 10).toInt();
QString statsFormat = settings.value("handler/stats_format").toString();
QString prometheusPort = settings.value("handler/prometheus_port").toString();
QString prometheusPrefix = settings.value("handler/prometheus_prefix").toString();
if(m2a_in_stream_specs.isEmpty() || m2a_out_specs.isEmpty())
{
log_error("must set m2a_in_stream_specs and m2a_out_specs");
return 1;
}
if(proxy_inspect_specs.isEmpty() || proxy_accept_specs.isEmpty() || proxy_retry_out_specs.isEmpty())
{
log_error("must set proxy_inspect_specs, proxy_accept_specs, and proxy_retry_out_specs");
return 1;
}
HandlerEngine::Configuration config;
config.appVersion = Config::get().version;
config.instanceId = "handler_" + QByteArray::number(QCoreApplication::applicationPid());
if(!services.contains("mongrel2") && (!connmgr_in_stream_specs.isEmpty() || !connmgr_out_specs.isEmpty()))
{
config.serverInStreamSpecs = connmgr_in_stream_specs;
config.serverOutSpecs = connmgr_out_specs;
}
else
{
config.serverInStreamSpecs = m2a_in_stream_specs;
config.serverOutSpecs = m2a_out_specs;
}
config.clientOutSpecs = expandSpecs(intreq_out_specs, proxyWorkerCount);
config.clientOutStreamSpecs = expandSpecs(intreq_out_stream_specs, proxyWorkerCount);
config.clientInSpecs = expandSpecs(intreq_in_specs, proxyWorkerCount);
config.inspectSpecs = expandSpecs(proxy_inspect_specs, proxyWorkerCount);
config.acceptSpecs = expandSpecs(proxy_accept_specs, proxyWorkerCount);
config.retryOutSpecs = expandSpecs(proxy_retry_out_specs, proxyWorkerCount);
config.wsControlInitSpecs = expandSpecs(ws_control_init_specs, proxyWorkerCount);
config.wsControlStreamSpecs = expandSpecs(ws_control_stream_specs, proxyWorkerCount);
config.statsSpec = stats_spec;
config.commandSpec = command_spec;
config.stateSpec = state_spec;
config.proxyStatsSpecs = expandSpecs(proxy_stats_specs, proxyWorkerCount);
config.proxyCommandSpec = firstSpec(proxy_command_spec, proxyWorkerCount);
config.pushInSpec = push_in_spec;
config.pushInSubSpecs = push_in_sub_specs;
config.pushInSubConnect = push_in_sub_connect;
config.pushInHttpAddr = QHostAddress(push_in_http_addr);
config.pushInHttpPort = push_in_http_port;
config.pushInHttpMaxHeadersSize = push_in_http_max_headers_size;
config.pushInHttpMaxBodySize = push_in_http_max_body_size;
config.ipcFileMode = ipcFileMode;
config.shareAll = shareAll;
config.messageRate = messageRate;
config.messageHwm = messageHwm;
config.messageBlockSize = messageBlockSize;
config.messageWait = messageWait;
config.idCacheTtl = idCacheTtl;
config.updateOnFirstSubscription = updateOnFirstSubscription;
config.connectionsMax = clientMaxconn;
config.connectionSubscriptionMax = connectionSubscriptionMax;
config.subscriptionLinger = subscriptionLinger;
config.statsConnectionSend = statsConnectionSend;
config.statsConnectionTtl = statsConnectionTtl;
config.statsSubscriptionTtl = statsSubscriptionTtl;
config.statsReportInterval = statsReportInterval;
config.statsFormat = statsFormat;
config.prometheusPort = prometheusPort;
config.prometheusPrefix = prometheusPrefix;
return runLoop(config, true);
}
private:
static int runLoop(const HandlerEngine::Configuration &config, bool newEventLoop)
{
// includes worst-case subscriptions and update registrations
int timersPerSession = qMax(TIMERS_PER_HTTPSESSION, TIMERS_PER_WSSESSION) +
(config.connectionSubscriptionMax * TIMERS_PER_SUBSCRIPTION) +
TIMERS_PER_UNIQUE_UPDATE_REGISTRATION;
// enough timers for sessions, plus an extra 100 for misc
int timersMax = (config.connectionsMax * timersPerSession) + 100;
std::unique_ptr<EventLoop> loop;
if(newEventLoop)
{
log_debug("using new event loop");
// enough for control requests and prometheus requests, plus an
// extra 100 for misc. client sessions don't use socket notifiers
int socketNotifiersMax = (SOCKETNOTIFIERS_PER_SIMPLEHTTPREQUEST * (CONTROL_CONNECTIONS_MAX + PROMETHEUS_CONNECTIONS_MAX)) + 100;
int registrationsMax = timersMax + socketNotifiersMax;
loop = std::make_unique<EventLoop>(registrationsMax);
}
else
{
// for qt event loop, timer subsystem must be explicitly initialized
Timer::init(timersMax);
}
std::unique_ptr<HandlerEngine> engine;
DeferCall deferCall;
deferCall.defer([&] {
engine = std::make_unique<HandlerEngine>();
ProcessQuit::instance()->quit.connect([&] {
log_info("stopping...");
// remove the handler, so if we get another signal then we crash out
ProcessQuit::cleanup();
engine.reset();
log_debug("stopped");
if(newEventLoop)
loop->exit(0);
else
QCoreApplication::exit(0);
});
ProcessQuit::instance()->hup.connect([&] {
log_info("reloading");
log_rotate();
engine->reload();
});
if(!engine->start(config))
{
engine.reset();
if(newEventLoop)
loop->exit(1);
else
QCoreApplication::exit(1);
return;
}
log_info("started");
});
int ret;
if(newEventLoop)
ret = loop->exec();
else
ret = QCoreApplication::exec();
if(!newEventLoop)
{
// ensure deferred deletes are processed
QCoreApplication::instance()->sendPostedEvents();
}
// deinit here, after all event loop activity has completed
if(!newEventLoop)
{
DeferCall::cleanup();
Timer::deinit();
}
return ret;
}
};
HandlerApp::HandlerApp() = default;
HandlerApp::~HandlerApp() = default;
int HandlerApp::run(const ffi::HandlerCliArgs *argsFfi)
{
return Private::run(argsFfi);
}
extern "C" {
int handler_init(const ffi::HandlerCliArgs *argsFfi)
{
// Create dummy argc/argv for QCoreApplication
int argc = 1;
char app_name[] = "pushpin-handler";
char* argv[] = { app_name, nullptr };
QCoreApplication qapp(argc, argv);
HandlerApp app;
return app.run(argsFfi);
}
}