From 4d030871c004c917458b5e1c296edcfc689a2e66 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 31 Oct 2026 15:59:41 +0100 Subject: Make tcp worker multithreaded, too This shamelessly steals the model established in the UDP workers, i.e. if the kernel is able to bind multiple listening sockets to the same endpoint, we will share them across threads. If it can't, all threads will accept on the same socket. The fast path handles most of the connections, only the rare case of no data yet on the socket is offloaded to the slow path (which used to be the mainloop) for completion. This scales nicely, as the higher the load is, the higher chances are that an accept happens only after the network roundtrip to the peer completes, leading to more connections that already have data waiting for us. We also consolidate the workstruct init and deinit code, preparation for later unifying all thread state structs for all protocols (mca, udc and tcp). The config file gains a per tcp connection worker count and some docs explaining what this knob buys and costs, nudging the user to either keep it 0 or dial it up to at least 2. --- ot_http.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'ot_http.c') diff --git a/ot_http.c b/ot_http.c index bb6ff24..602ff94 100644 --- a/ot_http.c +++ b/ot_http.c @@ -39,22 +39,30 @@ ssize_t g_stats_path_len; enum { SUCCESS_HTTP_HEADER_LENGTH = 80, SUCCESS_HTTP_SIZE_OFF = 17 }; -int http_adopt(const int64_t sock, struct http_data *cookie) { - struct http_data *heap = malloc(sizeof(*heap)); +struct http_data *http_adopt(const int64_t sock) { + struct http_data *cookie = fdstate_cookie(sock), *heap; + int interest; - if (!heap) - return -1; + if (!cookie || !(cookie->flag & STRUCT_HTTP_FLAG_ONSTACK)) + return cookie; + + if (!(heap = malloc(sizeof(*heap)))) + return NULL; *heap = *cookie; heap->flag &= ~STRUCT_HTTP_FLAG_ONSTACK; + + /* Publishing the cookie is what lets another thread reach this connection, + so nothing may touch it afterwards, not even to read what it asked for */ + interest = heap->interest; fdstate_set_cookie((int)sock, heap); - if (event_add((int)sock, heap->interest, fdstate_generation((int)sock)) == -1) { + if (event_add((int)sock, interest, fdstate_generation((int)sock)) == -1) { fdstate_set_cookie((int)sock, cookie); free(heap); - return -1; + return NULL; } - return 0; + return heap; } /* Give up a connection: the one place that knows everything hanging off it. */ @@ -348,13 +356,17 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c } #endif #endif - /* Pass this task to the worker thread */ cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; /* Clients waiting for us should not easily timeout */ fdstate_set_expiry(sock, 0); - fullscrape_deliver(sock, format); http_interest_set(sock, cookie, 0); + + /* Everything this connection needs has been written, so it can be given + a home of its own and only then be made reachable by a worker thread */ + if (!http_adopt(sock)) + HTTPERROR_500; + fullscrape_deliver(sock, format); return ws->reply_size = -2; } #endif @@ -366,6 +378,9 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c /* Complex stats also include expensive memory debugging tools */ fdstate_set_expiry(sock, 0); + + if (!http_adopt(sock)) + HTTPERROR_500; stats_deliver(sock, mode); return ws->reply_size = -2; } @@ -448,8 +463,11 @@ static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct * cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; /* Clients waiting for us should not easily timeout */ fdstate_set_expiry(sock, 0); - fullscrape_deliver(sock, format); http_interest_set(sock, cookie, 0); + + if (!http_adopt(sock)) + HTTPERROR_500; + fullscrape_deliver(sock, format); return ws->reply_size = -2; } #endif -- cgit v1.3