Skip to content

refactor(gateway): split the request pipeline into stages and server.rs into modules - #166

Merged
fylorn merged 1 commit into
mainfrom
refactor/split-pipeline
Sep 24, 2026
Merged

fylorn merged 1 commit into
mainfrom
refactor/split-pipeline

Conversation

@fylorn

@fylorn fylorn commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Pure refactor of `crates/tw-gateway/src/server.rs` (2887 lines). No behaviour change; all tests pass unchanged.

Pipeline (`server/pipeline.rs`, was one ~1100-line fn):

  • `pipeline` only strings the steps together: `probe` (step 1.3, local answers) → `read` (step 2, routing facts) → `admit` (step 1.5, model admission) → `route` (rules, serving filter, group ordering) → concurrency gate (step 3) → `start` (breaker filter, `RequestStarted`, ending, redaction record, body capture).
  • `pipeline/hop.rs`: `try_upstreams` (the failover loop, `RequestRouted`), with `protocol_mismatch`, `prepare` (per-hop body: passthrough rewrite or conversion, ChatGPT shaping) and `send` (headers + one OAuth 401 retry).
  • `pipeline/relay.rs`: `respond` builds the response; what to do is decided once in `Plan`, per-chunk work (restore → convert → tool wall) lives on `Relay` (`chunk` / `finish` / `error_tail`), so the stream body is just the loop and the ending.

Other moves

  • `state.rs`: `Runtime`, `AppState`; `state/credentials.rs` (headers, OAuth tokens, expiry/rotation reports), `state/upstream.rs` (auth/proxy/quota status).
  • `outbound.rs`: `base_client_builder`, `public_client`, `client_for_provider`, `proxy_shape`.
  • `server/listing.rs` (`/v1/models`), `server/upgrade.rs` (WebSocket upgrade), `latency::seed_url_test` (was `server::seed_latency`).
  • Public re-exports from `tw_gateway` are unchanged (`AppState`, `Runtime`, `client_for_provider`, `public_client`, `router`, `serve`).

🤖 Generated with Claude Code

…rs into modules

The 1100-line pipeline fn becomes one function per pipeline step
(probe, read, admit, route, start) plus two submodules: hop (try the
candidates in order) and relay (hand the response to the client, with
the per-chunk work on a Relay struct). server.rs keeps the router,
passthrough and the event helpers; AppState/Runtime move to state.rs
(credentials and upstream-status bookkeeping in child modules), the
per-upstream HTTP clients to outbound.rs, the model listing and the
WebSocket upgrade to their own files, and seed_latency to latency.rs.

No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn force-pushed the refactor/split-pipeline branch from 2989a75 to ed65391 Compare September 24, 2026 07:08
@fylorn
fylorn merged commit f250091 into main Sep 24, 2026
3 checks passed
@fylorn
fylorn deleted the refactor/split-pipeline branch September 24, 2026 07:18
@fylorn fylorn mentioned this pull request Sep 24, 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