-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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(), | ||
} | ||
|
@@ -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"); | ||
|
||
|
@@ -296,6 +316,7 @@ impl<T: Clone> Clone for Grpc<T> { | |
fn clone(&self) -> Self { | ||
Self { | ||
inner: self.inner.clone(), | ||
origin: self.origin.clone(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I cannot judge whether this clone or the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
|
@@ -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( | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)