Skip to content

Commit

Permalink
feat: builder with shared configuration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Fishrock123 committed Mar 15, 2024
1 parent 68c7d05 commit cbc9fa8
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/connector/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use hyper_util::client::legacy::connect::HttpConnector;
#[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))]
use rustls::crypto::CryptoProvider;
Expand Down Expand Up @@ -44,6 +46,27 @@ impl ConnectorBuilder<WantsTlsConfig> {
/// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
/// connector is built.
pub fn with_tls_config(self, config: ClientConfig) -> ConnectorBuilder<WantsSchemes> {
assert!(
config.alpn_protocols.is_empty(),
"ALPN protocols should not be pre-defined"
);
ConnectorBuilder(WantsSchemes {
tls_config: Arc::new(config),
})
}

/// Passes an [`Arc`]-shared rustls [`ClientConfig`] to configure the TLS connection
///
/// The [`alpn_protocols`](ClientConfig::alpn_protocols) field is
/// required to be empty (or the function will panic) and will be
/// rewritten to match the enabled schemes (see
/// [`enable_http1`](ConnectorBuilder::enable_http1),
/// [`enable_http2`](ConnectorBuilder::enable_http2)) before the
/// connector is built.
pub fn with_shared_tls_config(
self,
config: Arc<ClientConfig>,
) -> ConnectorBuilder<WantsSchemes> {
assert!(
config.alpn_protocols.is_empty(),
"ALPN protocols should not be pre-defined"
Expand Down Expand Up @@ -133,7 +156,7 @@ impl Default for ConnectorBuilder<WantsTlsConfig> {
/// State of a builder that needs schemes (https:// and http://) to be
/// configured next
pub struct WantsSchemes {
tls_config: ClientConfig,
tls_config: Arc<ClientConfig>,
}

impl ConnectorBuilder<WantsSchemes> {
Expand Down Expand Up @@ -166,7 +189,7 @@ impl ConnectorBuilder<WantsSchemes> {
///
/// No protocol has been enabled at this point.
pub struct WantsProtocols1 {
tls_config: ClientConfig,
tls_config: Arc<ClientConfig>,
https_only: bool,
override_server_name: Option<String>,
}
Expand All @@ -176,7 +199,7 @@ impl WantsProtocols1 {
HttpsConnector {
force_https: self.https_only,
http: conn,
tls_config: std::sync::Arc::new(self.tls_config),
tls_config: self.tls_config,
override_server_name: self.override_server_name,
}
}
Expand Down

0 comments on commit cbc9fa8

Please sign in to comment.