Skip to content

Commit 4a59f1e

Browse files
committed
feat: builder with shared configuration
Sometimes one might want to share the config to the builder api like one can to the connector api itself, allowing some optimization. Since the config eventually get's `Arc`-d anyways there can be no disadvantage to this.
1 parent 68c7d05 commit 4a59f1e

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.features": "all"
3+
}

src/connector/builder.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use hyper_util::client::legacy::connect::HttpConnector;
24
#[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))]
35
use rustls::crypto::CryptoProvider;
@@ -35,20 +37,24 @@ impl ConnectorBuilder<WantsTlsConfig> {
3537
Self(WantsTlsConfig(()))
3638
}
3739

38-
/// Passes a rustls [`ClientConfig`] to configure the TLS connection
40+
/// Passes a rustls [`ClientConfig`] to configure the TLS connection.
3941
///
4042
/// The [`alpn_protocols`](ClientConfig::alpn_protocols) field is
4143
/// required to be empty (or the function will panic) and will be
4244
/// rewritten to match the enabled schemes (see
4345
/// [`enable_http1`](ConnectorBuilder::enable_http1),
4446
/// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
4547
/// connector is built.
46-
pub fn with_tls_config(self, config: ClientConfig) -> ConnectorBuilder<WantsSchemes> {
48+
pub fn with_tls_config(
49+
self,
50+
config: impl Into<Arc<ClientConfig>>,
51+
) -> ConnectorBuilder<WantsSchemes> {
52+
let tls_config = config.into();
4753
assert!(
48-
config.alpn_protocols.is_empty(),
54+
tls_config.alpn_protocols.is_empty(),
4955
"ALPN protocols should not be pre-defined"
5056
);
51-
ConnectorBuilder(WantsSchemes { tls_config: config })
57+
ConnectorBuilder(WantsSchemes { tls_config })
5258
}
5359

5460
/// Use rustls' default crypto provider and other defaults, and the platform verifier
@@ -133,7 +139,7 @@ impl Default for ConnectorBuilder<WantsTlsConfig> {
133139
/// State of a builder that needs schemes (https:// and http://) to be
134140
/// configured next
135141
pub struct WantsSchemes {
136-
tls_config: ClientConfig,
142+
tls_config: Arc<ClientConfig>,
137143
}
138144

139145
impl ConnectorBuilder<WantsSchemes> {
@@ -166,7 +172,7 @@ impl ConnectorBuilder<WantsSchemes> {
166172
///
167173
/// No protocol has been enabled at this point.
168174
pub struct WantsProtocols1 {
169-
tls_config: ClientConfig,
175+
tls_config: Arc<ClientConfig>,
170176
https_only: bool,
171177
override_server_name: Option<String>,
172178
}
@@ -176,7 +182,7 @@ impl WantsProtocols1 {
176182
HttpsConnector {
177183
force_https: self.https_only,
178184
http: conn,
179-
tls_config: std::sync::Arc::new(self.tls_config),
185+
tls_config: self.tls_config,
180186
override_server_name: self.override_server_name,
181187
}
182188
}
@@ -203,7 +209,7 @@ impl ConnectorBuilder<WantsProtocols1> {
203209
/// This needs to be called explicitly, no protocol is enabled by default
204210
#[cfg(feature = "http2")]
205211
pub fn enable_http2(mut self) -> ConnectorBuilder<WantsProtocols3> {
206-
self.0.tls_config.alpn_protocols = vec![b"h2".to_vec()];
212+
Arc::make_mut(&mut self.0.tls_config).alpn_protocols = vec![b"h2".to_vec()];
207213
ConnectorBuilder(WantsProtocols3 {
208214
inner: self.0,
209215
enable_http1: false,
@@ -221,7 +227,7 @@ impl ConnectorBuilder<WantsProtocols1> {
221227
#[cfg(not(feature = "http1"))]
222228
let alpn_protocols = vec![b"h2".to_vec()];
223229

224-
self.0.tls_config.alpn_protocols = alpn_protocols;
230+
Arc::make_mut(&mut self.0.tls_config).alpn_protocols = alpn_protocols;
225231
ConnectorBuilder(WantsProtocols3 {
226232
inner: self.0,
227233
enable_http1: cfg!(feature = "http1"),
@@ -259,7 +265,8 @@ impl ConnectorBuilder<WantsProtocols2> {
259265
/// This needs to be called explicitly, no protocol is enabled by default
260266
#[cfg(feature = "http2")]
261267
pub fn enable_http2(mut self) -> ConnectorBuilder<WantsProtocols3> {
262-
self.0.inner.tls_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
268+
Arc::make_mut(&mut self.0.inner.tls_config).alpn_protocols =
269+
vec![b"h2".to_vec(), b"http/1.1".to_vec()];
263270
ConnectorBuilder(WantsProtocols3 {
264271
inner: self.0.inner,
265272
enable_http1: true,

0 commit comments

Comments
 (0)