Unable to access payloads for reflection from interceptors without extra decode

Open
#358 0 comments 0 reactions 0 assignees View on GitHub

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

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 its Router route and on the generated server's MethodDescriptor. Dispatch copies them into the RequestContext.
  • reflect_request decodes the payload as the method's view, caches the view in the Payload, 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 with to_owned_message.
  • reflect_response does 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. A reflect_all_message_types option records the rest, such as protoc-gen-connect-rust output or extern-mapped types. For a type that isn't recorded, reflect_request returns None.

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 a Spec holding a function pointer can no longer be used as a match pattern, 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/reflect crate generates a service with vtable reflection and checks every method kind through both the generated register() routes and the generated FooServiceServer. An internal decode counter shows that an interceptor reading every request adds no decodes. Bypassing the cache makes these tests fail.
  • Dispatch tests in connectrpc cover 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from connectrpc/connect-rust

All issues in connectrpc/connect-rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.