What does event_base_dispatch do? Source code (src/httpserver.cpp):

static void ThreadHTTP(struct event_base* base, struct evhttp* http)
{
    RenameThread("bitcoin-http");
    LogPrint("http", "Entering http event loop\n");
    event_base_dispatch(base);
    // Event loop will be interrupted by InterruptHTTPServer()
    LogPrint("http", "Exited http event loop\n");
}


Can anyone help me understand this portion of logic? In particular 


What is the HTTP magic happening here event_base_dispatch(base)?
Why are we renaming threads?
event_base_dispatch is part of libevent, a library that takes events from network sockets, and uses them to call callbacks in your program. To find out what it's calling, you should look at the struct event_base* base variable.

bool InitHTTPServer()
{
    // [...]
    base = event_base_new(); // XXX RAII
    // [...]
    http = evhttp_new(base); // XXX RAII
    // [...]
    evhttp_set_gencb(http, http_request_cb, NULL);


There are three important calls here. event_base_new creates the struct that tells event_base_dispatch how to handle incoming connections. evhttp_new tells libevent to interpret incoming data as the HTTP protocol, instead of passing along raw bytes. evhttp_set_gencb tells libevent which function to call when new requests come in. This is where that ends:

/** HTTP request callback */
static void http_request_cb(struct evhttp_request* req, void* arg)



  Why are we renaming threads?


Debuggging and performance logging.