Skip to content

Keep a streamed request alive while it waits for admission - #317

Merged
fylorn merged 1 commit into
mainfrom
fix/keepalive-while-queued
Oct 10, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/keepalive-while-queued

Conversation

@fylorn

@fylorn fylorn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

What

A streamed generation request that waits for admission — its key's max_concurrent gate, or room in the key's per-minute / per-hour window — now gets the same treatment as one waiting for an upstream's slot: after the 15-second opening hold it receives the 200, the stream headers and : keep-alive comments until the first content arrives.

Before, the opening hold (commit::hold) started only after admission::admit returned, so a queued request sent the client nothing at all for as long as it waited, and clients that time out on the response headers gave up even though the gateway would have served them shortly after.

How

  • Steps 3–5 of the pipeline (admission, content screening, the start event, the upstream hops, the relay) now live in one owned future (answer, with enter for steps 3–4) and the opening hold wraps that future, so the timer starts when the request enters admission.
  • If admission hard-refuses the request after the headers already went out (the rate window cannot free in time), the refusal ends the stream as an error frame in the client's own dialect, through the same path the idle-timeout and failover errors already use, carrying the same message code the non-streamed 429 uses. History records it exactly as before: started, empty attempt chain, failed with source rate_limited.
  • Non-streamed requests and Gemini clients without alt=sse are unchanged: no early headers, the answer is still awaited whole.
  • The idle-timeout clock (no content for idle_timeout_secs) still starts only when the upstream request is actually sent, not during the queue wait.
  • AppState::gate is now pub (like slots) so integration tests can hold a key's concurrency slot.

Tests

New in crates/tw-gateway/tests/idle_timeout.rs:

  • a_request_queued_on_the_keys_gate_gets_the_headers_and_keepalives_before_a_slot_frees — held on the key gate longer than the hold (and, together with the upstream's delay, longer than the idle window): headers arrive after the hold and before the slot frees, keepalives first, then the normal answer; attempt chain [served], finished 200.
  • a_refusal_after_the_headers_went_out_is_told_in_the_stream — one request per minute, no slot wait, second request held on the gate past the hold, then refused: 200 + keepalives, then an event: error frame with the 429 sentence; the upstream saw only the first request; history has the started row, an empty attempt chain and a rate_limited failure with code gw.key_limit.requests_rolling.
  • a_whole_answer_queued_on_the_keys_gate_still_waits_for_the_answer — non-streamed request held on the gate: headers only arrive with the whole answer, no keepalives.

The two streamed tests fail on main and pass with this change; the non-streamed one passes on both.

Verified locally: cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test -p tw-gateway and cargo test --workspace (local proxy unset) — all green.

New messages

None. No control API or protocol change.

🤖 Generated with Claude Code

The opening hold (15 s, then 200 + `: keep-alive` comments until the
first content) used to start only after admission. A streamed request
queued on its key's concurrency gate or waiting for room in the key's
per-minute/per-hour window therefore got nothing at all for as long as
it waited, and clients that time out on the response headers gave up
even though the gateway would have served them a moment later. Waiting
for an upstream's slot was already covered; waiting for the key was not.

Steps 3-5 of the pipeline (admission, content screening, the start
event, the upstream hops and the relay) now live in one owned future,
and the hold wraps that future, so the timer starts when the request
enters admission. A request held on the key gate or the rate window gets
its 200 and keepalives after 15 s exactly like one waiting on an
upstream. If admission then hard-refuses the request after the headers
went out, the same 429 message code ends the stream as an error in the
client's dialect, and history records the refusal exactly as before
(started, empty attempt chain, failed with the rate-limited source): the
owned future reports its own ending, as the answer stage already did.

Non-streamed requests and Gemini clients without `alt=sse` are unchanged
(no early headers). The idle-timeout clock still starts when the
upstream request is actually sent, not during the queue wait: the
existing slot-wait test and the new key-gate test both queue longer
than the window and still get their answer.

`AppState::gate` is now public like `slots`, so the integration tests
can hold a key's slot the way they already hold an upstream's.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@fylorn
fylorn merged commit a675f6d into main Oct 10, 2026
5 checks passed
@fylorn
fylorn deleted the fix/keepalive-while-queued branch October 10, 2026 10:52
@fylorn fylorn mentioned this pull request Oct 10, 2026
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