diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-06-29 08:24:18 +0200 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-06-29 08:24:26 +0200 |
commit | a000cb013cc1104ddcd6431d5a9b89e90bde2593 (patch) | |
tree | 7b38373088bb23150c11bc64d089fca52f5a210e | |
parent | e0face923533a1925a997228fc0c19117b19fd50 (diff) | |
parent | fa92e60f38cb109fe5a3c7acfe1017ffebc388cc (diff) | |
download | bitcoin-a000cb013cc1104ddcd6431d5a9b89e90bde2593.tar.xz |
Merge bitcoin/bitcoin#22361: refactor: Make httpserver work queue a unique_ptr
fa92e60f38cb109fe5a3c7acfe1017ffebc388cc refactor: Make httpserver work queue a unique_ptr (MarcoFalke)
Pull request description:
This simplifies the code a bit because `if (p) { delete p; p = nullptr; }` can be replaced by a call to the `reset()` member.
ACKs for top commit:
promag:
Core review ACK fa92e60f38cb109fe5a3c7acfe1017ffebc388cc.
jonatack:
ACK fa92e60f38cb109fe5a3c7acfe1017ffebc388cc code review, debug build clean, ran test/functional/interface*.py tests locally as a sanity check
hebasto:
ACK fa92e60f38cb109fe5a3c7acfe1017ffebc388cc, I have reviewed the code and it looks OK, I agree it can be merged.
Tree-SHA512: 6b122162317dd4ad6889341745c7ac1903a3ee510f6548f46dc356308442a6eff13eb8dc604c38ba18783e7a66d2b836d641a8594ff980a010c12c97f3856684
-rw-r--r-- | src/httpserver.cpp | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/httpserver.cpp b/src/httpserver.cpp index b3984a43bb..8741ad9b86 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -136,7 +136,7 @@ static struct evhttp* eventHTTP = nullptr; //! List of subnets to allow RPC connections from static std::vector<CSubNet> rpc_allow_subnets; //! Work queue for handling longer requests off the event loop thread -static WorkQueue<HTTPClosure>* workQueue = nullptr; +static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr}; //! Handlers for (sub)paths static std::vector<HTTPPathHandler> pathHandlers; //! Bound listening sockets @@ -256,10 +256,10 @@ static void http_request_cb(struct evhttp_request* req, void* arg) // Dispatch to worker thread if (i != iend) { std::unique_ptr<HTTPWorkItem> item(new HTTPWorkItem(std::move(hreq), path, i->handler)); - assert(workQueue); - if (workQueue->Enqueue(item.get())) + assert(g_work_queue); + if (g_work_queue->Enqueue(item.get())) { item.release(); /* if true, queue took ownership */ - else { + } else { LogPrintf("WARNING: request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting\n"); item->req->WriteReply(HTTP_SERVICE_UNAVAILABLE, "Work queue depth exceeded"); } @@ -392,7 +392,7 @@ bool InitHTTPServer() int workQueueDepth = std::max((long)gArgs.GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L); LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth); - workQueue = new WorkQueue<HTTPClosure>(workQueueDepth); + g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth); // transfer ownership to eventBase/HTTP via .release() eventBase = base_ctr.release(); eventHTTP = http_ctr.release(); @@ -424,7 +424,7 @@ void StartHTTPServer() g_thread_http = std::thread(ThreadHTTP, eventBase); for (int i = 0; i < rpcThreads; i++) { - g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i); + g_thread_http_workers.emplace_back(HTTPWorkQueueRun, g_work_queue.get(), i); } } @@ -435,16 +435,17 @@ void InterruptHTTPServer() // Reject requests on current connections evhttp_set_gencb(eventHTTP, http_reject_request_cb, nullptr); } - if (workQueue) - workQueue->Interrupt(); + if (g_work_queue) { + g_work_queue->Interrupt(); + } } void StopHTTPServer() { LogPrint(BCLog::HTTP, "Stopping HTTP server\n"); - if (workQueue) { + if (g_work_queue) { LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n"); - for (auto& thread: g_thread_http_workers) { + for (auto& thread : g_thread_http_workers) { thread.join(); } g_thread_http_workers.clear(); @@ -467,10 +468,7 @@ void StopHTTPServer() event_base_free(eventBase); eventBase = nullptr; } - if (workQueue) { - delete workQueue; - workQueue = nullptr; - } + g_work_queue.reset(); LogPrint(BCLog::HTTP, "Stopped HTTP server\n"); } |