Skip to content

Commit 643a3c6

Browse files
committed
feat: TLS session resumption
1 parent 3b2f18f commit 643a3c6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/net/tls.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
use std::sync::Arc;
33

44
use anyhow::Result;
5+
use once_cell::sync::Lazy;
56

67
use crate::net::session::SessionStream;
78

9+
use rustls::client::ClientSessionStore;
10+
811
pub async fn wrap_tls(
912
strict_tls: bool,
1013
hostname: &str,
@@ -30,6 +33,10 @@ pub async fn wrap_tls(
3033
}
3134
}
3235

36+
// This is the default as of version 0.23.16, but make it shared between clients.
37+
static RESUMPTION_STORE: Lazy<Arc<dyn ClientSessionStore>> =
38+
Lazy::new(|| Arc::new(rustls::client::ClientSessionMemoryCache::new(256)));
39+
3340
pub async fn wrap_rustls(
3441
hostname: &str,
3542
alpn: &[&str],
@@ -43,6 +50,9 @@ pub async fn wrap_rustls(
4350
.with_no_client_auth();
4451
config.alpn_protocols = alpn.iter().map(|s| s.as_bytes().to_vec()).collect();
4552

53+
let resumption = rustls::client::Resumption::store(Arc::clone(&RESUMPTION_STORE));
54+
config.resumption = resumption;
55+
4656
let tls = tokio_rustls::TlsConnector::from(Arc::new(config));
4757
let name = rustls_pki_types::ServerName::try_from(hostname)?.to_owned();
4858
let tls_stream = tls.connect(name, stream).await?;

0 commit comments

Comments
 (0)