@@ -136,7 +136,7 @@ static struct evhttp* eventHTTP = nullptr;
136
136
// ! List of subnets to allow RPC connections from
137
137
static std::vector<CSubNet> rpc_allow_subnets;
138
138
// ! Work queue for handling longer requests off the event loop thread
139
- static WorkQueue<HTTPClosure>* workQueue = nullptr ;
139
+ static std::unique_ptr< WorkQueue<HTTPClosure>> g_work_queue{ nullptr } ;
140
140
// ! Handlers for (sub)paths
141
141
static std::vector<HTTPPathHandler> pathHandlers;
142
142
// ! Bound listening sockets
@@ -256,10 +256,10 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
256
256
// Dispatch to worker thread
257
257
if (i != iend) {
258
258
std::unique_ptr<HTTPWorkItem> item (new HTTPWorkItem (std::move (hreq), path, i->handler ));
259
- assert (workQueue );
260
- if (workQueue ->Enqueue (item.get ()))
259
+ assert (g_work_queue );
260
+ if (g_work_queue ->Enqueue (item.get ())) {
261
261
item.release (); /* if true, queue took ownership */
262
- else {
262
+ } else {
263
263
LogPrintf (" WARNING: request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting\n " );
264
264
item->req ->WriteReply (HTTP_SERVICE_UNAVAILABLE, " Work queue depth exceeded" );
265
265
}
@@ -392,7 +392,7 @@ bool InitHTTPServer()
392
392
int workQueueDepth = std::max ((long )gArgs .GetArg (" -rpcworkqueue" , DEFAULT_HTTP_WORKQUEUE), 1L );
393
393
LogPrintf (" HTTP: creating work queue of depth %d\n " , workQueueDepth);
394
394
395
- workQueue = new WorkQueue<HTTPClosure>(workQueueDepth);
395
+ g_work_queue = std::make_unique< WorkQueue<HTTPClosure> >(workQueueDepth);
396
396
// transfer ownership to eventBase/HTTP via .release()
397
397
eventBase = base_ctr.release ();
398
398
eventHTTP = http_ctr.release ();
@@ -424,7 +424,7 @@ void StartHTTPServer()
424
424
g_thread_http = std::thread (ThreadHTTP, eventBase);
425
425
426
426
for (int i = 0 ; i < rpcThreads; i++) {
427
- g_thread_http_workers.emplace_back (HTTPWorkQueueRun, workQueue , i);
427
+ g_thread_http_workers.emplace_back (HTTPWorkQueueRun, g_work_queue. get () , i);
428
428
}
429
429
}
430
430
@@ -435,16 +435,17 @@ void InterruptHTTPServer()
435
435
// Reject requests on current connections
436
436
evhttp_set_gencb (eventHTTP, http_reject_request_cb, nullptr );
437
437
}
438
- if (workQueue)
439
- workQueue->Interrupt ();
438
+ if (g_work_queue) {
439
+ g_work_queue->Interrupt ();
440
+ }
440
441
}
441
442
442
443
void StopHTTPServer ()
443
444
{
444
445
LogPrint (BCLog::HTTP, " Stopping HTTP server\n " );
445
- if (workQueue ) {
446
+ if (g_work_queue ) {
446
447
LogPrint (BCLog::HTTP, " Waiting for HTTP worker threads to exit\n " );
447
- for (auto & thread: g_thread_http_workers) {
448
+ for (auto & thread : g_thread_http_workers) {
448
449
thread.join ();
449
450
}
450
451
g_thread_http_workers.clear ();
@@ -467,10 +468,7 @@ void StopHTTPServer()
467
468
event_base_free (eventBase);
468
469
eventBase = nullptr ;
469
470
}
470
- if (workQueue) {
471
- delete workQueue;
472
- workQueue = nullptr ;
473
- }
471
+ g_work_queue.reset ();
474
472
LogPrint (BCLog::HTTP, " Stopped HTTP server\n " );
475
473
}
476
474
0 commit comments