Skip to content

Commit

Permalink
fix(transport): Update builders to move self (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
alce authored and LucioFranco committed Nov 11, 2019
1 parent 4490812 commit 85ef18f
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 112 deletions.
5 changes: 2 additions & 3 deletions tonic-examples/src/gcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let tls_config = ClientTlsConfig::with_rustls()
.ca_certificate(Certificate::from_pem(certs.as_slice()))
.domain_name("pubsub.googleapis.com")
.clone();
.domain_name("pubsub.googleapis.com");

let channel = Channel::from_static(ENDPOINT)
.intercept_headers(move |headers| {
headers.insert("authorization", header_value.clone());
})
.tls_config(&tls_config)
.tls_config(tls_config)
.connect()
.await?;

Expand Down
5 changes: 2 additions & 3 deletions tonic-examples/src/tls/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let tls = ClientTlsConfig::with_rustls()
.ca_certificate(ca)
.domain_name("example.com")
.clone();
.domain_name("example.com");

let channel = Channel::from_static("http://[::1]:50051")
.tls_config(&tls)
.tls_config(tls)
.connect()
.await?;

Expand Down
1 change: 0 additions & 1 deletion tonic-examples/src/tls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

Server::builder()
.tls_config(ServerTlsConfig::with_rustls().identity(identity))
.clone()
.add_service(pb::server::EchoServer::new(server))
.serve(addr)
.await?;
Expand Down
5 changes: 2 additions & 3 deletions tonic-examples/src/tls_client_auth/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let tls = ClientTlsConfig::with_rustls()
.domain_name("localhost")
.ca_certificate(server_root_ca_cert)
.identity(client_identity)
.clone();
.identity(client_identity);

let channel = Channel::from_static("http://[::1]:50051")
.tls_config(&tls)
.tls_config(tls)
.connect()
.await?;

Expand Down
5 changes: 2 additions & 3 deletions tonic-examples/src/tls_client_auth/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let tls = ServerTlsConfig::with_rustls()
.identity(server_identity)
.client_ca_root(client_ca_cert)
.clone();
.client_ca_root(client_ca_cert);

Server::builder()
.tls_config(&tls)
.tls_config(tls)
.add_service(pb::server::EchoServer::new(server))
.serve(addr)
.await?;
Expand Down
9 changes: 4 additions & 5 deletions tonic-interop/src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[allow(unused_mut)]
let mut endpoint = Endpoint::from_static("http://localhost:10000")
.timeout(Duration::from_secs(5))
.concurrency_limit(30)
.clone();
.concurrency_limit(30);

if matches.use_tls {
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
{
panic!("No TLS libary feature selected");
panic!("No TLS library feature selected");
}

#[cfg(feature = "tls_rustls")]
{
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
let ca = Certificate::from_pem(pem);
endpoint.tls_config(
endpoint = endpoint.tls_config(
ClientTlsConfig::with_rustls()
.ca_certificate(ca)
.domain_name("foo.test.google.fr"),
Expand All @@ -54,7 +53,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
{
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
let ca = Certificate::from_pem(pem);
endpoint.tls_config(
endpoint = endpoint.tls_config(
ClientTlsConfig::with_openssl()
.ca_certificate(ca)
.domain_name("foo.test.google.fr"),
Expand Down
54 changes: 26 additions & 28 deletions tonic-interop/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

let addr = "127.0.0.1:10000".parse().unwrap();

let mut builder = Server::builder();

if matches.use_tls {
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
{
panic!("No TLS libary feature selected");
}

#[cfg(feature = "tls_rustls")]
{
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
let identity = Identity::from_pem(cert, key);

builder.tls_config(ServerTlsConfig::with_rustls().identity(identity));
}

#[cfg(feature = "tls_openssl")]
{
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
let identity = Identity::from_pem(cert, key);

builder.tls_config(ServerTlsConfig::with_openssl().identity(identity));
}
}

builder.interceptor_fn(|svc, req| {
let mut builder = Server::builder().interceptor_fn(|svc, req| {
let echo_header = req
.headers()
.get("x-grpc-test-echo-initial")
Expand Down Expand Up @@ -76,6 +49,31 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
}
});

if matches.use_tls {
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
{
panic!("No TLS library feature selected");
}

#[cfg(feature = "tls_rustls")]
{
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
let identity = Identity::from_pem(cert, key);

builder = builder.tls_config(ServerTlsConfig::with_rustls().identity(identity));
}

#[cfg(feature = "tls_openssl")]
{
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
let identity = Identity::from_pem(cert, key);

builder = builder.tls_config(ServerTlsConfig::with_openssl().identity(identity));
}
}

let test_service = server::TestServiceServer::new(server::TestService::default());
let unimplemented_service =
server::UnimplementedServiceServer::new(server::UnimplementedService::default());
Expand Down
99 changes: 60 additions & 39 deletions tonic/src/transport/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ impl Endpoint {
/// # let mut builder = Endpoint::from_static("https://example.com");
/// builder.timeout(Duration::from_secs(5));
/// ```
pub fn timeout(&mut self, dur: Duration) -> &mut Self {
self.timeout = Some(dur);
self
pub fn timeout(self, dur: Duration) -> Self {
Endpoint {
timeout: Some(dur),
..self
}
}

/// Apply a concurrency limit to each request.
Expand All @@ -88,9 +90,11 @@ impl Endpoint {
/// # let mut builder = Endpoint::from_static("https://example.com");
/// builder.concurrency_limit(256);
/// ```
pub fn concurrency_limit(&mut self, limit: usize) -> &mut Self {
self.concurrency_limit = Some(limit);
self
pub fn concurrency_limit(self, limit: usize) -> Self {
Endpoint {
concurrency_limit: Some(limit),
..self
}
}

/// Apply a rate limit to each request.
Expand All @@ -101,9 +105,11 @@ impl Endpoint {
/// # let mut builder = Endpoint::from_static("https://example.com");
/// builder.rate_limit(32, Duration::from_secs(1));
/// ```
pub fn rate_limit(&mut self, limit: u64, duration: Duration) -> &mut Self {
self.rate_limit = Some((limit, duration));
self
pub fn rate_limit(self, limit: u64, duration: Duration) -> Self {
Endpoint {
rate_limit: Some((limit, duration)),
..self
}
}

/// Sets the [`SETTINGS_INITIAL_WINDOW_SIZE`][spec] option for HTTP2
Expand All @@ -112,33 +118,41 @@ impl Endpoint {
/// Default is 65,535
///
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_INITIAL_WINDOW_SIZE
pub fn initial_stream_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
self.init_stream_window_size = sz.into();
self
pub fn initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> Self {
Endpoint {
init_stream_window_size: sz.into(),
..self
}
}

/// Sets the max connection-level flow control for HTTP2
///
/// Default is 65,535
pub fn initial_connection_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
self.init_connection_window_size = sz.into();
self
pub fn initial_connection_window_size(self, sz: impl Into<Option<u32>>) -> Self {
Endpoint {
init_connection_window_size: sz.into(),
..self
}
}

/// Intercept outbound HTTP Request headers;
pub fn intercept_headers<F>(&mut self, f: F) -> &mut Self
pub fn intercept_headers<F>(self, f: F) -> Self
where
F: Fn(&mut http::HeaderMap) + Send + Sync + 'static,
{
self.interceptor_headers = Some(Arc::new(f));
self
Endpoint {
interceptor_headers: Some(Arc::new(f)),
..self
}
}

/// Configures TLS for the endpoint.
#[cfg(feature = "tls")]
pub fn tls_config(&mut self, tls_config: &ClientTlsConfig) -> &mut Self {
self.tls = Some(tls_config.tls_connector(self.uri.clone()).unwrap());
self
pub fn tls_config(self, tls_config: ClientTlsConfig) -> Self {
Endpoint {
tls: Some(tls_config.tls_connector(self.uri.clone()).unwrap()),
..self
}
}

/// Create a channel from this config.
Expand Down Expand Up @@ -262,48 +276,55 @@ impl ClientTlsConfig {
///
/// This has no effect if `rustls_client_config` or `openssl_connector` is used to configure
/// Rustls or OpenSSL respectively.
pub fn domain_name(&mut self, domain_name: impl Into<String>) -> &mut Self {
self.domain = Some(domain_name.into());
self
pub fn domain_name(self, domain_name: impl Into<String>) -> Self {
ClientTlsConfig {
domain: Some(domain_name.into()),
..self
}
}

/// Sets the CA Certificate against which to verify the server's TLS certificate.
///
/// This has no effect if `rustls_client_config` or `openssl_connector` is used to configure
/// Rustls or OpenSSL respectively.
pub fn ca_certificate(&mut self, ca_certificate: Certificate) -> &mut Self {
self.cert = Some(ca_certificate);
self
pub fn ca_certificate(self, ca_certificate: Certificate) -> Self {
ClientTlsConfig {
cert: Some(ca_certificate),
..self
}
}

/// Sets the client identity to present to the server.
///
/// This has no effect if `rustls_client_config` or `openssl_connector` is used to configure
/// Rustls or OpenSSL respectively.
pub fn identity(&mut self, identity: Identity) -> &mut Self {
self.identity = Some(identity);
self
pub fn identity(self, identity: Identity) -> Self {
ClientTlsConfig {
identity: Some(identity),
..self
}
}

/// Use options specified by the given `SslConnector` to configure TLS.
///
/// This overrides all other TLS options set via other means.
#[cfg(feature = "openssl")]
pub fn openssl_connector(&mut self, connector: openssl1::ssl::SslConnector) -> &mut Self {
self.openssl_raw = Some(connector);
self
pub fn openssl_connector(self, connector: openssl1::ssl::SslConnector) -> Self {
ClientTlsConfig {
openssl_raw: Some(connector),
..self
}
}

/// Use options specified by the given `ClientConfig` to configure TLS.
///
/// This overrides all other TLS options set via other means.
#[cfg(feature = "rustls")]
pub fn rustls_client_config(
&mut self,
config: tokio_rustls::rustls::ClientConfig,
) -> &mut Self {
self.rustls_raw = Some(config);
self
pub fn rustls_client_config(self, config: tokio_rustls::rustls::ClientConfig) -> Self {
ClientTlsConfig {
rustls_raw: Some(config),
..self
}
}

fn tls_connector(&self, uri: Uri) -> Result<TlsConnector, crate::Error> {
Expand Down
Loading

0 comments on commit 85ef18f

Please sign in to comment.