Skip to content

refactor(mux): make the per-IP limit gate a returned step (#1340, Q6) - #1527

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-router-ip-gate-step
Sep 25, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-router-ip-gate-step

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Completes the H2 half of Question 6 of #1340. Stacked on #1525; retarget to main when
that lands.

Why the gate could not be a view

L7Proxy::clusters and L7Proxy::kind became RoutingView in #1525. The per-(cluster, source-IP) limit gate could not follow them, because it is not a read. It consults
SessionManager::cluster_ip_at_limit and calls SessionManager::track_cluster_ip, which
mutates connections_per_cluster_ip — worker-global state keyed on (cluster, ip) across every
session on the worker
, and read again by lib/src/tcp.rs's own gate for the TCP proxy. A
read-only view cannot carry a mutation, and a trait with a mutating method would be a name that
lies.

So it becomes a step:

Router::plan_connect  ──► ConnectStep::CheckIpLimit(ConnectResume)
                              │
                     consult_ip_gate (mod.rs): consult, and track
                              │
                          IpGateVerdict
                              ▼
Router::plan_connect_resume ──► ConnectPlan::{Attached, Dial}

Router::plan_connect now takes no Rc<RefCell<dyn L7Proxy>> at all.

Two properties the types enforce, rather than comments

  • ConnectResume is taken by value. A parked decision cannot be resumed twice. That is
    H2Shell::settled taking its parked target, applied to this seam.
  • The resumed call returns ConnectPlan, not ConnectStep. A second gate consultation is
    unrepresentable, not merely incorrect.

Both entries — gated and ungated — share one Router::decide_after_gate body, so the two paths
cannot drift. Drift is the thing a split like this most easily gets wrong.

IpGateVerdict::Admitted carries no payload, on the same principle
H2WritableStateTarget::Flush carries no bool: it means the embedder has already performed
the track, so there is no binding a later edit could reuse to re-ask a question it has answered.

Counts — and why 3 is the floor, not a miss

Comment lines filtered, production only, in lib/src/protocol/mux/router.rs:

before after
Rc<RefCell<dyn L7Proxy>> production code references 4 3

Removing the gate leaves L7Proxy at 3 in router.rs — the import plus
Router::backend_from_request and Router::get_backend_for_sticky_session reaching
L7Proxy::backends — and reaching zero requires giving selection a borrowed view of backend load
state, which is Question 12 part 2, a fourth stage and not a failure of the third.

Across the three PRs of this half: ProxySession 4 → 0 (#1519), L7Proxy 5 → 4 (#1525) → 3 (here).

The test is red for the resume ordering, not for the gate's verdict

an_admitted_stream_is_counted_before_the_decision_resumes drives the real production sequence —
Router::plan_connect → consult_ip_gate → Router::plan_connect_resume — for two different
frontend tokens from the same source IP
, against a limit of one.

Two tokens rather than two streams on one token, and that is the load-bearing detail: the gate is
deliberately idempotent within a token, because an H2 session multiplexing many streams to one
cluster holds a single slot. A second stream on the same token could never trip the limit and would
prove nothing.

consult_ip_gate is a free function in mod.rs rather than a Mux method precisely so the test
can drive it. Testing this ordering through a copy of the logic would prove nothing about the
logic.

Seen red by deleting the track_cluster_ip call so the gate returns Admitted without
counting:

a SECOND token from the same source IP must find the slot already counted — an Admitted verdict
here means the first stream resumed without being tracked

That is red for the ordering — an admitted stream that resumed uncounted — not for whether the
gate computes the right verdict. The production diff and the test share no string literal.
cargo test -p sozu-lib goes 1128 → 1129 passed.

Verification

Feature set --no-default-features --features crypto-ring,opentelemetry,splice,simd. Every exit
code read directly, never through a pipe.

Command Exit
cargo build --locked <flags> 0
cargo +nightly fmt --all -- --check 0
cargo clippy --all-targets --locked <flags> -- -D warnings 0
cargo test -p sozu-lib --locked <flags> 0 — 1129 passed
cargo test -p sozu --locked <flags> 0
cargo test -p sozu-e2e --locked <flags> -- --skip tests::fuzz_tests:: 0 — 447 passed
RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked 0 — 30 passed, unchanged
RUSTFLAGS="--cfg tokio_unstable" cargo clippy -p sozu-sim --all-targets --locked -- -D warnings 0
check_doc_citations.py --root . 0
check_doc_citations.py --base c1000dae 0
check_doc_citations.py --audit --root . 0 — 7 findings, all 7 identical at base
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked 0
cargo bench -p sozu-lib --bench h2_stream_table -- --test 0

Citations remapped by content; the in-place remap was proved equivalent to a base reset (every
guarded doc's citation sequence was byte-identical to base beforehand). All 17 exempt spans (22
endpoints)
audited on three keys — byte-identical over the span's entire length rather than
only its endpoints, the base→head line map from each source file's own content diff, and the
enclosing fn matched by name with an enclosing-type fallback. All 17 hold.

One citation was converted to a symbol rather than renumbered: Mux::timeout_inner's
mod.rs:2776. It is correct at head, but the number is one this changeset reused for different
code, so the drift rule compares it against what the base put there — the class
#1447 named. The prose already names the symbol, so the line added nothing.

e2e was run in a quiet window, with 1-minute loadavg and pgrep sozu_e2e | wc -l recorded before
and after.

No sozu-sim scenario, workload weight or assertion is touched; this PR modifies no file under
sim/.

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the refactor/h2-router-cluster-view branch from c1000da to 2e02720 Compare September 25, 2026 11:30
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the refactor/h2-router-ip-gate-step branch from 41157ff to 10d47af Compare September 25, 2026 11:32
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the refactor/h2-router-cluster-view branch from 2e02720 to b9f56c1 Compare September 25, 2026 12:42
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the refactor/h2-router-ip-gate-step branch from 10d47af to f9bc17f Compare September 25, 2026 12:47
@FlorentinDUBOIS
FlorentinDUBOIS changed the base branch from refactor/h2-router-cluster-view to main September 25, 2026 12:47
The last thing `Router::plan_connect` reached
`Rc<RefCell<dyn L7Proxy>>` for. It could not follow `clusters()` and
`kind()` into the borrowed view, because it is not a read: it calls
`SessionManager::cluster_ip_at_limit` AND
`SessionManager::track_cluster_ip`, which mutates
`connections_per_cluster_ip` — worker-global state keyed on
`(cluster, ip)` across every session, read again by `lib/src/tcp.rs`'s
own gate for the TCP proxy.

So it becomes a step. `Router::plan_connect` answers
`ConnectStep::CheckIpLimit(ConnectResume)`, `consult_ip_gate` performs
the consult and the track, and `Router::plan_connect_resume` finishes on
the verdict. `plan_connect` takes no `L7Proxy` handle at all.

Two properties are enforced by types rather than by comment.
`ConnectResume` is taken by value, so a parked decision cannot be resumed
twice — `H2Shell::settled` taking its parked target, applied here. The
resumed call returns `ConnectPlan`, not `ConnectStep`, so a second gate
consultation is unrepresentable rather than merely incorrect. Both
entries share one `Router::decide_after_gate` body so the gated and
ungated paths cannot drift.

`Rc<RefCell<dyn L7Proxy>>` production code references in `mux/router.rs`
go 4 -> 3, comment lines filtered. That 3 is this stage's floor and not a
miss: the remaining three are the import plus `backend_from_request` and
`get_backend_for_sticky_session`, which reach `L7Proxy::backends` for
selection. Reaching zero means giving selection a borrowed view of
backend load state, which is #1340 Question 12's second part — a further
stage, not a failure of this one.

No behaviour change.
`an_admitted_stream_is_counted_before_the_decision_resumes` drives the
real three-step sequence for two different frontend tokens from one
source IP against a limit of one, and pins that the track lands before
the decision resumes. Two tokens rather than two streams on one token,
because the gate is deliberately idempotent within a token. Seen red by
deleting the `track_cluster_ip` call so the gate returns `Admitted`
without counting: the second token is then admitted too. The production
diff and the test share no string literal. `cargo test -p sozu-lib` goes
1128 -> 1129.

Signed-off-by: Florentin Dubois <[email protected]>
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