Skip to content

interceptor: streaming RPC support via Stream-shaped intercept_streaming - #121

Merged
iainmcgin merged 1 commit into
mainfrom
iain/streaming-interceptors
May 20, 2026
Merged

iainmcgin merged 1 commit into
mainfrom
iain/streaming-interceptors

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Why

Interceptor::intercept_unary (#114) covers only unary RPCs. Server-streaming, client-streaming, and bidi-streaming bypass the chain. For an auth interceptor that's a vulnerability — a streaming RPC that skips authorization — not a gap. This adds Interceptor::intercept_streaming covering all three streaming shapes.

Design: Stream-shaped, not connection-shaped

connect-go exposes a StreamingHandlerConn with Receive()/Send() methods because Go handlers push: they call stream.Send(res) for each item. There's a per-item Send() call site for the interceptor's wrapper to hook.

connect-rust handlers produce: they return a Stream the framework polls. There is no per-item send() call site anywhere in the dispatch path. To synthesize one — to make the framework "call" conn.send(item) for each polled item so a conn wrapper could hook it — would require an mpsc channel between the handler's stream and the wire encoder, plus a pump task that drains it. A per-stream allocation, a task spawn, a buffer-size knob, and changed backpressure semantics, for the privilege of looking like Go.

The Rust-idiomatic shape against a Stream-producing handler is a Stream adapter: the interceptor wraps the inbound Stream, calls next.run, wraps the outbound Stream, returns. No channel, no pump task — .map() is one closure call per item when the closure does something and zero when it doesn't. This matches how tower, tonic, and axum all do Stream-shaped body interception.

Cross-stream coordination (deciding on an outbound item based on what was observed inbound) needs shared state between the two adapters — Arc<Mutex<..>> captured by both closures. This is rare; documented in the trait.

Unification: one method, three shapes

intercept_streaming(req: StreamRequest, inbound: PayloadStream, next: NextStream<'_>) -> Result<StreamResponse, ConnectError> covers server-streaming, client-streaming, and bidi by treating "one" as "stream of one." The terminal does the un-unification:

  • Server-streaming: terminal pulls the single item out of the (1-item) inbound stream and calls dispatcher.call_server_streaming(path, ctx, body, format). The dispatcher's outbound Stream is the response.
  • Client-streaming: terminal passes the inbound stream straight to dispatcher.call_client_streaming(...) and wraps the single EncodedResponse as a 1-item outbound stream; call_client_streaming_intercepted collapses it back.
  • Bidi: passthrough both directions.

The Dispatcher trait, codegen, and handler signatures are unchanged — the unify/un-unify lives entirely in the new call_*_streaming_intercepted wrappers, the same way call_unary_intercepted slots into the existing call_unary contract.

StreamResponse = Response<PayloadStream> carries response headers, trailers, and the compression hint alongside the body stream — same as UnaryResponse = Response<Payload> does for unary. The interceptor can set or rewrite all of them.

Cost

Each call_*_streaming_intercepted has the same is_empty() fast path as call_unary_intercepted: a service with no interceptors pays one branch, no PayloadStream, no NextStream, no per-item Payload construction.

For a registered chain, each stream item is wrapped in Payload::new(bytes, format) — one struct construction (a Bytes move + an empty OnceLock, no allocation). An interceptor that never reads message bodies pays only that. An interceptor that does read calls payload.message::<M>() per item, which decodes once and caches.

Public surface

  • Interceptor::intercept_streaming (default passthrough).
  • StreamRequest, StreamResponse, PayloadStream, NextStream — streaming counterparts of UnaryRequest/UnaryResponse/Next.
  • streaming_interceptor closure helper, run_chain_streaming test helper.
  • with_interceptor doc updated to drop the "Streaming calls bypass the chain" caveat.

Confidence

  • 9 unit tests in interceptor.rs: ordering (outermost-first), short-circuit with diagnostic headers, passthrough preserves items + metadata, inbound Stream adapter wrapping, outbound Stream adapter wrapping, closure helper, empty-chain fast path (pointer-equality on the echoed bytes), error-header propagation through call_*_streaming_intercepted without invoking the dispatcher, and the un-unification round trip through a passthrough chain.
  • 2 e2e tests in tests/streaming/: a recording interceptor wrapping all three streaming shapes through axum + the typed client (asserting it sees path() and stamps response headers), and a denying interceptor whose error reaches the client as a structured permission_denied (via stream.error() for server-streaming, via the call result for client-streaming).
  • 338 lib + 22 e2e tests, cargo fmt, cargo clippy --all-targets -- -D warnings (lib + test crate), cargo doc clean (no new warnings). The conformance suite runs zero interceptors and is unaffected.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented May 19, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Server-streaming, client-streaming, and bidi-streaming RPCs bypassed
the interceptor chain added in #114. For an auth interceptor that's a
vulnerability — a streaming RPC that skips authorization — not a gap.
This adds Interceptor::intercept_streaming covering all three shapes.

The streaming surface is Stream-shaped, not connection-shaped.
connect-go exposes a StreamingHandlerConn with Receive()/Send() because
Go handlers push: they call stream.Send(res). Rust handlers produce:
they return a Stream the framework polls. There is no per-item send()
call site to hook, so a conn wrapper would need a channel intermediary
plus a pump task per stream. Wrapping the inbound and outbound streams
with adapters has the same expressive power without that cost, and
matches the rest of the Rust ecosystem.

A single intercept_streaming method covers all three streaming shapes
by treating one as a stream of one. The interceptor receives an inbound
PayloadStream, calls next.run(req, inbound), and gets back a
StreamResponse carrying the outbound PayloadStream and response
metadata. The terminal does the un-unification: server-streaming pulls
the single inbound item out before calling the dispatcher;
client-streaming collapses the dispatcher's single response into a
1-item outbound stream and the call_*_intercepted wrapper pulls it back
out. The Dispatcher trait, codegen, and handler signatures are
unchanged.

Public surface:
- Interceptor::intercept_streaming, default passthrough.
- StreamRequest, StreamResponse (Response<PayloadStream>),
  PayloadStream, NextStream — streaming counterparts of UnaryRequest,
  UnaryResponse, Next.
- streaming_interceptor closure helper, run_chain_streaming test
  helper — counterparts of unary_interceptor / run_chain.

Each call_*_streaming_intercepted has the same is_empty() fast path as
call_unary_intercepted: a service with no interceptors pays nothing
beyond a single branch — no PayloadStream, no NextStream, no per-item
Payload construction.

A streaming interceptor that returns Err before next.run() short-
circuits before the handler runs; the dispatch path renders it as an
EndStreamResponse envelope (Connect) or grpc-status trailer (gRPC),
including any response_headers the error carries.
@iainmcgin
iainmcgin force-pushed the iain/streaming-interceptors branch from 43015ce to 2f237a1 Compare May 20, 2026 00:59
@iainmcgin
iainmcgin marked this pull request as ready for review May 20, 2026 01:38
@iainmcgin
iainmcgin requested a review from aknott-ant May 20, 2026 01:38
@iainmcgin
iainmcgin enabled auto-merge (squash) May 20, 2026 01:39
@iainmcgin
iainmcgin requested a review from azdagron May 20, 2026 01:39
@iainmcgin
iainmcgin merged commit 1e4fee6 into main May 20, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants