Skip to content

docs(rate-limit): record that a failed backend dial keeps the per-(cluster, IP) slot - #1528

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/cluster-ip-slot-on-failed-dial
Sep 25, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/cluster-ip-slot-on-failed-dial

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Answers the measurement #1521 asks for, and takes the decision it leads to. No
production code changes: lib/src/server.rs gains only /// lines.

Rebased onto main after #1519 landed, and re-derived against it rather than
search-and-replaced — the structural claim below is still true, but it is now true
across two functions, and a reader needs to know which exits live where to check it.

The measurement

Router::plan_connect claims a (cluster, source-IP) slot after the
cluster_ip_at_limit gate and before backend selection. Six fallible exits
follow that claim, not the four the issue lists — the fresh connection's
start_stream refusal and the register_socket rollback are also post-claim — and
none of them releases. Since #1519 they sit in two functions:

Exit Lives in Error In the issue?
reused connection missing from the router's backend map Router::plan_connect MaxSessionsMemory yes
reused backend refuses one more stream Router::plan_connect MaxSessionsMemory yes
? on Router::backend_from_request Mux::dial_backend Backend(_) yes
Connection::new_h2_client pool exhaustion Mux::dial_backend MaxBuffers yes
freshly-built connection refuses the stream Mux::dial_backend MaxSessionsMemory no
L7Proxy::register_socket fails (rolls back gauges, slab, epoll — not the slot) Mux::dial_backend MaxSessionsMemory no

The split moved no ordering and did not fork the error path: Mux::ready_inner
joins the plan and the dial with and_then, so both halves land in one Result
and one error arm. That arm holds 11 set_default_answer* calls and zero
occurrences of SessionResult — nothing there closes a session directly.

What closes the connection is the answer, not the error. Every built-in
template in lib/src/protocol/kawa_h1/answers.rs carries Connection: close, so
set_default_answer_with_retry_after clears HttpContext::keep_alive_frontend.

  • HTTP/1.1 — the response-complete branch of ConnectionH1::writable takes
    defer_close_for_tls_flush instead of the keep-alive reset. One flush.
  • HTTP/2, failed stream was the only one — close_frontend is raised as the
    stream retires and ConnectionH2::goaway(NoError) is sent. Same pass, modulo the
    TLS flush.
  • HTTP/2, other streams live — ConnectionH2::graceful_goaway sends the
    advisory GOAWAY instead, and a draining connection refuses new peer streams
    (RefusedStream). So a failed dial on one stream does not leave the connection
    serving indefinitely: the window is the remaining lifetime of the streams already
    open, and the client cannot extend it. The final GOAWAY goes out from
    finalize_write when the last stream retires. The
    h2_graceful_shutdown_deadline_seconds forced close does not bound this — it
    is armed only from the worker soft-stop path.
  • Raw TCP — one post-claim exit (backend_from_cluster_id). Any Err becomes
    SessionResult::Close in the same event-loop pass, and TcpSession::close
    untracks directly.

Two branches where the window is longer, both written down:

  1. A custom answer template that omits Connection: close — the keep-alive opt-in
    the 429 template already documents — never clears keep_alive_frontend, so no
    H1 close and no GOAWAY. The slot is then held for the connection's whole
    lifetime.
  2. ConnectionH2::try_recycle_server_stream defers retirement while the client has
    not sent END_STREAM, so the GOAWAY waits with it. Bounded by
    h2_stream_idle_timeout_seconds / cancel_timed_out_streams, and it costs one
    slot for one held connection — the same arithmetic as a healthy connection.

A backend that is registered and healthy-status but down may not fail on the dial
at all. Backend::try_connect is non-blocking: a synchronous refusal (the common
case for a closed port on loopback) surfaces as BackendError::ConnectionFailures
and takes the answer path, while an EINPROGRESS connect returns Ok and its
refusal arrives later as a backend HUP, so the stream retries through
EndStreamAction::Reconnect. The re-track is idempotent, so one slot — not one
per attempt — is held for at most CONN_RETRIES (3) attempts, each bounded by
connect_timeout (default 3 s).

The decision

The short-window reading holds in the shipped configuration, so this documents the
claim rather than pairing a release with it. Releasing on failure would loosen the
cap under exactly the conditions a client can induce, and would buy little: the
connection that provoked the failure is closed by the answer it provoked, so it
cannot accumulate slots either way.

What changed

  • lib/src/server.rs — the reasoning at SessionManager::track_cluster_ip, so the
    unpaired claim reads as a decision. /// lines only.
  • doc/rate-limit-design.md — a new §3.2.1 under Counter discipline.
  • doc/lifetime_of_a_session.md — citation re-anchor forced by the 69-line
    insertion into lib/src/server.rs. Each cited line was re-resolved by its text at
    the base revision and its enclosing fn. Two citations to the
    accept_queue.backpressure gauge are symbol citations rather than numbers,
    because the shift makes this changeset reuse line 594 for different code.

No test: the outcome is documentation, and there is no behaviour to pin.

Closes #1521

…uster, IP) slot

`Router::plan_connect` claims a `(cluster, source-IP)` slot after the
`cluster_ip_at_limit` gate and before backend selection, and none of the
six fallible exits that follow the claim releases it. Since #1519 split
the connect path those exits sit in two functions — the two pool-reuse
refusals stay in `Router::plan_connect`, the four on the fresh-dial path
moved to `Mux::dial_backend` — but the ordering is unchanged and
`Mux::ready_inner` joins both halves into one `Result` with `and_then`,
so they still share one error arm. The only release is
`SessionManager::untrack_all_cluster_ip`, from session teardown. The
unpaired claim reads like an oversight, so state that it is a decision and
what the decision rests on.

Measured on the error path rather than assumed. Every post-claim exit
reaches the `BackendConnectionError` arm of the pending-link loop in
`Mux::ready_inner`, which answers the stream from the template registry
instead of retrying, and every built-in answer template carries
`Connection: close`. That clears `HttpContext::keep_alive_frontend`, and
that one bit is what ends the frontend connection: H1 closes after the
answer flushes instead of taking the keep-alive reset, H2 sends a final
GOAWAY when the failed stream was the only one and the advisory GOAWAY of
a graceful drain otherwise, and a draining connection refuses new peer
streams so the client cannot extend the window. Raw TCP never reaches an
answer — any error out of `TcpSession::connect_to_backend` closes the
session in the same event-loop pass. So the slot comes back with the
connection, and the "a failed dial leaks a slot until an unrelated
timeout" reading does not hold in the shipped configuration.

Two branches are written down beside it. A backend that is registered and
healthy-status but down does not fail on the dial at all, because
`Backend::try_connect` is non-blocking: the refusal arrives later as a
backend HUP and the stream retries, holding one slot — not one per
attempt, the re-track is idempotent — for at most `CONN_RETRIES` attempts.
And a custom answer template that omits `Connection: close` — the
keep-alive opt-in the 429 template already documents — holds the slot for
the connection's whole lifetime.

No behaviour change: `lib/src/server.rs` gains only `///` lines. The
citation re-anchor in `doc/lifetime_of_a_session.md` is that insertion
shifting the file by 69 lines; each cited line was re-resolved by its
text at the base revision and its enclosing `fn`. Two citations to the
`accept_queue.backpressure` gauge become symbol citations because the
shift made this changeset reuse line 594 for different code.

Refs: #1521
Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/cluster-ip-slot-on-failed-dial branch from d366b50 to 914bc65 Compare September 25, 2026 12:55
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 00118f7 into main Sep 25, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/cluster-ip-slot-on-failed-dial branch September 25, 2026 14:09
FlorentinDUBOIS added a commit that referenced this pull request Sep 25, 2026
…er-pool span (#1533)

`doc/lifetime_of_a_session.md` cited a buffer-pool gauge block as the evidence
for the 1 Hz `accept_queue.saturated_seconds` ticker, in the section 3.3
narrative and again in the metric inventory. The ticker is fifteen lines further
down, in the `ACCEPT_SATURATION_TICK` block of `Server::run`; both sites now
cite that symbol. The companion span covering the constant's own declaration and
doc comment was already correct and is kept as a range.

Every gate passed on the defect, each for a defensible reason: the cited lines
resolve and are not blank, so rule 1 is satisfied, and the cited text never
changed, so rule 2 has nothing to compare — #1528 re-anchored the number
faithfully while it was already wrong. A faithful re-anchor of a wrong citation
is indistinguishable from a faithful re-anchor of a right one. That is #1447
seen from the other side: there a rule fired on a citation that was correct,
here every rule stayed silent on one that is not. The fix is a symbol rather
than a corrected number because a number that has survived two faithful
re-anchors while wrong is exactly the case the convention exists for.

Sweeping all eighteen cited spans in the document by hand found three more of
the same class, none of them in the metric inventory, which is clean. The
write-only-shutdown pair both landed inside the failed-upgrade branch whose own
comment says the front socket is unreachable — the inverse of the claim they
support — and now cite the `Shutdown::Write` block of `HttpsSession::close` and
its mirror in `HttpSession::close`. The SoftStop / HardStop citation straddled
two functions while containing neither verb, and now cites
`Server::read_channel_messages_and_notify`.

Whether `--audit` can be made to catch the class was measured, and the answer is
no. The candidate is not a new rule but a wider prose window on the existing
symbol signal, which stayed silent only because the citation sits mid-line under
the sentence naming the metric, so both spans were declined rather than
examined. Widening reports one real defect per six additions, or two per
thirteen for the literal rule the issue proposed, against the six-in-thirteen
rate the mode measures for itself and prints in its own banner; the literal rule
also silences three correct-citation findings it reports today. The variants that
do fire on this citation fire by a one-line margin — the window is eight and the
ticker's own comment naming the metric sits nine lines past the wrong span's
end. Those numbers, the second silencer behind them and that margin are recorded
in `doc/README.md` and in the script's header. The script's behaviour is
unchanged; the diff to it is comment-only.

Documentation only: no `.rs` file changes.

Closes #1531

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.

A failed backend dial claims the per-(cluster, source-IP) slot until session close, shared across sessions and with the TCP proxy

1 participant