Unable to access payloads for reflection from interceptors without extra decode
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- grpc, rust
- Domain
- api, backend-api-design
Research direction
Start by reviewing the proof of concept branch and the generated Router, MethodDescriptor, RequestContext, and Payload paths described in the issue. Trace how unary and streaming dispatch pass payloads to handlers, then compare the proposed reflection cache behavior with the tests in tests/reflect and the connectrpc dispatch tests. Done means the API and codegen design resolve the listed trade-offs, cached views avoid duplicate request decodes, and relevant tests pass; the conformance suite has not been run.
Written by the indexing model from the issue text.
Description
When prototyping a protovalidate interceptor for connect-rust, I found no way for an interceptor to read a request message without knowing its Rust type. Payload::message::<M>() and Payload::view::<V>() need the concrete type, and Spec doesn't say what the message types are. A generic interceptor can look up the method's input type in a descriptor pool and decode the bytes into a DynamicMessage, but then every request is decoded twice: once by the interceptor, through the slower dynamic path, and again by the handler. Reading messages through reflection seems generally useful for interceptors (validation, logging, redaction), so it would be nice to support it without the extra decode.
Proof of concept
Branch: https://github.com/anuraaga/connect-rust/tree/spec-reflection
With a new reflect feature, an interceptor can read a request without naming its type:
async fn intercept_unary(
&self,
req: UnaryRequest,
next: Next<'_>,
) -> Result<UnaryResponse, ConnectError> {
if let Some(message) = req.ctx.reflect_request(&req.payload) {
self.validate(message?)?; // &dyn buffa_descriptor::reflect::ReflectMessage
}
next.run(req).await
}
How it works:
- Generated code records each method's request and response types, as a
MessageType, on itsRouterroute and on the generated server'sMethodDescriptor. Dispatch copies them into theRequestContext. reflect_requestdecodes the payload as the method's view, caches the view in thePayload, and returns it as a&dyn ReflectMessage. Views implement that trait with buffa's vtable reflection. Handlers then take the cached view instead of decoding again: view handlers use it directly, and owned-message handlers convert it withto_owned_message.reflect_responsedoes the same for responses. The server has already encoded those, so reading one decodes it again.- Codegen records a type only when it knows the view implements
ReflectMessage: it generated the type with vtable reflection, and the impls aren't gated behind a crate feature. Areflect_all_message_typesoption records the rest, such asprotoc-gen-connect-rustoutput or extern-mapped types. For a type that isn't recorded,reflect_requestreturnsNone.
This includes a breaking change. So that streaming handlers can reuse the cached views too, Dispatcher::call_server_streaming takes a Payload instead of Bytes, and RequestStream yields Payloads. That matches call_unary, which already takes a Payload. It also removes the step where the interceptor chain re-encoded payloads to bytes before dispatch.
Trade-offs and open questions
- I first put the types on
Spec, but aSpecholding a function pointer can no longer be used as amatchpattern, so they live next to it. There may be a better place. - When an interceptor reflects a request, the payload caches a view rather than an owned message, so owned-message handlers convert the view instead of reusing an owned decode.
- Without interceptors, each streamed message is now wrapped in a
Payload. I haven't benchmarked that. - Client interceptors aren't covered, since the client doesn't build a
RequestContext. - When the type isn't recorded, a validator has to choose between failing open and failing closed.
Testing
- A new
tests/reflectcrate generates a service with vtable reflection and checks every method kind through both the generatedregister()routes and the generatedFooServiceServer. An internal decode counter shows that an interceptor reading every request adds no decodes. Bypassing the cache makes these tests fail. - Dispatch tests in
connectrpccover unary (Connect and gRPC), server, client and bidi streaming. - I haven't run the conformance suite.
- Dominant language
- Rust
- Stars
- 544
- Forks
- 77
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from connectrpc/connect-rust
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
connectrpc/connect-rust#360 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 53/100
connectrpc/connect-rust#355 ·
-
Streaming request body errors are always reported as `internal`Possibly taken @AhmedElBanna80 claimed this 18 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
connectrpc/connect-rust#352 ·
-
Difficulty 3/5 Half a day Newbie friendliness 48/100
connectrpc/connect-rust#349 ·
-
Client fails 12 cases of connectconformance v1.0.7 (gRPC-Web bidi metadata, gRPC truncated server-stream message)Possibly taken @excavador claimed this 34 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 48/100
connectrpc/connect-rust#346 · 1 comment ·
All issues in connectrpc/connect-rust
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Choochmeque/tauri-plugin-webdriver#134 ·
Maintainers usually reply within 17 days
-
bug rust SDK & CLI & REST API
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
boxlite-ai/boxlite#3292 ·
Maintainers usually reply within 1 day
-
Android bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
anefzaoui/misterdownloadmanager#2516 ·
Maintainers usually reply within 1 day
-
editor ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
state:triage tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 89/100
agent8-cash/ethpayserver#731 ·
Maintainers usually reply within 1 day