Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add server config cache #48

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tracing = "0.1.40"
hyper-util = { version = "0.1.7", features = ["tokio"] }
native-tls = { version = "0.2.12", features = ["alpn"] }
thiserror = "1.0.62"
dashmap = "6.0.1"

[dev-dependencies]
axum = { version = "0.7.2", features = ["http2"] }
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "stable"
29 changes: 24 additions & 5 deletions src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
use std::{borrow::BorrowMut, sync::LazyLock};

use dashmap::{try_result::TryResult, DashMap};
use rustls::ServerConfig;

static SERVER_CONFIG_CACHE: LazyLock<DashMap<(String, Vec<u8>), rustls::ServerConfig>> = LazyLock::new(|| DashMap::new());

pub fn server_config(
host: String,
root_cert: &rcgen::CertifiedKey,
h2: bool,
) -> Result<rustls::ServerConfig, rustls::Error> {
let mut cert_params = rcgen::CertificateParams::new(vec![host]).unwrap();

if let TryResult::Present(config) = SERVER_CONFIG_CACHE.try_get(&(host.clone(), root_cert.key_pair.serialize_der())) {
let mut config = config.clone();
return Ok(maybe_h2_config(&mut config, h2).to_owned());
}

let mut cert_params = rcgen::CertificateParams::new(vec![host.clone()]).unwrap();
cert_params
.key_usages
.push(rcgen::KeyUsagePurpose::DigitalSignature);
Expand All @@ -29,11 +42,17 @@ pub fn server_config(
)),
);

if let Ok(config) = &config {
SERVER_CONFIG_CACHE.insert((host, root_cert.key_pair.serialize_der()), config.clone());
}

config.map(|mut config| maybe_h2_config(config.borrow_mut(), h2).to_owned())
}

fn maybe_h2_config(config: &mut ServerConfig, h2: bool) -> &ServerConfig {
if h2 {
config.map(|mut server_config| {
server_config.alpn_protocols = vec!["h2".into(), "http/1.1".into()];
server_config
})
config.alpn_protocols = vec!["h2".into(), "http/1.1".into()];
config
} else {
config
}
Expand Down
Loading