Repository navigation
Description
Observation
Closing an HTTP/2 frontend connection over TLS costs two writev(2) calls back to back: one for the final GOAWAY, one for the TLS close_notify.
Release build of b19d94f8, one worker, python http.server backend, 20 sequential curl --http2 requests each on its own TLS connection, LOAD1 < 8, fdtrace (LD_PRELOAD) and intentrace -p:
| per connection | fdtrace | intentrace |
|---|---|---|
writev |
6.90 | 6.00 |
epoll_wait |
8.00 | 5.00 |
| total | 33.80 | 42.10 |
The close of every connection whose client sent a GOAWAY ends with:
recv fd=16 ret=48
writev fd=16 ret=39 len=39 <- GOAWAY (17-byte frame in one TLS 1.3 record)
writev fd=16 ret=24 len=24 <- close_notify alert
shutdown / close
No epoll_wait separates the two writes: they belong to the same pass.
Where the two writes come from
- The GOAWAY:
ConnectionH2::goawayserialises it intozero(lib/src/protocol/mux/h2.rs), and the nextH2Shell::writableflushes it throughflush_zero_to_socket→FrontRustls::socket_write, which hands the plaintext to rustls and immediately runswrite_tls(lib/src/socket.rs). - The
close_notify: theH2State::GoAwayarm then disconnects, andH2Shell::closecallsdrain_tls_close_notify(lib/src/protocol/mux/shared.rs), i.e.send_close_notifyand a secondwrite_tls.
rustls's write_tls sends every queued record in one write_vectored (rustls-0.23.45/src/vecbuf.rs, ChunkVecBuffer::write_to), so queueing the alert after the GOAWAY plaintext and before the flush would put both records in one writev.
Proposal
When the zero buffer being flushed ends in the final GOAWAY of a server connection that closes right after it, hand rustls the GOAWAY, queue close_notify behind it, and flush once. The close_notify stays mandatory (RFC 8446 §6.1) and still follows the GOAWAY on the wire; only the number of writes changes. Expected: -1 writev per H2 TLS connection closed this way.
HAProxy does not merge them: h2_release (src/mux_h2.c) flushes the mux buffer and then calls conn_xprt_shutw, whose ssl_sock_shutw (src/ssl_sock.c) issues its own SSL_shutdown.
Out of scope: H2 backends and the EOF-as-HUP of #1605
#1605 records a backend EOF read with the last bytes as HUP for H1 backends only. For H2 backends there is nothing measurable to gain, with or without #1606:
- A graceful backend close (GOAWAY, then FIN) never reads the EOF:
handle_goaway_frame→goaway()clearsexpect_read, andforce_disconnect'sPosition::Clientarm writesReady::HUPitself, so the backend closes in the same pass. - H2 reads are exact-sized (
H2ReadTarget::Fill { amount }), so under perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 a complete frame is a full read (Continue), not a short one, and the EOF is met by the next header read in the same pass exactly as onmain. - What remains is a FIN without GOAWAY that lands between the
epoll_waitand therecv: at most oneepoll_waitper backend H2 connection, never per request.
Activity
perf(mux-h2): send the final GOAWAY and the TLS close_notify in one w…