Skip to content

refactor(mux-h2): sample the frontend RTT once per Mux pass (#1339, Q11) - #1515

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-rtt-per-sweep
Sep 25, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-rtt-per-sweep

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Question 11 of #1339, local half. The remote half already shipped in #1471:
Endpoint::socket became Endpoint::peer_rtt, so server_rtt was injected
already. This is the client_rtt half.

What changed

ConnectionH2::snapshot_rtts called stats::socket_rtt on every stream
recycle, at five call sites. That was the last SocketHandler::socket_ref
reach in production h2.rs
— socket_mut never had one, and every
remaining spelling is in mod tests. A concrete mio::net::TcpStream is
exactly what no in-memory transport can synthesise, so this was one of the
last things tying the core to an operating-system type.

ConnectionH2::client_rtt is now a carried field, written only by
Mux::refresh_client_rtt and read only by snapshot_rtts. The five call
sites are untouched.

This costs syscalls, it does not save them

A per-sweep sample beats a per-stream one only above one recycle per
readiness sweep
, and at e2e concurrency it is well under that. Counted by
marking stats::socket_info and Mux::refresh_client_rtt, three runs per
workload, 1-minute load average 3.5 before and after:

workload stream recycles client_rtt TCP_INFO before after ratio all TCP_INFO before after ratio
test_h2_concurrent_streams (10 × 5 streams) 50 50 140 2.80× 110 200 1.82×
test_h2_50_concurrent_streams_no_crosstalk (5 × 50) 250 250 483 1.93× 505 738 1.46×

The before-counts are exactly the stream-recycle counts (50 and 250) and are
identical across all three runs; the after-counts vary a few percent, because
the rate now follows event-loop wake-ups rather than work completed. Recycles
per sweep were 0.36 and 0.52, where 1.0 is break-even — so the ratio improves
as multiplexing rises, and a busy connection amortises the sample the way the
shape intends while a quiet one does not.

For scale: the rejected per-entry-point shape (mirroring the value in wherever
ConnectionH2::now is mirrored from Context::now) measured 7.8–9.7× with
+6.5% / +5.3% CPU. So per-sweep is the cheaper of the two extraction shapes,
and it is not a free one. Emitting the access log as an output the shell fills
in removes the cost outright and is a larger change.

What the access log's RTT now means

On HTTP/2 only, client_rtt no longer means "the frontend SRTT when this
stream finished"
. It means "the frontend SRTT at the last readiness sweep".

  • Several H2 access logs emitted from one sweep carry an identical
    client_rtt. Repeated values across concurrent streams are expected and are
    not a stuck measurement.
  • A percentile over H2 access logs is now weighted by how many streams
    happened to complete together, and a value can be up to one sweep old.
  • server_rtt is unchanged — still read per access log through
    Endpoint::peer_rtt, because the backend socket is the other side of the
    connection. So the two cells on one H2 log line no longer share a sampling
    instant
    , and their difference is not a network asymmetry.
  • H1, Pipe (TCP/WS) and the TCP frontend are untouched and still measure per
    access log.

doc/configure.md gains a "When each cell is measured" subsection saying all
of this where an operator trending the field will find it.

Where the sample is taken

Mux samples at the three entry points that can reach an access log:
SessionState::ready, Mux::timeout_inner, Mux::shutting_down_inner.

  • In ready, above ready_inner's outer loop rather than inside it where
    Context::now is refreshed — a clock read is free, a getsockopt(TCP_INFO)
    is not, so one sweep pays at most one.
  • In timeout_inner, below Mux::consume_timer_entry: the wheel rounds to
    the nearest tick and a firing that re-validates as early is common (§7.6),
    so sampling above the gate would spend a syscall on passes that do nothing.
  • Mux::close keeps its own pre-existing sample; ConnectionH2::close emits
    no access log.

Docs

doc/configure.md (operator contract), doc/h2_mux_internals.md (decision,
the three shapes, the measurement table — and the now-false "the RTT read is
deliberately not taken here" paragraph rewritten), lib/src/protocol/mux/LIFECYCLE.md
§7.5 (the RTT joins the per-pass snapshot discipline, with its two differences
from the clock), CHANGELOG.md.

Tests

  • snapshot_rtts_reports_the_carried_pass_sample_to_every_stream (h2.rs) —
    two streams finishing in one pass report the same carried value. Premise
    asserted: the live loopback socket answers a µs SRTT that can never equal the
    sentinel, so a snapshot_rtts that merely lost the ability to read the
    socket would not pass.
  • a_mux_pass_refreshes_the_carried_client_rtt (mod.rs) — a later pass
    reports a freshly sampled value. Entered through SessionState::timeout,
    which needs no mock; SessionState::ready takes dyn ProxySession +
    dyn L7Proxy and this crate has no test implementation of either.

Both seen red first against a surgical revert, each carrying its own
TO SEE THIS RED recipe with the measured assertion text.

`ConnectionH2::snapshot_rtts` called `stats::socket_rtt` on every stream
recycle, at five call sites. That was the last
`SocketHandler::socket_ref` reach in production `h2.rs`, and a concrete
`mio::net::TcpStream` is exactly what no in-memory transport can
synthesise, so it was one of the last things tying the core to an
operating-system type. Q11's remote half already shipped: `Endpoint::socket`
became `Endpoint::peer_rtt`, so `server_rtt` was injected already.

`ConnectionH2::client_rtt` is now a carried field, written only by
`Mux::refresh_client_rtt` and read only by `snapshot_rtts`. `Mux` samples
at the three entry points that can reach an access log:
`SessionState::ready`, `Mux::timeout_inner` and
`Mux::shutting_down_inner`. In `ready` the sample sits above
`ready_inner`'s outer loop rather than inside it, where `Context::now` is
refreshed: a clock read is free and a `getsockopt(TCP_INFO)` is not, so
one readiness sweep must pay for at most one. In `timeout_inner` it sits
below `Mux::consume_timer_entry`, because the wheel rounds to the nearest
tick and a firing that re-validates as early would otherwise spend a
syscall on a pass that does nothing. `Mux::close` keeps its own existing
sample and `ConnectionH2::close` emits no access log.

Behaviour change, stated in `doc/configure.md` for operators: on HTTP/2
the access log's `client_rtt` no longer means "the frontend SRTT when
this stream finished" but "the frontend SRTT at the last readiness
sweep". Streams completing in one sweep report one identical value, a
percentile over H2 access logs is weighted by how many streams finished
together, and the value can be one sweep old. `server_rtt` still samples
per access log through `Endpoint::peer_rtt`, so the two cells on an H2
log line no longer share a sampling instant. H1, Pipe and the TCP
frontend are untouched.

This costs syscalls rather than saving them at e2e concurrency, because
a per-sweep sample beats a per-stream one only above one recycle per
sweep. Counted three times per workload at a 1-minute load average of
3.5, with `stats::socket_info` and `Mux::refresh_client_rtt` marked:
`client_rtt` TCP_INFO calls went 50 -> 140 (2.80x) on
`test_h2_concurrent_streams` and 250 -> 483 (1.93x) on
`test_h2_50_concurrent_streams_no_crosstalk`; all TCP_INFO went
110 -> 200 (1.82x) and 505 -> 738 (1.46x). The before-counts are exactly
the stream-recycle counts and identical across runs; the after-counts
vary a few percent because the rate now follows event-loop wake-ups
rather than work completed. The rejected per-entry-point shape measured
7.8-9.7x with +6.5% / +5.3% CPU, so this is the cheaper extraction and
not a free one. Emitting the access log as an output the shell fills in
removes the cost outright and is a larger change.

Pinned by `snapshot_rtts_reports_the_carried_pass_sample_to_every_stream`
and `a_mux_pass_refreshes_the_carried_client_rtt`, each seen red first
against a surgical revert and carrying its own recipe.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the refactor/h2-rtt-per-sweep branch from a0f2bcb to 54d9292 Compare September 25, 2026 02:00
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 9025758 into main Sep 25, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the refactor/h2-rtt-per-sweep branch September 25, 2026 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant