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

feat: Add Grpc::with_origin for clients #1017

Merged
merged 1 commit into from Jun 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 3 additions & 23 deletions examples/src/tls/client_rustls.rs
Expand Up @@ -48,30 +48,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let client = hyper::Client::builder().build(connector);

// Hyper expects an absolute `Uri` to allow it to know which server to connect too.
// Currently, tonic's generated code only sets the `path_and_query` section so we
// are going to write a custom tower layer in front of the hyper client to add the
// scheme and authority.
//
// Again, this Uri is `example.com` because our tls certs is signed with this SNI but above
// we actually map this back to `[::1]:50051` before the `Uri` is passed to hyper's `HttpConnector`
// to allow it to correctly establish the tcp connection to the local `tls-server`.
// Using `with_origin` will let the codegenerated client set the `scheme` and
// `authority` from the porvided `Uri`.
let uri = Uri::from_static("https://example.com");
let svc = tower::ServiceBuilder::new()
.map_request(move |mut req: http::Request<tonic::body::BoxBody>| {
let uri = Uri::builder()
.scheme(uri.scheme().unwrap().clone())
.authority(uri.authority().unwrap().clone())
.path_and_query(req.uri().path_and_query().unwrap().clone())
.build()
.unwrap();

*req.uri_mut() = uri;
req
})
.service(client);

let mut client = EchoClient::new(svc);
let mut client = EchoClient::with_origin(client, uri);
Copy link

Choose a reason for hiding this comment

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

This replaces most of the “this feels wrong” when migrating from Tonic 0.6 to 0.7 while using rustls. Much more ergonomic and friendly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok perfect, my 1.0 roadmap would change this to be the new fn since you can always provide an empty uri. This was exactly the same thought I had when I wrote that example so glad that this fixes most of that :)


let request = tonic::Request::new(EchoRequest {
message: "hello".into(),
Expand Down
6 changes: 6 additions & 0 deletions tonic-build/src/client.rs
Expand Up @@ -44,6 +44,7 @@ pub fn generate<T: Service>(
clippy::let_unit_value,
)]
use tonic::codegen::*;
use tonic::codegen::http::Uri;

#service_doc
#(#struct_attributes)*
Expand All @@ -66,6 +67,11 @@ pub fn generate<T: Service>(
Self { inner }
}

pub fn with_origin(inner: T, origin: Uri) -> Self {
let inner = tonic::client::Grpc::with_origin(inner, origin);
Self { inner }
}

pub fn with_interceptor<F>(inner: T, interceptor: F) -> #service_ident<InterceptedService<T, F>>
where
F: tonic::service::Interceptor,
Expand Down
23 changes: 23 additions & 0 deletions tonic/src/client/grpc.rs
Expand Up @@ -30,6 +30,7 @@ use std::fmt;
/// [gRPC protocol definition]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
pub struct Grpc<T> {
inner: T,
origin: Uri,
/// Which compression encodings does the client accept?
accept_compression_encodings: EnabledCompressionEncodings,
/// The compression encoding that will be applied to requests.
Expand All @@ -41,6 +42,20 @@ impl<T> Grpc<T> {
pub fn new(inner: T) -> Self {
Self {
inner,
origin: Uri::default(),
send_compression_encodings: None,
accept_compression_encodings: EnabledCompressionEncodings::default(),
}
}

/// Creates a new gRPC client with the provided [`GrpcService`] and `Uri`.
///
/// The provided Uri will use only the scheme and authority parts as the
/// path_and_query portion will be set for each method.
pub fn with_origin(inner: T, origin: Uri) -> Self {
Self {
inner,
origin,
send_compression_encodings: None,
accept_compression_encodings: EnabledCompressionEncodings::default(),
}
Expand Down Expand Up @@ -211,8 +226,13 @@ impl<T> Grpc<T> {
M1: Send + Sync + 'static,
M2: Send + Sync + 'static,
{
let scheme = self.origin.scheme().cloned();
let authority = self.origin.authority().cloned();

let mut parts = Parts::default();
parts.path_and_query = Some(path);
parts.scheme = scheme;
parts.authority = authority;

let uri = Uri::from_parts(parts).expect("path_and_query only is valid Uri");

Expand Down Expand Up @@ -296,6 +316,7 @@ impl<T: Clone> Clone for Grpc<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
origin: self.origin.clone(),
Copy link

Choose a reason for hiding this comment

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

While I cannot judge whether this clone or the ServiceBuilder’s clone would be more efficient, this tradeoff with the scheme and authority being owned by the Grpc seems right to optimize for the expected use cases, making it clear how to replace Channels going forward without needing to learn Tower as well just to get started.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well I think the structure works as follows we clone the server which just contains the client then this struct constructs you origin + grpc path + body and submits that to the service.

send_compression_encodings: self.send_compression_encodings,
accept_compression_encodings: self.accept_compression_encodings,
}
Expand All @@ -308,6 +329,8 @@ impl<T: fmt::Debug> fmt::Debug for Grpc<T> {

f.field("inner", &self.inner);

f.field("origin", &self.origin);

f.field("compression_encoding", &self.send_compression_encodings);

f.field(
Expand Down