commit 98c4ff7492a724a00e9cfa843e43909e8220865e89d6ebc04638c64020bd1237 from: Stefan Sperling date: Sun Sep 7 09:34:35 2025 UTC dispatch new requests to the least-busy gotwebd worker Instead of distributing requests across workers in a round-robin fashion keep track of how many requests a given worker has queued and send new requests to the worker which has the least amount of work queued for it. This makes gotwebd much more responsive than stupid round-robin, even though the new strategy is still not optimal because different types of requests carry different cost. E.g. blaming a file is usually heavier than listing trees. But it might be good enough, for now. Let's try and see. ok op@ commit - 330d8550d25bc257f9f672826148a99562676753c7d741e98efdd827f4569f0c commit + 98c4ff7492a724a00e9cfa843e43909e8220865e89d6ebc04638c64020bd1237 blob - 3fbd61d2e922808cc96e430a73af19ace07a1f99abc4d9aaa10a7580c6a13992 blob + 7c90faa7369b761d27e5dd7d94cce7e8dc4bd03057c2dfadec7c74c618f59779 --- gotwebd/gotwebd.c +++ gotwebd/gotwebd.c @@ -500,6 +500,13 @@ main(int argc, char **argv) www_username = env->www_user; } + if (proc_type == GOTWEBD_PROC_SERVER) { + env->worker_load = calloc(env->prefork, + sizeof(env->worker_load[0])); + if (env->worker_load == NULL) + fatal("calloc"); + } + pw = getpwnam(www_username); if (pw == NULL) fatalx("unknown user %s", www_username); blob - 1b15f0f306b27b0c70add3bb3f7e900110dcf2221acb7a5c729749a203965f95 blob + f43c646f8a0496302fda217081e68adeff8c3192a0974e05f887a6cca3418941 --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -273,6 +273,7 @@ struct request { struct event *resp_event; int sock_id; uint32_t request_id; + int worker_idx; uint8_t *buf; size_t buf_len; @@ -391,6 +392,7 @@ struct gotwebd { uint16_t prefork; int gotweb_pending; int gotweb_cur; + int *worker_load; int server_cnt; blob - 11f705535cad6a65999703b221b4109cdc514c960c34742150e955b486aaff8c blob + 3db3a7683b3faf044310441013b91a5f22634125c39c53195be0d5a5aae82acb --- gotwebd/sockets.c +++ gotwebd/sockets.c @@ -134,8 +134,16 @@ find_request(uint32_t request_id) static void cleanup_request(struct request *c) { + struct gotwebd *env = gotwebd_env; + cgi_inflight--; + if (c->worker_idx != -1) { + if (env->worker_load[c->worker_idx] <= 0) + fatalx("request in flight on worker with zero load"); + env->worker_load[c->worker_idx]--; + } + del_request(c); event_add(&c->sock->ev, NULL); @@ -468,6 +476,29 @@ recv_gotweb_pipe(struct gotwebd *env, struct imsg *ims env->gotweb_pending--; } +static struct imsgev * +select_worker(struct request *c) +{ + struct gotwebd *env = gotwebd_env; + int i, least_busy_worker_idx, min_load; + + min_load = env->worker_load[0]; + least_busy_worker_idx = 0; + for (i = 1; i < env->prefork; i++) { + if (env->worker_load[i] > min_load) + continue; + + min_load = env->worker_load[i]; + least_busy_worker_idx = i; + } + + log_debug("dispatching request %u to gotweb %d", + c->request_id, least_busy_worker_idx); + + c->worker_idx = least_busy_worker_idx; + return &env->iev_gotweb[least_busy_worker_idx]; +} + static int process_request(struct request *c) { @@ -491,19 +522,18 @@ process_request(struct request *c) ic.priv_fd[i] = -1; ic.fd = -1; - /* Round-robin requests across gotweb processes. */ - iev_gotweb = &env->iev_gotweb[env->gotweb_cur]; - env->gotweb_cur = (env->gotweb_cur + 1) % env->prefork; - + iev_gotweb = select_worker(c); ret = imsg_compose_event(iev_gotweb, GOTWEBD_IMSG_REQ_PROCESS, GOTWEBD_PROC_SERVER, -1, c->fd, &ic, sizeof(ic)); if (ret == -1) { log_warn("imsg_compose_event"); + c->worker_idx = -1; return -1; } c->fd = -1; c->client_status = CLIENT_REQUEST; + env->worker_load[c->worker_idx]++; return 0; } @@ -782,7 +812,7 @@ sockets_shutdown(void) for (i = 0; i < gotwebd_env->prefork; i++) imsgbuf_clear(&gotwebd_env->iev_gotweb[i].ibuf); free(gotwebd_env->iev_gotweb); - + free(gotwebd_env->worker_load); free(gotwebd_env); exit(0); @@ -1165,6 +1195,7 @@ sockets_socket_accept(int fd, short event, void *arg) c->buf_len = 0; c->client_status = CLIENT_CONNECT; c->request_id = get_request_id(); + c->worker_idx = -1; event_set(&c->ev, s, EV_READ, read_fcgi_records, c); event_add(&c->ev, NULL);