Skip to content

Report how many requests are still being served - #23

Merged
fylorn merged 1 commit into
mainfrom
feat/in-flight
Sep 16, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/in-flight

Conversation

@fylorn

@fylorn fylorn commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

What this changes

tw_api::Status gains in_flight: the number of requests currently being
served, from entering the data plane until the last byte of the response has
been sent or the client has gone. Queued requests and streams still producing
output both count.

Workspace version 0.1.1.

Why

The desktop app is getting one-press automatic updates. Installing a new
version restarts the gateway, and a restart cuts every stream that has not
finished: a Claude Code task in the middle of a response loses that output and
has to send the request again. With this number the app can wait for it to
reach zero and restart in the gap between two requests instead.

It cannot be counted until the handler returns. For a streaming response
the handler returns before the first byte goes out, so a six-minute task would
count as finished before it had produced anything. The pass
(tw_gateway::live::Pass) travels with the response body and is returned when
the body is dropped. That covers a completed stream, and also a client walking
away mid-stream, when hyper discards the body and none of the code after the
stream loop runs — the path a hand-written decrement would miss, leaving a count
that never gets back to zero.

WebSocket upgrades move the pass into the upgraded connection's task, so a
Codex session counts for as long as its connection is open.

How it was checked

crates/tw-gateway/tests/live.rs runs a real gateway in front of a fake
upstream that sends one SSE frame and then stalls:

  • a request still streaming counts as 1 after its first frame has arrived;
  • a client that drops the body mid-stream brings the count back to 0;
  • a completed request and a request rejected at auth both return to 0;
  • an open WebSocket counts as 1 until the client disconnects.

Each test was also run against the broken version it guards against. Holding
the pass only until the handler returns fails the streaming and disconnect
tests ("流还没结束,它就不该被当成已经结束"); not moving it into the upgrade
fails the WebSocket test ("计数停在 0,等的是 1").

Gates: cargo fmt --check, cargo clippy --workspace --all-targets -D warnings,
cargo test --workspace (1,128 passed), ./scripts/smoke.sh (46 passed).

Found along the way, not fixed here

A client that disconnects mid-stream never produces RequestFinished or
RequestFailed, because the tail of the stream block never runs. The recorder
keeps such a request in memory and never writes it, so the tokens the upstream
already billed for it go unrecorded. That deserves its own change.

🤖 Generated with Claude Code

`Status` gains `in_flight`: requests from the moment they enter the data
plane until the last byte of the response has gone out, or the client
has left. Queued requests count; streams still producing output count.

The desktop app needs this to update without breaking anyone's work.
Installing a new version restarts the gateway, and a restart cuts every
stream that has not finished — a Claude Code task mid-response loses its
output and has to start that request again. Waiting for this number to
reach zero turns the restart into a gap between requests.

It cannot be counted until the handler returns. For a streaming response
the handler returns before the first byte is sent, so a six-minute task
would count as finished before it had started producing anything. The
pass travels with the response body instead, and is returned when the
body is dropped: when the stream completes, and also when a client walks
away and hyper discards the body — which is the path a hand-written
decrement would miss, leaving a count that never reaches zero. The
WebSocket path moves its pass into the upgraded connection's task, so a
Codex session counts for as long as the connection is open.

Both failure modes were checked against the tests: counting until the
handler returns fails the streaming and client-disconnect tests; not
moving the pass into the upgrade fails the WebSocket test.

Version 0.1.1, for the release the desktop app will pin.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit 84c3435 into main Sep 16, 2026
2 checks passed
@fylorn
fylorn deleted the feat/in-flight branch September 16, 2026 10:31
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