Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions benches/rpc/src/generated/connect/bench.__connect.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benches/rpc/src/generated/connect/bench_noutf8.__connect.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benches/rpc/src/generated/connect/echo_bloat.__connect.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benches/rpc/src/generated/connect/filter.__connect.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benches/rpc/src/generated/connect/fortune.__connect.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions connectrpc-codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,11 @@ fn generate_service_server(
#method_name => {
let svc = ::std::sync::Arc::clone(&self.inner);
Box::pin(async move {
let req = ::connectrpc::dispatcher::codegen::decode_request_view::<#input_view>(request, format)?;
// Generated handlers are view-based, so the owned-message
// cache an interceptor may have populated cannot be reused.
// `encoded()` returns the (post-replacement) wire bytes —
// a cheap `Bytes` clone for the common no-replacement case.
let req = ::connectrpc::dispatcher::codegen::decode_request_view::<#input_view>(request.encoded()?, format)?;
svc.#method_snake(ctx, req).await?.encode::<#output_type>(format)
})
}
Expand Down Expand Up @@ -1556,7 +1560,7 @@ fn generate_service_server(
&self,
path: &str,
ctx: ::connectrpc::RequestContext,
request: ::buffa::bytes::Bytes,
request: ::connectrpc::Payload,
format: ::connectrpc::CodecFormat,
) -> ::connectrpc::dispatcher::codegen::UnaryResult {
let Some(method) = path.strip_prefix(#path_prefix) else {
Expand Down
21 changes: 16 additions & 5 deletions connectrpc/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::codec::CodecFormat;
use crate::error::ConnectError;
use crate::handler::BoxFuture;
use crate::handler::BoxStream;
use crate::payload::Payload;
use crate::response::{EncodedResponse, RequestContext};
use crate::router::MethodKind;
use crate::spec::Spec;
Expand Down Expand Up @@ -152,14 +153,24 @@ pub trait Dispatcher: Send + Sync + 'static {

/// Dispatch a unary call.
///
/// The caller decodes the request body to raw bytes (after envelope
/// stripping / decompression), and the dispatcher decodes it to the
/// concrete request type, invokes the handler, and encodes the response.
/// The caller wraps the body bytes (after envelope stripping /
/// decompression) in a [`Payload`], and the dispatcher decodes it to
/// the concrete request type, invokes the handler, and encodes the
/// response.
///
/// The `request` is a [`Payload`] rather than raw `Bytes` so a
/// dispatcher backing an owned-message handler can call
/// [`Payload::take_message`] and reuse the decode an interceptor may
/// already have cached, instead of decoding the same bytes twice.
/// Dispatchers backing zero-copy view handlers call
/// [`Payload::encoded`] to recover the (post-replacement) wire bytes
/// — the cache stores owned messages, not views, so it cannot help
/// the view path.
fn call_unary(
&self,
path: &str,
ctx: RequestContext,
request: Bytes,
request: Payload,
format: CodecFormat,
) -> UnaryResult;

Expand Down Expand Up @@ -255,7 +266,7 @@ impl<A: Dispatcher, B: Dispatcher> Dispatcher for Chain<A, B> {
&self,
path: &str,
ctx: RequestContext,
request: Bytes,
request: Payload,
format: CodecFormat,
) -> UnaryResult {
if self.0.lookup(path).is_some() {
Expand Down
21 changes: 15 additions & 6 deletions connectrpc/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ where

/// Type-erased unary handler for use in the router.
pub(crate) trait ErasedHandler: Send + Sync {
/// Handle a request with raw bytes and specified codec format.
/// Handle a request, decoding the [`Payload`] to the concrete request
/// type. Owned-message handlers should call [`Payload::take_message`]
/// to reuse a decode an interceptor may already have cached; view
/// handlers should call [`Payload::encoded`] for the wire bytes.
fn call_erased(
&self,
ctx: RequestContext,
request: Bytes,
request: crate::Payload,
format: CodecFormat,
) -> BoxFuture<'static, Result<EncodedResponse, ConnectError>>;

Expand Down Expand Up @@ -241,12 +244,14 @@ where
fn call_erased(
&self,
ctx: RequestContext,
request: Bytes,
request: crate::Payload,
format: CodecFormat,
) -> BoxFuture<'static, Result<EncodedResponse, ConnectError>> {
let handler = Arc::clone(&self.handler);
Box::pin(async move {
let req: Req = decode_request(&request, format)?;
// `take_message` reuses an interceptor's decode when one ran
// and cached this `Req`, instead of decoding the bytes again.
let req: Req = request.take_message()?;
handler.call(ctx, req).await?.encode::<Res>(format)
})
}
Expand Down Expand Up @@ -783,12 +788,16 @@ where
fn call_erased(
&self,
ctx: RequestContext,
request: Bytes,
request: crate::Payload,
format: CodecFormat,
) -> BoxFuture<'static, Result<EncodedResponse, ConnectError>> {
let handler = Arc::clone(&self.handler);
Box::pin(async move {
let req = decode_request_view::<ReqView>(request, format)?;
// The cache stores owned messages, not views, so it can't help
// here. `encoded()` is the wire bytes — a cheap `Bytes` clone
// unless an interceptor replaced the body, in which case it
// re-encodes the replacement.
let req = decode_request_view::<ReqView>(request.encoded()?, format)?;
handler.call(ctx, req, format).await
})
}
Expand Down
Loading
Loading