Skip to content

Commit 85ef18f

Browse files
alceLucioFranco
authored andcommitted
fix(transport): Update builders to move self (#132)
1 parent 4490812 commit 85ef18f

File tree

9 files changed

+144
-112
lines changed

9 files changed

+144
-112
lines changed

tonic-examples/src/gcp/client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2929

3030
let tls_config = ClientTlsConfig::with_rustls()
3131
.ca_certificate(Certificate::from_pem(certs.as_slice()))
32-
.domain_name("pubsub.googleapis.com")
33-
.clone();
32+
.domain_name("pubsub.googleapis.com");
3433

3534
let channel = Channel::from_static(ENDPOINT)
3635
.intercept_headers(move |headers| {
3736
headers.insert("authorization", header_value.clone());
3837
})
39-
.tls_config(&tls_config)
38+
.tls_config(tls_config)
4039
.connect()
4140
.await?;
4241

tonic-examples/src/tls/client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1212

1313
let tls = ClientTlsConfig::with_rustls()
1414
.ca_certificate(ca)
15-
.domain_name("example.com")
16-
.clone();
15+
.domain_name("example.com");
1716

1817
let channel = Channel::from_static("http://[::1]:50051")
19-
.tls_config(&tls)
18+
.tls_config(tls)
2019
.connect()
2120
.await?;
2221

tonic-examples/src/tls/server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6060

6161
Server::builder()
6262
.tls_config(ServerTlsConfig::with_rustls().identity(identity))
63-
.clone()
6463
.add_service(pb::server::EchoServer::new(server))
6564
.serve(addr)
6665
.await?;

tonic-examples/src/tls_client_auth/client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1616
let tls = ClientTlsConfig::with_rustls()
1717
.domain_name("localhost")
1818
.ca_certificate(server_root_ca_cert)
19-
.identity(client_identity)
20-
.clone();
19+
.identity(client_identity);
2120

2221
let channel = Channel::from_static("http://[::1]:50051")
23-
.tls_config(&tls)
22+
.tls_config(tls)
2423
.connect()
2524
.await?;
2625

tonic-examples/src/tls_client_auth/server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3939

4040
let tls = ServerTlsConfig::with_rustls()
4141
.identity(server_identity)
42-
.client_ca_root(client_ca_cert)
43-
.clone();
42+
.client_ca_root(client_ca_cert);
4443

4544
Server::builder()
46-
.tls_config(&tls)
45+
.tls_config(tls)
4746
.add_service(pb::server::EchoServer::new(server))
4847
.serve(addr)
4948
.await?;

tonic-interop/src/bin/client.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3030
#[allow(unused_mut)]
3131
let mut endpoint = Endpoint::from_static("http://localhost:10000")
3232
.timeout(Duration::from_secs(5))
33-
.concurrency_limit(30)
34-
.clone();
33+
.concurrency_limit(30);
3534

3635
if matches.use_tls {
3736
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
3837
{
39-
panic!("No TLS libary feature selected");
38+
panic!("No TLS library feature selected");
4039
}
4140

4241
#[cfg(feature = "tls_rustls")]
4342
{
4443
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
4544
let ca = Certificate::from_pem(pem);
46-
endpoint.tls_config(
45+
endpoint = endpoint.tls_config(
4746
ClientTlsConfig::with_rustls()
4847
.ca_certificate(ca)
4948
.domain_name("foo.test.google.fr"),
@@ -54,7 +53,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5453
{
5554
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
5655
let ca = Certificate::from_pem(pem);
57-
endpoint.tls_config(
56+
endpoint = endpoint.tls_config(
5857
ClientTlsConfig::with_openssl()
5958
.ca_certificate(ca)
6059
.domain_name("foo.test.google.fr"),

tonic-interop/src/bin/server.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
2121

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

24-
let mut builder = Server::builder();
25-
26-
if matches.use_tls {
27-
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
28-
{
29-
panic!("No TLS libary feature selected");
30-
}
31-
32-
#[cfg(feature = "tls_rustls")]
33-
{
34-
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
35-
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
36-
let identity = Identity::from_pem(cert, key);
37-
38-
builder.tls_config(ServerTlsConfig::with_rustls().identity(identity));
39-
}
40-
41-
#[cfg(feature = "tls_openssl")]
42-
{
43-
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
44-
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
45-
let identity = Identity::from_pem(cert, key);
46-
47-
builder.tls_config(ServerTlsConfig::with_openssl().identity(identity));
48-
}
49-
}
50-
51-
builder.interceptor_fn(|svc, req| {
24+
let mut builder = Server::builder().interceptor_fn(|svc, req| {
5225
let echo_header = req
5326
.headers()
5427
.get("x-grpc-test-echo-initial")
@@ -76,6 +49,31 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
7649
}
7750
});
7851

52+
if matches.use_tls {
53+
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
54+
{
55+
panic!("No TLS library feature selected");
56+
}
57+
58+
#[cfg(feature = "tls_rustls")]
59+
{
60+
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
61+
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
62+
let identity = Identity::from_pem(cert, key);
63+
64+
builder = builder.tls_config(ServerTlsConfig::with_rustls().identity(identity));
65+
}
66+
67+
#[cfg(feature = "tls_openssl")]
68+
{
69+
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
70+
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
71+
let identity = Identity::from_pem(cert, key);
72+
73+
builder = builder.tls_config(ServerTlsConfig::with_openssl().identity(identity));
74+
}
75+
}
76+
7977
let test_service = server::TestServiceServer::new(server::TestService::default());
8078
let unimplemented_service =
8179
server::UnimplementedServiceServer::new(server::UnimplementedService::default());

tonic/src/transport/endpoint.rs

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ impl Endpoint {
7676
/// # let mut builder = Endpoint::from_static("https://example.com");
7777
/// builder.timeout(Duration::from_secs(5));
7878
/// ```
79-
pub fn timeout(&mut self, dur: Duration) -> &mut Self {
80-
self.timeout = Some(dur);
81-
self
79+
pub fn timeout(self, dur: Duration) -> Self {
80+
Endpoint {
81+
timeout: Some(dur),
82+
..self
83+
}
8284
}
8385

8486
/// Apply a concurrency limit to each request.
@@ -88,9 +90,11 @@ impl Endpoint {
8890
/// # let mut builder = Endpoint::from_static("https://example.com");
8991
/// builder.concurrency_limit(256);
9092
/// ```
91-
pub fn concurrency_limit(&mut self, limit: usize) -> &mut Self {
92-
self.concurrency_limit = Some(limit);
93-
self
93+
pub fn concurrency_limit(self, limit: usize) -> Self {
94+
Endpoint {
95+
concurrency_limit: Some(limit),
96+
..self
97+
}
9498
}
9599

96100
/// Apply a rate limit to each request.
@@ -101,9 +105,11 @@ impl Endpoint {
101105
/// # let mut builder = Endpoint::from_static("https://example.com");
102106
/// builder.rate_limit(32, Duration::from_secs(1));
103107
/// ```
104-
pub fn rate_limit(&mut self, limit: u64, duration: Duration) -> &mut Self {
105-
self.rate_limit = Some((limit, duration));
106-
self
108+
pub fn rate_limit(self, limit: u64, duration: Duration) -> Self {
109+
Endpoint {
110+
rate_limit: Some((limit, duration)),
111+
..self
112+
}
107113
}
108114

109115
/// Sets the [`SETTINGS_INITIAL_WINDOW_SIZE`][spec] option for HTTP2
@@ -112,33 +118,41 @@ impl Endpoint {
112118
/// Default is 65,535
113119
///
114120
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_INITIAL_WINDOW_SIZE
115-
pub fn initial_stream_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
116-
self.init_stream_window_size = sz.into();
117-
self
121+
pub fn initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> Self {
122+
Endpoint {
123+
init_stream_window_size: sz.into(),
124+
..self
125+
}
118126
}
119127

120128
/// Sets the max connection-level flow control for HTTP2
121129
///
122130
/// Default is 65,535
123-
pub fn initial_connection_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
124-
self.init_connection_window_size = sz.into();
125-
self
131+
pub fn initial_connection_window_size(self, sz: impl Into<Option<u32>>) -> Self {
132+
Endpoint {
133+
init_connection_window_size: sz.into(),
134+
..self
135+
}
126136
}
127137

128138
/// Intercept outbound HTTP Request headers;
129-
pub fn intercept_headers<F>(&mut self, f: F) -> &mut Self
139+
pub fn intercept_headers<F>(self, f: F) -> Self
130140
where
131141
F: Fn(&mut http::HeaderMap) + Send + Sync + 'static,
132142
{
133-
self.interceptor_headers = Some(Arc::new(f));
134-
self
143+
Endpoint {
144+
interceptor_headers: Some(Arc::new(f)),
145+
..self
146+
}
135147
}
136148

137149
/// Configures TLS for the endpoint.
138150
#[cfg(feature = "tls")]
139-
pub fn tls_config(&mut self, tls_config: &ClientTlsConfig) -> &mut Self {
140-
self.tls = Some(tls_config.tls_connector(self.uri.clone()).unwrap());
141-
self
151+
pub fn tls_config(self, tls_config: ClientTlsConfig) -> Self {
152+
Endpoint {
153+
tls: Some(tls_config.tls_connector(self.uri.clone()).unwrap()),
154+
..self
155+
}
142156
}
143157

144158
/// Create a channel from this config.
@@ -262,48 +276,55 @@ impl ClientTlsConfig {
262276
///
263277
/// This has no effect if `rustls_client_config` or `openssl_connector` is used to configure
264278
/// Rustls or OpenSSL respectively.
265-
pub fn domain_name(&mut self, domain_name: impl Into<String>) -> &mut Self {
266-
self.domain = Some(domain_name.into());
267-
self
279+
pub fn domain_name(self, domain_name: impl Into<String>) -> Self {
280+
ClientTlsConfig {
281+
domain: Some(domain_name.into()),
282+
..self
283+
}
268284
}
269285

270286
/// Sets the CA Certificate against which to verify the server's TLS certificate.
271287
///
272288
/// This has no effect if `rustls_client_config` or `openssl_connector` is used to configure
273289
/// Rustls or OpenSSL respectively.
274-
pub fn ca_certificate(&mut self, ca_certificate: Certificate) -> &mut Self {
275-
self.cert = Some(ca_certificate);
276-
self
290+
pub fn ca_certificate(self, ca_certificate: Certificate) -> Self {
291+
ClientTlsConfig {
292+
cert: Some(ca_certificate),
293+
..self
294+
}
277295
}
278296

279297
/// Sets the client identity to present to the server.
280298
///
281299
/// This has no effect if `rustls_client_config` or `openssl_connector` is used to configure
282300
/// Rustls or OpenSSL respectively.
283-
pub fn identity(&mut self, identity: Identity) -> &mut Self {
284-
self.identity = Some(identity);
285-
self
301+
pub fn identity(self, identity: Identity) -> Self {
302+
ClientTlsConfig {
303+
identity: Some(identity),
304+
..self
305+
}
286306
}
287307

288308
/// Use options specified by the given `SslConnector` to configure TLS.
289309
///
290310
/// This overrides all other TLS options set via other means.
291311
#[cfg(feature = "openssl")]
292-
pub fn openssl_connector(&mut self, connector: openssl1::ssl::SslConnector) -> &mut Self {
293-
self.openssl_raw = Some(connector);
294-
self
312+
pub fn openssl_connector(self, connector: openssl1::ssl::SslConnector) -> Self {
313+
ClientTlsConfig {
314+
openssl_raw: Some(connector),
315+
..self
316+
}
295317
}
296318

297319
/// Use options specified by the given `ClientConfig` to configure TLS.
298320
///
299321
/// This overrides all other TLS options set via other means.
300322
#[cfg(feature = "rustls")]
301-
pub fn rustls_client_config(
302-
&mut self,
303-
config: tokio_rustls::rustls::ClientConfig,
304-
) -> &mut Self {
305-
self.rustls_raw = Some(config);
306-
self
323+
pub fn rustls_client_config(self, config: tokio_rustls::rustls::ClientConfig) -> Self {
324+
ClientTlsConfig {
325+
rustls_raw: Some(config),
326+
..self
327+
}
307328
}
308329

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

0 commit comments

Comments
 (0)