Skip to content

Commit cbc9fa8

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 cbc9fa8

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/connector/builder.rs

+26-3
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
}

0 commit comments

Comments
 (0)