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

+27 -3
View File
@@ -105,6 +105,10 @@ const DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
const EXPORTER_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
async fn serve(config: Config) -> Result<()> {
// Install SIGHUP handling before anything else: the default action would
// terminate the process, with or without TLS.
let hup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())
.context("installing SIGHUP handler")?;
let guard = telemetry::init(&config).context("initialising telemetry")?;
let db = if config.auth.enabled {
Some(db::open(&config).await?)
@@ -136,8 +140,22 @@ async fn serve(config: Config) -> Result<()> {
let exporters = telemetry::spawn_exporters(state.clone());
let pool = state.db.clone();
let app = server::build_router(state);
if let Some((_, cert)) = &tls {
tls::spawn_sighup_reload(cert.clone(), shutdown.clone());
match &tls {
Some((_, cert)) => tls::spawn_sighup_reload(cert.clone(), hup, shutdown.clone()),
None => {
// Plain path: SIGHUP is ignored (logged only), nothing to reload.
let stop = shutdown.clone();
let mut hup = hup;
tokio::spawn(async move {
loop {
tokio::select! {
_ = stop.cancelled() => break,
r = hup.recv() => if r.is_none() { break },
}
tracing::info!("SIGHUP received; nothing to reload (TLS is disabled)");
}
});
}
}
let signal_token = shutdown.clone();
@@ -152,7 +170,13 @@ async fn serve(config: Config) -> Result<()> {
// immediately. Bound the drain so a stuck client cannot hang SIGTERM.
let serving: std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send>> = match tls
{
Some((acceptor, _)) => Box::pin(tls::serve_tls(listener, acceptor, app, shutdown.clone())),
Some((acceptor, _)) => Box::pin(tls::serve_tls(
listener,
acceptor,
app,
shutdown.clone(),
tls::Limits::default(),
)),
None => Box::pin(server::serve_listener(listener, app, shutdown.clone())),
};
tokio::pin!(serving);
+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() => {