Changeset View
Changeset View
Standalone View
Standalone View
src/httpserver.cpp
Show All 10 Lines | |||||
#include <logging.h> | #include <logging.h> | ||||
#include <netbase.h> | #include <netbase.h> | ||||
#include <rpc/protocol.h> // For HTTP status codes | #include <rpc/protocol.h> // For HTTP status codes | ||||
#include <shutdown.h> | #include <shutdown.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <ui_interface.h> | #include <ui_interface.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/threadnames.h> | |||||
#include <event2/buffer.h> | #include <event2/buffer.h> | ||||
#include <event2/bufferevent.h> | #include <event2/bufferevent.h> | ||||
#include <event2/keyvalq_struct.h> | #include <event2/keyvalq_struct.h> | ||||
#include <event2/thread.h> | #include <event2/thread.h> | ||||
#include <event2/util.h> | #include <event2/util.h> | ||||
#include <support/events.h> | #include <support/events.h> | ||||
▲ Show 20 Lines • Show All 261 Lines • ▼ Show 20 Lines | |||||
/** Callback to reject HTTP requests after shutdown. */ | /** Callback to reject HTTP requests after shutdown. */ | ||||
static void http_reject_request_cb(struct evhttp_request *req, void *) { | static void http_reject_request_cb(struct evhttp_request *req, void *) { | ||||
LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n"); | LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n"); | ||||
evhttp_send_error(req, HTTP_SERVUNAVAIL, nullptr); | evhttp_send_error(req, HTTP_SERVUNAVAIL, nullptr); | ||||
} | } | ||||
/** Event dispatcher thread */ | /** Event dispatcher thread */ | ||||
static bool ThreadHTTP(struct event_base *base) { | static bool ThreadHTTP(struct event_base *base) { | ||||
RenameThread("bitcoin-http"); | util::ThreadRename("http"); | ||||
LogPrint(BCLog::HTTP, "Entering http event loop\n"); | LogPrint(BCLog::HTTP, "Entering http event loop\n"); | ||||
event_base_dispatch(base); | event_base_dispatch(base); | ||||
// Event loop will be interrupted by InterruptHTTPServer() | // Event loop will be interrupted by InterruptHTTPServer() | ||||
LogPrint(BCLog::HTTP, "Exited http event loop\n"); | LogPrint(BCLog::HTTP, "Exited http event loop\n"); | ||||
return event_base_got_break(base) == 0; | return event_base_got_break(base) == 0; | ||||
} | } | ||||
/** Bind HTTP server to specified addresses */ | /** Bind HTTP server to specified addresses */ | ||||
Show All 39 Lines | for (std::vector<std::pair<std::string, uint16_t>>::iterator i = | ||||
LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, | LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, | ||||
i->second); | i->second); | ||||
} | } | ||||
} | } | ||||
return !boundSockets.empty(); | return !boundSockets.empty(); | ||||
} | } | ||||
/** Simple wrapper to set thread name and run work queue */ | /** Simple wrapper to set thread name and run work queue */ | ||||
static void HTTPWorkQueueRun(WorkQueue<HTTPClosure> *queue) { | static void HTTPWorkQueueRun(WorkQueue<HTTPClosure> *queue, int worker_num) { | ||||
RenameThread("bitcoin-httpworker"); | util::ThreadRename(strprintf("httpworker.%i", worker_num)); | ||||
queue->Run(); | queue->Run(); | ||||
} | } | ||||
/** libevent event log callback */ | /** libevent event log callback */ | ||||
static void libevent_log_cb(int severity, const char *msg) { | static void libevent_log_cb(int severity, const char *msg) { | ||||
#ifndef EVENT_LOG_WARN | #ifndef EVENT_LOG_WARN | ||||
// EVENT_LOG_WARN was added in 2.0.19; but before then _EVENT_LOG_WARN existed. | // EVENT_LOG_WARN was added in 2.0.19; but before then _EVENT_LOG_WARN existed. | ||||
#define EVENT_LOG_WARN _EVENT_LOG_WARN | #define EVENT_LOG_WARN _EVENT_LOG_WARN | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | |||||
void StartHTTPServer() { | void StartHTTPServer() { | ||||
LogPrint(BCLog::HTTP, "Starting HTTP server\n"); | LogPrint(BCLog::HTTP, "Starting HTTP server\n"); | ||||
int rpcThreads = | int rpcThreads = | ||||
std::max((long)gArgs.GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L); | std::max((long)gArgs.GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L); | ||||
LogPrintf("HTTP: starting %d worker threads\n", rpcThreads); | LogPrintf("HTTP: starting %d worker threads\n", rpcThreads); | ||||
threadHTTP = std::thread(ThreadHTTP, eventBase); | threadHTTP = std::thread(ThreadHTTP, eventBase); | ||||
for (int i = 0; i < rpcThreads; i++) { | for (int i = 0; i < rpcThreads; i++) { | ||||
g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue); | g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i); | ||||
} | } | ||||
} | } | ||||
void InterruptHTTPServer() { | void InterruptHTTPServer() { | ||||
LogPrint(BCLog::HTTP, "Interrupting HTTP server\n"); | LogPrint(BCLog::HTTP, "Interrupting HTTP server\n"); | ||||
if (eventHTTP) { | if (eventHTTP) { | ||||
// Reject requests on current connections | // Reject requests on current connections | ||||
evhttp_set_gencb(eventHTTP, http_reject_request_cb, nullptr); | evhttp_set_gencb(eventHTTP, http_reject_request_cb, nullptr); | ||||
▲ Show 20 Lines • Show All 215 Lines • Show Last 20 Lines |