Skip to content

Commit 94bd6ce

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 94bd6ce

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
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

+30-6
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;
@@ -44,6 +46,27 @@ impl ConnectorBuilder<WantsTlsConfig> {
4446
/// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
4547
/// connector is built.
4648
pub fn with_tls_config(self, config: ClientConfig) -> ConnectorBuilder<WantsSchemes> {
49+
assert!(
50+
config.alpn_protocols.is_empty(),
51+
"ALPN protocols should not be pre-defined"
52+
);
53+
ConnectorBuilder(WantsSchemes {
54+
tls_config: Arc::new(config),
55+
})
56+
}
57+
58+
/// Passes an [`Arc`]-shared rustls [`ClientConfig`] to configure the TLS connection
59+
///
60+
/// The [`alpn_protocols`](ClientConfig::alpn_protocols) field is
61+
/// required to be empty (or the function will panic) and will be
62+
/// rewritten to match the enabled schemes (see
63+
/// [`enable_http1`](ConnectorBuilder::enable_http1),
64+
/// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
65+
/// connector is built.
66+
pub fn with_shared_tls_config(
67+
self,
68+
config: Arc<ClientConfig>,
69+
) -> ConnectorBuilder<WantsSchemes> {
4770
assert!(
4871
config.alpn_protocols.is_empty(),
4972
"ALPN protocols should not be pre-defined"
@@ -133,7 +156,7 @@ impl Default for ConnectorBuilder<WantsTlsConfig> {
133156
/// State of a builder that needs schemes (https:// and http://) to be
134157
/// configured next
135158
pub struct WantsSchemes {
136-
tls_config: ClientConfig,
159+
tls_config: Arc<ClientConfig>,
137160
}
138161

139162
impl ConnectorBuilder<WantsSchemes> {
@@ -166,7 +189,7 @@ impl ConnectorBuilder<WantsSchemes> {
166189
///
167190
/// No protocol has been enabled at this point.
168191
pub struct WantsProtocols1 {
169-
tls_config: ClientConfig,
192+
tls_config: Arc<ClientConfig>,
170193
https_only: bool,
171194
override_server_name: Option<String>,
172195
}
@@ -176,7 +199,7 @@ impl WantsProtocols1 {
176199
HttpsConnector {
177200
force_https: self.https_only,
178201
http: conn,
179-
tls_config: std::sync::Arc::new(self.tls_config),
202+
tls_config: self.tls_config,
180203
override_server_name: self.override_server_name,
181204
}
182205
}
@@ -203,7 +226,7 @@ impl ConnectorBuilder<WantsProtocols1> {
203226
/// This needs to be called explicitly, no protocol is enabled by default
204227
#[cfg(feature = "http2")]
205228
pub fn enable_http2(mut self) -> ConnectorBuilder<WantsProtocols3> {
206-
self.0.tls_config.alpn_protocols = vec![b"h2".to_vec()];
229+
Arc::make_mut(&mut self.0.tls_config).alpn_protocols = vec![b"h2".to_vec()];
207230
ConnectorBuilder(WantsProtocols3 {
208231
inner: self.0,
209232
enable_http1: false,
@@ -221,7 +244,7 @@ impl ConnectorBuilder<WantsProtocols1> {
221244
#[cfg(not(feature = "http1"))]
222245
let alpn_protocols = vec![b"h2".to_vec()];
223246

224-
self.0.tls_config.alpn_protocols = alpn_protocols;
247+
Arc::make_mut(&mut self.0.tls_config).alpn_protocols = alpn_protocols;
225248
ConnectorBuilder(WantsProtocols3 {
226249
inner: self.0,
227250
enable_http1: cfg!(feature = "http1"),
@@ -259,7 +282,8 @@ impl ConnectorBuilder<WantsProtocols2> {
259282
/// This needs to be called explicitly, no protocol is enabled by default
260283
#[cfg(feature = "http2")]
261284
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()];
285+
Arc::make_mut(&mut self.0.inner.tls_config).alpn_protocols =
286+
vec![b"h2".to_vec(), b"http/1.1".to_vec()];
263287
ConnectorBuilder(WantsProtocols3 {
264288
inner: self.0.inner,
265289
enable_http1: true,

0 commit comments

Comments
 (0)