Repository navigation
interceptor: streaming RPC support via Stream-shaped intercept_streaming - #121
Merged
Merged
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
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
force-pushed
the
iain/streaming-interceptors
branch
from
May 20, 2026 00:59
43015ce to
2f237a1
Compare
iainmcgin
marked this pull request as ready for review
May 20, 2026 01:38
iainmcgin
enabled auto-merge (squash)
May 20, 2026 01:39
azdagron
approved these changes
May 20, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
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 addsInterceptor::intercept_streamingcovering all three streaming shapes.Design:
Stream-shaped, not connection-shapedconnect-go exposes a
StreamingHandlerConnwithReceive()/Send()methods because Go handlers push: they callstream.Send(res)for each item. There's a per-itemSend()call site for the interceptor's wrapper to hook.connect-rust handlers produce: they return a
Streamthe framework polls. There is no per-itemsend()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 anmpscchannel 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 aStreamadapter: the interceptor wraps the inboundStream, callsnext.run, wraps the outboundStream, 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 howtower,tonic, andaxumall doStream-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:dispatcher.call_server_streaming(path, ctx, body, format). The dispatcher's outboundStreamis the response.dispatcher.call_client_streaming(...)and wraps the singleEncodedResponseas a 1-item outbound stream;call_client_streaming_interceptedcollapses it back.The
Dispatchertrait, codegen, and handler signatures are unchanged — the unify/un-unify lives entirely in the newcall_*_streaming_interceptedwrappers, the same waycall_unary_interceptedslots into the existingcall_unarycontract.StreamResponse = Response<PayloadStream>carries response headers, trailers, and the compression hint alongside the body stream — same asUnaryResponse = Response<Payload>does for unary. The interceptor can set or rewrite all of them.Cost
Each
call_*_streaming_interceptedhas the sameis_empty()fast path ascall_unary_intercepted: a service with no interceptors pays one branch, noPayloadStream, noNextStream, no per-itemPayloadconstruction.For a registered chain, each stream item is wrapped in
Payload::new(bytes, format)— one struct construction (aBytesmove + an emptyOnceLock, no allocation). An interceptor that never reads message bodies pays only that. An interceptor that does read callspayload.message::<M>()per item, which decodes once and caches.Public surface
Interceptor::intercept_streaming(default passthrough).StreamRequest,StreamResponse,PayloadStream,NextStream— streaming counterparts ofUnaryRequest/UnaryResponse/Next.streaming_interceptorclosure helper,run_chain_streamingtest helper.with_interceptordoc updated to drop the "Streaming calls bypass the chain" caveat.Confidence
interceptor.rs: ordering (outermost-first), short-circuit with diagnostic headers, passthrough preserves items + metadata, inboundStreamadapter wrapping, outboundStreamadapter wrapping, closure helper, empty-chain fast path (pointer-equality on the echoed bytes), error-header propagation throughcall_*_streaming_interceptedwithout invoking the dispatcher, and the un-unification round trip through a passthrough chain.tests/streaming/: a recording interceptor wrapping all three streaming shapes through axum + the typed client (asserting it seespath()and stamps response headers), and a denying interceptor whose error reaches the client as a structuredpermission_denied(viastream.error()for server-streaming, via the call result for client-streaming).cargo fmt,cargo clippy --all-targets -- -D warnings(lib + test crate),cargo docclean (no new warnings). The conformance suite runs zero interceptors and is unaffected.🤖 Generated with Claude Code