Skip to content

dispatcher: pass Payload to call_unary so handlers reuse the interceptor decode - #119

Merged
iainmcgin merged 2 commits into
mainfrom
iain/payload-handler
May 20, 2026
Merged

iainmcgin merged 2 commits into
mainfrom
iain/payload-handler

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Why

When an interceptor reads the request body via Payload::message::<M>(), the decode is cached in Payload's OnceLock. DispatchTerminal::call 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 — every body-inspecting interceptor pays for the decode twice.

What

Pass the Payload through the dispatcher contract so the handler's decode can reuse the interceptor's cache.

  • Dispatcher::call_unary takes Payload, not Bytes. ErasedHandler and the Router/codegen impls follow. Pre-1.0 breaking change.
  • Payload::take_message::<M>() consumes self and returns an owned M: the cached decode if one exists (no re-decode, the Box<M> is unboxed in place), the replacement if set_message was called, or a fresh decode otherwise. Owned-message handlers (Router::route) call this.
  • AnyMessage::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 same cost as before.
  • 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

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_message surfaces the wrong-type cache as an Internal error 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. The take_message_wrong_cached_type_errors test pins it.

Confidence

  • 6 new Payload::take_message unit tests (fresh decode, cache reuse, replacement, wrong cache type, wrong replacement type, decode error code).
  • New dispatch_terminal_forwards_payload_to_handler interceptor test: hands the dispatcher a Payload whose wire bytes are garbage but whose cache holds a replacement — if the terminal stripped the Payload to bytes (the old behavior), the dispatcher's decode would error; if it forwards the Payload, the dispatcher sees the replacement.
  • 336 connectrpc lib tests, 20 streaming-test e2e tests pass. cargo clippy --workspace --all-targets -- -D warnings clean. cargo fmt clean. cargo doc introduces no new warnings.
  • task generate:all regenerated all checked-in *.__connect.rs files; the diff there is just the request: Payload signature change and the request.encoded()? call.

🤖 Generated with Claude Code

…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>
@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.

@iainmcgin
iainmcgin marked this pull request as ready for review May 19, 2026 22:33
@iainmcgin
iainmcgin requested a review from aknott-ant May 19, 2026 22:35

@aknott-ant aknott-ant left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@iainmcgin
iainmcgin enabled auto-merge (squash) May 20, 2026 00:44
@iainmcgin
iainmcgin merged commit 12f1a8b 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