WS-I: review fixes (connection cap, header timeout, SIGHUP on plain path, HTTP/1.1 only)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ariandClaude Sonnet 5.5 committed 2026-10-16 07:19:14 -04:00
1 parent c5972b762f
commit 9f2bbc7d14
6 files changed
+252 -18

No files matched your search

+56 -13
View File
@@ -13,7 +13,7 @@ use std::time::Duration;
use anyhow::{anyhow, Context, Result};
use axum::Router;
use hyper_util::rt::{TokioExecutor, TokioIo};
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use hyper_util::server::conn::auto::Builder;
use hyper_util::service::TowerToHyperService;
use rustls::crypto::ring as provider;
@@ -22,7 +22,9 @@ use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use rustls::server::{ClientHello, ResolvesServerCert};
use rustls::sign::CertifiedKey;
use rustls::ServerConfig;
use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tokio::time::Instant;
use tokio_rustls::TlsAcceptor;
use tokio_util::sync::CancellationToken;
@@ -106,15 +108,13 @@ pub fn acceptor(cert: Arc<ReloadableCert>) -> Result<TlsAcceptor> {
}
/// Reload on every SIGHUP until `shutdown`. Failures are logged, never fatal.
pub fn spawn_sighup_reload(cert: Arc<ReloadableCert>, shutdown: CancellationToken) {
use tokio::signal::unix::{signal, SignalKind};
let mut hup = match signal(SignalKind::hangup()) {
Ok(s) => s,
Err(e) => {
tracing::error!(error = %e, "cannot install SIGHUP handler; TLS reload disabled");
return;
}
};
/// `hup` is installed by the caller before binding so no SIGHUP can kill the
/// process in the meantime.
pub fn spawn_sighup_reload(
cert: Arc<ReloadableCert>,
mut hup: tokio::signal::unix::Signal,
shutdown: CancellationToken,
) {
tokio::spawn(async move {
loop {
tokio::select! {
@@ -132,6 +132,26 @@ pub fn spawn_sighup_reload(cert: Arc<ReloadableCert>, shutdown: CancellationToke
});
}
/// Connection limits for the TLS listener.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
/// Maximum concurrent connections (handshaking or established). At the
/// cap, accepting pauses until a connection closes.
pub max_conns: usize,
/// Time allowed to receive a request header (also covers idle
/// keep-alive and post-handshake silence).
pub header_timeout: Duration,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_conns: 1024,
header_timeout: Duration::from_secs(30),
}
}
}
/// Serve `app` over TLS until `shutdown` is cancelled, then drain: stop
/// accepting, ask each connection to finish gracefully, and wait. The caller
/// bounds the drain by dropping this future (which aborts the connections).
@@ -140,11 +160,23 @@ pub async fn serve_tls(
acceptor: TlsAcceptor,
app: Router,
shutdown: CancellationToken,
limits: Limits,
) -> Result<()> {
let addr = listener.local_addr().context("local_addr")?;
tracing::info!(%addr, tls = true, "listening");
let mut conns: JoinSet<()> = JoinSet::new();
let slots = Arc::new(Semaphore::new(limits.max_conns));
let mut last_accept_warn: Option<Instant> = None;
loop {
// Wait for a free slot first, so at the cap we stop accepting.
let permit = tokio::select! {
_ = shutdown.cancelled() => break,
Some(_) = conns.join_next(), if !conns.is_empty() => continue,
p = slots.clone().acquire_owned() => match p {
Ok(p) => p,
Err(_) => break,
},
};
tokio::select! {
_ = shutdown.cancelled() => break,
Some(_) = conns.join_next(), if !conns.is_empty() => {}
@@ -152,7 +184,10 @@ pub async fn serve_tls(
let (tcp, peer) = match r {
Ok(x) => x,
Err(e) => {
tracing::warn!(error = %e, "accept failed");
if last_accept_warn.is_none_or(|t| t.elapsed() > Duration::from_secs(10)) {
tracing::warn!(error = %e, "accept failed (further failures suppressed for 10s)");
last_accept_warn = Some(Instant::now());
}
tokio::time::sleep(Duration::from_millis(100)).await;
continue;
}
@@ -161,6 +196,7 @@ pub async fn serve_tls(
let svc = TowerToHyperService::new(app.clone());
let stop = shutdown.clone();
conns.spawn(async move {
let _permit = permit;
let tls = match tokio::time::timeout(HANDSHAKE_TIMEOUT, acceptor.accept(tcp)).await {
Ok(Ok(t)) => t,
Ok(Err(e)) => {
@@ -172,8 +208,15 @@ pub async fn serve_tls(
return;
}
};
let builder = Builder::new(TokioExecutor::new());
let conn = builder.serve_connection_with_upgrades(TokioIo::new(tls), svc);
let mut builder = Builder::new(TokioExecutor::new()).http1_only();
builder
.http1()
.timer(TokioTimer::new())
.header_read_timeout(limits.header_timeout);
// Plain serve_connection: the auto builder's _with_upgrades
// variant sniffs the protocol first, which would defeat the
// header timeout for silent clients.
let conn = builder.serve_connection(TokioIo::new(tls), svc);
tokio::pin!(conn);
tokio::select! {
r = conn.as_mut() => {