Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow futures on Serve and Stub to be Send #448

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions tarpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ tracing = { version = "0.1", default-features = false, features = [
"log",
] }
tracing-opentelemetry = { version = "0.22.0", default-features = false }
trait-variant = "0.1.1"
opentelemetry = { version = "0.21.0", default-features = false }


Expand Down
16 changes: 2 additions & 14 deletions tarpc/src/client/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use crate::{
client::{Channel, RpcError},
context,
server::Serve,
RequestName,
context, RequestName,
};

pub mod load_balance;
Expand All @@ -16,6 +14,7 @@ mod mock;
/// A connection to a remote service.
/// Calls the service with requests of type `Req` and receives responses of type `Resp`.
#[allow(async_fn_in_trait)]
#[trait_variant::make(TokioStub: Send)]
pub trait Stub {
/// The service request type.
type Req: RequestName;
Expand All @@ -39,14 +38,3 @@ where
Self::call(self, ctx, request).await
}
}

impl<S> Stub for S
where
S: Serve + Clone,
{
type Req = S::Req;
type Resp = S::Resp;
async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
self.clone().serve(ctx, req).await.map_err(RpcError::Server)
}
}
33 changes: 33 additions & 0 deletions tarpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

//! Provides a server that concurrently handles many connections sending multiplexed requests.

use crate::client::stub::Stub;
use crate::client::RpcError;
use crate::{
cancellations::{cancellations, CanceledRequests, RequestCancellation},
context::{self, SpanExt},
Expand Down Expand Up @@ -66,6 +68,27 @@ impl Config {
}
}

/// A [`Stub`] implementation that simply warps a `Serve`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: typo

Suggested change
/// A [`Stub`] implementation that simply warps a `Serve`.
/// A [`Stub`] implementation that simply wraps a `Serve`.

pub struct ServeStub<S> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe keep this type private for now and return an imp Stub? Can always make it public later if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea

serve: S,
}

impl<S> Stub for ServeStub<S>
where
S: Serve + Clone,
{
type Req = <S as Serve>::Req;
type Resp = <S as Serve>::Resp;

async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
self.serve
.clone()
.serve(ctx, req)
.await
.map_err(RpcError::Server)
}
}

/// Equivalent to a `FnOnce(Req) -> impl Future<Output = Resp>`.
#[allow(async_fn_in_trait)]
pub trait Serve {
Expand All @@ -77,6 +100,16 @@ pub trait Serve {

/// Responds to a single request.
async fn serve(self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, ServerError>;

/// Wrap this `Serve` in a type that implements [`Stub`].
async fn into_stub(self) -> ServeStub<Self>
where
Self: Clone,
{
ServeStub {
serve: self.clone(),
}
}
}

/// A Serve wrapper around a Fn.
Expand Down
Loading