Repository navigation
Keep a streamed request alive while it waits for admission - #317
Merged
Merged
Conversation
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>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A streamed generation request that waits for admission — its key's
max_concurrentgate, 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 the200, the stream headers and: keep-alivecomments until the first content arrives.Before, the opening hold (
commit::hold) started only afteradmission::admitreturned, 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
answer, withenterfor steps 3–4) and the opening hold wraps that future, so the timer starts when the request enters admission.rate_limited.alt=sseare unchanged: no early headers, the answer is still awaited whole.idle_timeout_secs) still starts only when the upstream request is actually sent, not during the queue wait.AppState::gateis nowpub(likeslots) 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 anevent: errorframe with the 429 sentence; the upstream saw only the first request; history has the started row, an empty attempt chain and arate_limitedfailure with codegw.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
mainand 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-gatewayandcargo test --workspace(local proxy unset) — all green.New messages
None. No control API or protocol change.
🤖 Generated with Claude Code