Repository navigation
dispatcher: pass Payload to call_unary so handlers reuse the interceptor decode - #119
Merged
Merged
Conversation
…tor decode When an interceptor reads the request body via Payload::message(), the decode is cached. The terminal then stripped the Payload back to raw bytes (Payload::encoded()) and handed them to Dispatcher::call_unary, which decoded the same bytes a second time for the handler. The interceptor's cache was discarded. Pass the Payload through the dispatcher contract instead: - Dispatcher::call_unary takes Payload, not Bytes. ErasedHandler and the Router/codegen impls follow. - Payload::take_message<M>() consumes the Payload and returns an owned M: the cached decode if one exists (no re-decode), the replacement if set_message was called, or a fresh decode otherwise. Owned-message handlers (Router::route) call this. - AnyMessage gains into_any(self: Box<Self>) -> Box<dyn Any> so a cached Box<dyn AnyMessage> can be downcast and moved out by value. Breaking for manual AnyMessage impls (the doc says "you will almost never" write one); the blanket impl covers every codegen message. - Generated dispatchers and zero-copy view handlers call Payload::encoded() — the cache stores owned messages, not views, so it cannot help the view path; encoded() is a refcounted Bytes clone for the common no-replacement case. - The empty-interceptor-chain fast path keeps its no-overhead invariant: Payload::new is plain struct construction (no allocation, no decode). Behavioral change worth noting: an interceptor that decodes the body as the wrong message type used to fail silently — the handler decoded the wire bytes independently and never saw the interceptor's mistake. Now Payload::take_message surfaces the wrong-type cache as an Internal error. This is the intended behavior: interceptors and handlers for the same RPC must agree on the message types, and a mismatch should be loud. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
All contributors have signed the CLA ✍️ ✅ |
iainmcgin
marked this pull request as ready for review
May 19, 2026 22:33
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
When an interceptor reads the request body via
Payload::message::<M>(), the decode is cached inPayload'sOnceLock.DispatchTerminal::callthen stripped thePayloadback to rawBytes(payload.encoded()) and handed them toDispatcher::call_unary, which decoded the same bytes a second time for the handler. The interceptor's cache was discarded — every body-inspecting interceptor pays for the decode twice.What
Pass the
Payloadthrough the dispatcher contract so the handler's decode can reuse the interceptor's cache.Dispatcher::call_unarytakesPayload, notBytes.ErasedHandlerand theRouter/codegen impls follow. Pre-1.0 breaking change.Payload::take_message::<M>()consumesselfand returns an ownedM: the cached decode if one exists (no re-decode, theBox<M>is unboxed in place), the replacement ifset_messagewas called, or a fresh decode otherwise. Owned-message handlers (Router::route) call this.AnyMessage::into_any(self: Box<Self>) -> Box<dyn Any>so a cachedBox<dyn AnyMessage>can be downcast and moved out by value. Breaking for manualAnyMessageimpls (the doc says "you will almost never" write one); the blanket impl covers every codegen message.Payload::encoded()— the cache stores owned messages, not views, so it cannot help the view path.encoded()is a refcountedBytesclone for the common no-replacement case, the same cost as before.Payload::newis plain struct construction (no allocation, no decode).Behavioral change worth noting
Before this PR, an interceptor that decoded the body as the wrong message type failed silently: the handler decoded the wire bytes independently and never saw the interceptor's mistake. Now
Payload::take_messagesurfaces the wrong-type cache as anInternalerror at the handler. This is the intended behavior — interceptors and handlers for the same RPC must agree on the message types — but it's a change. Thetake_message_wrong_cached_type_errorstest pins it.Confidence
Payload::take_messageunit tests (fresh decode, cache reuse, replacement, wrong cache type, wrong replacement type, decode error code).dispatch_terminal_forwards_payload_to_handlerinterceptor test: hands the dispatcher aPayloadwhose wire bytes are garbage but whose cache holds a replacement — if the terminal stripped thePayloadto bytes (the old behavior), the dispatcher's decode would error; if it forwards thePayload, the dispatcher sees the replacement.connectrpclib tests, 20streaming-teste2e tests pass.cargo clippy --workspace --all-targets -- -D warningsclean.cargo fmtclean.cargo docintroduces no new warnings.task generate:allregenerated all checked-in*.__connect.rsfiles; the diff there is just therequest: Payloadsignature change and therequest.encoded()?call.🤖 Generated with Claude Code