Skip to content

Commit fb03626

Browse files
authored
only build SslConnectorBuilder once (#2503)
1 parent d2b9724 commit fb03626

8 files changed

Lines changed: 61 additions & 24 deletions

File tree

‎actix-http-test/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
107107
Connector::new()
108108
.conn_lifetime(Duration::from_secs(0))
109109
.timeout(Duration::from_millis(30000))
110-
.ssl(builder.build())
110+
.openssl(builder.build())
111111
};
112112

113113
#[cfg(not(feature = "openssl"))]

‎actix-test/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ where
340340
Connector::new()
341341
.conn_lifetime(Duration::from_secs(0))
342342
.timeout(Duration::from_millis(30000))
343-
.ssl(builder.build())
343+
.openssl(builder.build())
344344
}
345345
#[cfg(not(feature = "openssl"))]
346346
{

‎awc/CHANGES.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changes
22

33
## Unreleased - 2021-xx-xx
4+
* Rename `Connector::{ssl => openssl}`. [#2503]
5+
* Improve `Client` instantiation efficiency when using `openssl` by only building connectors once. [#2503]
6+
7+
[#2503]: https://github.com/actix/actix-web/pull/2503
48

59

610
## 3.0.0-beta.14 - 2021-12-17

‎awc/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ actix-codec = "0.4.1"
6262
actix-service = "2.0.0"
6363
actix-http = "3.0.0-beta.16"
6464
actix-rt = { version = "2.1", default-features = false }
65-
actix-tls = { version = "3.0.0-rc.1", features = ["connect", "uri"] }
65+
actix-tls = { version = "3.0.0-rc.2", features = ["connect", "uri"] }
6666
actix-utils = "3.0.0"
6767

6868
ahash = "0.7"

‎awc/src/client/connector.rs‎

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ use futures_core::{future::LocalBoxFuture, ready};
2222
use http::Uri;
2323
use pin_project_lite::pin_project;
2424

25-
use super::config::ConnectorConfig;
26-
use super::connection::{Connection, ConnectionIo};
27-
use super::error::ConnectError;
28-
use super::pool::ConnectionPool;
29-
use super::Connect;
25+
use super::{
26+
config::ConnectorConfig,
27+
connection::{Connection, ConnectionIo},
28+
error::ConnectError,
29+
pool::ConnectionPool,
30+
Connect,
31+
};
3032

3133
enum OurTlsConnector {
3234
#[allow(dead_code)] // only dead when no TLS feature is enabled
@@ -35,6 +37,12 @@ enum OurTlsConnector {
3537
#[cfg(feature = "openssl")]
3638
Openssl(actix_tls::connect::openssl::reexports::SslConnector),
3739

40+
/// Provided because building the OpenSSL context on newer versions can be very slow.
41+
/// This prevents unnecessary calls to `.build()` while constructing the client connector.
42+
#[cfg(feature = "openssl")]
43+
#[allow(dead_code)] // false positive; used in build_ssl
44+
OpensslBuilder(actix_tls::connect::openssl::reexports::SslConnectorBuilder),
45+
3846
#[cfg(feature = "rustls")]
3947
Rustls(std::sync::Arc<actix_tls::connect::rustls::reexports::ClientConfig>),
4048
}
@@ -57,7 +65,7 @@ pub struct Connector<T> {
5765
config: ConnectorConfig,
5866

5967
#[allow(dead_code)] // only dead when no TLS feature is enabled
60-
ssl: OurTlsConnector,
68+
tls: OurTlsConnector,
6169
}
6270

6371
impl Connector<()> {
@@ -72,7 +80,7 @@ impl Connector<()> {
7280
Connector {
7381
connector: TcpConnector::new(resolver::resolver()).service(),
7482
config: ConnectorConfig::default(),
75-
ssl: Self::build_ssl(vec![b"h2".to_vec(), b"http/1.1".to_vec()]),
83+
tls: Self::build_ssl(vec![b"h2".to_vec(), b"http/1.1".to_vec()]),
7684
}
7785
}
7886

@@ -116,7 +124,7 @@ impl Connector<()> {
116124
log::error!("Can not set ALPN protocol: {:?}", err);
117125
}
118126

119-
OurTlsConnector::Openssl(ssl.build())
127+
OurTlsConnector::OpensslBuilder(ssl)
120128
}
121129
}
122130

@@ -134,7 +142,7 @@ impl<S> Connector<S> {
134142
Connector {
135143
connector,
136144
config: self.config,
137-
ssl: self.ssl,
145+
tls: self.tls,
138146
}
139147
}
140148
}
@@ -167,23 +175,35 @@ where
167175
self
168176
}
169177

178+
/// Use custom OpenSSL `SslConnector` instance.
179+
#[cfg(feature = "openssl")]
180+
pub fn openssl(
181+
mut self,
182+
connector: actix_tls::connect::openssl::reexports::SslConnector,
183+
) -> Self {
184+
self.tls = OurTlsConnector::Openssl(connector);
185+
self
186+
}
187+
188+
/// See docs for [`Connector::openssl`].
189+
#[doc(hidden)]
170190
#[cfg(feature = "openssl")]
171-
/// Use custom `SslConnector` instance.
191+
#[deprecated(since = "3.0.0", note = "Renamed to `Connector::openssl`.")]
172192
pub fn ssl(
173193
mut self,
174194
connector: actix_tls::connect::openssl::reexports::SslConnector,
175195
) -> Self {
176-
self.ssl = OurTlsConnector::Openssl(connector);
196+
self.tls = OurTlsConnector::Openssl(connector);
177197
self
178198
}
179199

200+
/// Use custom Rustls `ClientConfig` instance.
180201
#[cfg(feature = "rustls")]
181-
/// Use custom `ClientConfig` instance.
182202
pub fn rustls(
183203
mut self,
184204
connector: std::sync::Arc<actix_tls::connect::rustls::reexports::ClientConfig>,
185205
) -> Self {
186-
self.ssl = OurTlsConnector::Rustls(connector);
206+
self.tls = OurTlsConnector::Rustls(connector);
187207
self
188208
}
189209

@@ -198,7 +218,7 @@ where
198218
unimplemented!("actix-http client only supports versions http/1.1 & http/2")
199219
}
200220
};
201-
self.ssl = Connector::build_ssl(versions);
221+
self.tls = Connector::build_ssl(versions);
202222
self
203223
}
204224

@@ -270,8 +290,8 @@ where
270290
}
271291

272292
/// Finish configuration process and create connector service.
273-
/// The Connector builder always concludes by calling `finish()` last in
274-
/// its combinator chain.
293+
///
294+
/// The `Connector` builder always concludes by calling `finish()` last in its combinator chain.
275295
pub fn finish(self) -> ConnectorService<S, IO> {
276296
let local_address = self.config.local_address;
277297
let timeout = self.config.timeout;
@@ -284,7 +304,15 @@ where
284304
service: tcp_service_inner.clone(),
285305
};
286306

287-
let tls_service = match self.ssl {
307+
let tls = match self.tls {
308+
#[cfg(feature = "openssl")]
309+
OurTlsConnector::OpensslBuilder(builder) => {
310+
OurTlsConnector::Openssl(builder.build())
311+
}
312+
tls => tls,
313+
};
314+
315+
let tls_service = match tls {
288316
OurTlsConnector::None => {
289317
#[cfg(not(feature = "dangerous-h2c"))]
290318
{
@@ -374,6 +402,11 @@ where
374402
Some(actix_service::boxed::rc_service(tls_service))
375403
}
376404

405+
#[cfg(feature = "openssl")]
406+
OurTlsConnector::OpensslBuilder(_) => {
407+
unreachable!("OpenSSL builder is built before this match.");
408+
}
409+
377410
#[cfg(feature = "rustls")]
378411
OurTlsConnector::Rustls(tls) => {
379412
const H2: &[u8] = b"h2";
@@ -853,7 +886,7 @@ mod tests {
853886
let connector = Connector {
854887
connector: TcpConnector::new(resolver::resolver()).service(),
855888
config: ConnectorConfig::default(),
856-
ssl: OurTlsConnector::None,
889+
tls: OurTlsConnector::None,
857890
};
858891

859892
let client = Client::builder().connector(connector).finish();

‎awc/tests/test_connector.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async fn test_connection_window_size() {
5858
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
5959

6060
let client = awc::Client::builder()
61-
.connector(awc::Connector::new().ssl(builder.build()))
61+
.connector(awc::Connector::new().openssl(builder.build()))
6262
.initial_window_size(100)
6363
.initial_connection_window_size(100)
6464
.finish();

‎awc/tests/test_ssl_client.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async fn test_connection_reuse_h2() {
7272
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
7373

7474
let client = awc::Client::builder()
75-
.connector(awc::Connector::new().ssl(builder.build()))
75+
.connector(awc::Connector::new().openssl(builder.build()))
7676
.finish();
7777

7878
// req 1

‎tests/test_httpserver.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async fn test_start_ssl() {
121121
let client = awc::Client::builder()
122122
.connector(
123123
awc::Connector::new()
124-
.ssl(builder.build())
124+
.openssl(builder.build())
125125
.timeout(Duration::from_millis(100)),
126126
)
127127
.finish();

0 commit comments

Comments
 (0)