Skip to content

refactor(mux-h2): scope the H2 block converter to one kawa.prepare call - #1463

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-write-streams
Sep 23, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-write-streams

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Step 7 of the H2 sans-io extraction. Follows #1440.

write_streams built one H2BlockConverter and held it across the whole
per-stream write loop, which forced every function it touched to be an
associated function taking individual field references instead of a method —
because a &self receiver would have conflicted with the converter's borrow.

This scopes the converter to one kawa.prepare call. The borrow no longer
spans the loop, so those functions become ordinary methods.

What changes

  • H2BlockConverter is constructed per kawa.prepare call rather than once per pass.
  • snapshot_rtts becomes a &self method (was an associated fn taking
    position, socket, endpoint, linked_token).
  • try_recycle_server_stream becomes a &mut self method (was an associated fn
    taking position, bytes, streams, …).
  • hpack_state.rs gains one line in shrink_converter_buffers.

Documentation the rebase made mandatory

main had added prose explaining why those two were free functions:

snapshot_rtts "is an associated function taking individual field references
rather than a &self method … a &self receiver would conflict"

try_recycle_server_stream "calls the free function directly because it is
itself a static helper holding &mut H2ByteAccounting rather than &mut self"

Both statements are false once this changeset lands — it is precisely the
constraint they describe that it removes. They are rewritten rather than left:

snapshot_rtts "is an ordinary &self method: the H2BlockConverter is built
for one kawa.prepare call rather than held across the per-stream write loop"

try_recycle_server_stream "… rather than through the &mut self wrapper — a
spelling choice, not a constraint"

This is the repository's dominant defect class — a justification comment that
outlives its reason — caught because the rebase forced the two sides together.

Rebase notes (this branch is old)

Three of its four original commits are already on main as squash merges and
were dropped with --onto, each verified by content, not by commit message:

dropped evidence
f76c8eb3 h2_drain.rs blob identical to main's
3be8c888 h2_header_reassembly.rs blob identical to main's
f4d49696 h2_flood_detector.rs differs +319/−34 — #1418 privatised H2FloodConfig behind accessors. Main's version won; the branch's older copy was not resurrected

Independent check that nothing was resurrected: the merged ## [Unreleased]
section adds exactly one bullet over main, with zero duplicates and no
heading drift.

47 conflicts against the final base. 45 were numbers-only — main and this
branch renumbering the same citations from a shared ancestor — resolved by
remapping every h2.rs:NNN through a difflib.SequenceMatcher map into the
final merged file and text-verifying each against the pre-rebase tree, zero
unmapped. Two were genuine prose, both resolved in main's favour: #1434 had
converted the compute_stream_byte_totals pinned block to a symbol citation,
and added prose justifying a retained line for generate_access_log.

Also re-derived, and not mentioned in the commit message: doc/README.md's
syntax example (hpack_state.rs:121-131 → 122-132, :139 → :140) and 11
pinned blocks in doc/h2_mux_internals.md, two of which had their content
changed by this changeset and were re-quoted rather than renumbered. A collision
check for the #1447 reused-number shape found none.

Replay faithfulness, counted: 409/409 added h2.rs lines, 116/116
converter.rs, 5/5 hpack_state.rs, 36/36 CHANGELOG — nothing lost, nothing new.

Verification

CI's own per-crate commands, FLAGS='--no-default-features --features crypto-ring,opentelemetry,splice,simd':

check result
cargo +nightly fmt --all -- --check 0
cargo build --locked $FLAGS 0
cargo clippy --all-targets --locked $FLAGS -- -D warnings 0
cargo test -p sozu-lib 0 — 1017 passed
cargo test -p sozu 0 — 224 passed
cargo test -p sozu-e2e -- --skip tests::fuzz_tests:: 0 — 446 passed, 0 failed
3 --ignored cells (ci.yml:182-184) 0 / 0 / 0 — 3, 1, 1 passed
check_doc_citations.py --root . / --base / --self-test 0 / 0 / 0
git merge-tree --write-tree origin/main <head> 0, gate re-run inside the extracted tree: 0

Note the two sozu-sim cells build a 0-test binary unless tokio_unstable
is set — run with RUSTFLAGS='--cfg tokio_unstable' they are 6 and 6 passed.

An earlier intermediate base produced three red e2e runs with disjoint
failure sets at 1-minute load 14–30 on 8 cores; all passed in isolation, and the
verbatim suite went 446/0 at load 9.6. Load-dependent, not a regression — and
the only candidate code explanation was ruled out directly (#1452's sole live
change replaced the literal 4096 with a named constant of the same value).

`ConnectionH2::write_streams` built one `H2BlockConverter` per write pass and
held it across the whole per-stream loop. The converter borrows the connection's
HPACK encoder out of `HpackState`, so that outstanding `&mut self.hpack` made
every `&self` / `&mut self` method uncallable inside the loop: `snapshot_rtts`
and `try_recycle_server_stream` were associated functions taking individual
field references, and both said so in their own doc comments.

Build the converter for exactly ONE `kawa.prepare` call instead. The borrow then
lasts two statements rather than the loop, and both functions become ordinary
methods — `self.snapshot_rtts(&endpoint, linked_token)`, exactly the shape
`doc/h2_mux_internals.md` was already describing, and
`self.try_recycle_server_stream(stream, ...)` — dropping five parameters across
five call sites.

New `converter::H2ConverterPass` carries what has to cross from one stream's
`prepare` to the next: the three reusable scratch buffers, MOVED (`Vec`
pointer/length/capacity) and never copied, and the RFC 7541 §6.3
dynamic-table-size-update signal, which belongs to the first header block of the
pass and to no other. Threading that signal through `reclaim` rather than by hand
at each site is what stops a second stream re-emitting the prefix:
`emit_pending_size_update_if_new_block` `take()`s it the moment it writes it. The
encoder stays in `HpackState` and is re-borrowed per `prepare`, so the HPACK
dynamic table remains continuous across the streams of a pass.

No behaviour change and no new copy on the write path: the eleven buffer-copying
sites in `converter.rs` are the same eleven, and `flush_stream_out` still gathers
`IoSlice`s straight out of `kawa.storage.buffer()`.

What stays deferred to after the loop is deferred for ordering reasons the
comments now state instead of citing the borrow: RST accounting via
`freshly_emitted_rsts`, because a MadeYouReset cap trip returns a GOAWAY result
that must not preempt the remaining streams' writes; and stream retirement via
`completed_streams`, because `try_recycle_server_stream` passes `is_last_stream`
as `streams().len() == 1` — the branch that hands the whole remaining overhead
pool to one stream — so retiring inline would let a later completer of the same
pass drain it while other streams are still live.

Tests: `one_write_pass_prefixes_its_first_header_block_only_and_shares_one_encoder`
pins both cross-stream properties on the wire — two streams in one pass, the §6.3
prefix on the first block only, one peer decoder replaying both blocks, and, as
the negative half, a fresh decoder that must FAIL on the second because it indexes
dynamic-table entries only the first block created. Seen red both ways: dropping
the `pending_table_size_update` line from `reclaim` makes stream 3 re-emit the
prefix, and handing the pass a fresh encoder makes the last assertion fail.
`write_pass_property::qc_one_write_pass_prefixes_its_first_header_block_only`
generalises the stream count (2..=5) and the advertised table size with
`quickcheck`. Neither property is visible to `converter.rs`'s own unit tests,
which drive one converter over one kawa.

Docs: LIFECYCLE.md gains invariant 25 for the two properties, invariant 17's two
approximate `~NNNN` line references become exact citations of the three
bucket-decrement sites, and 75 citation endpoints across LIFECYCLE.md and
doc/testing.md are renumbered to the lines their text moved to, each by an
exact `difflib` mapping over the diff's `equal` blocks so the new line's text is
identical to the old by construction. Four became symbol citations instead,
because their new numbers collided with numbers the base documents cite for
different code and the drift rule keys on the citation, not on where it sits:
the `EndStreamAction::Reconnect` `StreamState::Link` transition, the four
`self.now = context.now;` entry points (one enumeration, so all four moved
together), and the `FcStallAction::Arm` branch of `write_streams`.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 5d5191e into main Sep 23, 2026
24 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the refactor/h2-write-streams branch September 23, 2026 03:55
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