Repository navigation
Description
Summary
Every accepted frontend connection pays three syscalls on the accept path that carry no information the worker does not already hold:
- 2 ×
getpeername(2):accept4(2)already returns the peer address, but the three listeners throw it away and the worker reads it back twice. - 1 ×
setsockopt(TCP_NODELAY): set on each accepted socket, although the kernel copiesTCP_NODELAYfrom the listening socket to every socket it accepts.
Sites (origin/main b5169d44f15e97661dcf20cfca6777d4272d9403)
getpeername, discarded address then two re-reads:
lib/src/http.rs:1577,lib/src/https.rs:1888,lib/src/tcp.rs:3026:.map(|(sock, _)| sock)drops the addressaccept4returned.lib/src/server.rs:3747:let peer = sock.peer_addr().ok();(first read, feeds the per-source accept counter and the accept-queue tuple, whose_peeris then dropped bycreate_sessions).lib/src/http.rs:148,lib/src/https.rs:227,lib/src/tcp.rs:183and:305: second read, seeds the session's cached peer address.
TCP_NODELAY per connection:
lib/src/http.rs:1726,lib/src/https.rs:2449,lib/src/tcp.rs:3086.
Measurement
Release build of b5169d44, one worker, intentrace -p on the worker, 20 requests with one connection per request, counted over the window from the first accept4 to the last close:
| syscall | H1 (per connection) | H2 over TLS (per connection) |
|---|---|---|
getpeername |
2.00 | 2.00 |
setsockopt |
2.00 (1 frontend + 1 backend) | 2.00 |
Inheritance check on Linux 7.2: a listener with TCP_NODELAY=1 yields accepted sockets with TCP_NODELAY=1; a control listener without it yields 0.
The kernels copy the flag at accept time in their own source:
- FreeBSD
sys/netinet/tcp_syncache.c:936-937(b98d169d1f910e0b28c7ea2f26fcc09f83614ec9):tp->t_flags = sototcpcb(lso)->t_flags & (TF_LRD|TF_NOPUSH|TF_NODELAY); - XNU (macOS)
bsd/netinet/tcp_input.c:2501(f6217f891ac0bb64f3d375211650a4c1ff8ca1ea):tp->t_flags |= otp->t_flags & (TF_NOPUSH | TF_NOOPT | TF_NODELAY); - OpenBSD
sys/netinet/tcp_input.c:3619(58ef3138855e39db066749e3b8e95b21b7471144):tp->t_flags = intotcpcb(listeninp)->t_flags & (TF_NOPUSH|TF_NODELAY); - NetBSD
sys/netinet/tcp_syncache.c:697(60f9ecc654fc8f9e8c857946c295d3ed57b2f8d9):tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
Proposed fix
ProxyConfiguration::acceptreturns the(TcpStream, SocketAddr)pair fromaccept4;Server::acceptfeeds that address to the per-source counter and the accept queue;create_sessionhands it to the session constructors instead of callingpeer_addr(). The PROXY-protocol paths keep their current semantics: the network address is the accept address, and the logical peer still comes from the PROXY header at upgrade. Only listeners cross SCM_RIGHTS at upgrade, never accepted sockets, so every session is built from an accepted socket.- Set
TCP_NODELAYonce in each listener'sactivate, on the resolved socket (freshly bound, inherited through SCM_RIGHTS, or parked after a failed registration), and drop the per-connection call. Setting it inserver_bindwould not do: an upgrade from an older binary hands over a listener that never had the flag. The tree has no systemd socket activation (git grep -n -i -E 'LISTEN_FDS|sd_listen'is empty).
Backend sockets keep their own TCP_NODELAY call; connect(2) sockets inherit nothing.
Expected gain: −2 getpeername and −1 setsockopt per accepted connection, on H1, H2 and TCP listeners.
Prior art: HAProxy stores the accept4 address as the connection source (src/sock.c:95, :125, sock_accept_conn) and sets its listener-scoped TCP options on the listener in tcp_bind_listener (src/proto_tcp.c:662, e.g. :694, :709, :806). It still sets TCP_NODELAY per accepted connection (src/session.c:296), under a FIXME/WTA note (:288-290) that these calls belong at the protocol level.
Activity
perf(server): reuse the accept(2) peer address and set TCP_NODELAY on…
perf(server): reuse the accept(2) peer address and set TCP_NODELAY on…
perf(server): reuse the accept(2) peer address and set TCP_NODELAY on…
perf(server): reuse the accept(2) peer address and set TCP_NODELAY on…