tcp-listen: add SO_REUSEPORT so N workers can bind same port

Lets bend (examples/cuda-fanout/gpu-worker.lsp) run multiple worker
processes behind a single listening port. Each worker calls
tcp-listen on the same port; the kernel distributes incoming
connections across the bound sockets.

Foxhop production use case: 2 bend workers per GPU host (3090 + 4090)
to consume the .ready queue at 2x throughput without an external
load balancer.
This commit is contained in:
russell@unturf.com 2026-06-09 13:51:03 -04:00
parent b4732371a0
commit 29fcdcf367
No known key found for this signature in database

View file

@ -1224,6 +1224,12 @@ static Value bi_tcp_listen(Value *a, int n, Env *e) {
if (fd < 0) return VAL_FALSE;
int one = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
/* SO_REUSEPORT lets N processes bind() the same port. Kernel hashes
incoming connections across the listening sockets so each accept()
returns a fresh client to whichever process is ready. Used by bend
to run multiple workers behind one port without an external
distributor. */
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);