Serve plain HTTP through the shared hyper accept loop with connection cap and header timeout
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
d5a0373a75
commit
f1d659965b
6 files changed
+245
-136
No files matched your search
+2
-117
@@ -1,7 +1,8 @@
|
||||
//! Native TLS: rustls with the `ring` provider passed explicitly to
|
||||
//! the config builder (nothing is installed process-wide, so the OTLP
|
||||
//! clients' provider choice is unaffected). Only used when
|
||||
//! `server.tls.enabled`; the plain path never touches this module.
|
||||
//! `server.tls.enabled`; the plain path never touches this module. The accept
|
||||
//! loop itself lives in [`crate::server`].
|
||||
//!
|
||||
//! The certificate lives behind a [`ResolvesServerCert`] that can be swapped
|
||||
//! at runtime, so SIGHUP reload leaves established connections untouched and
|
||||
@@ -9,29 +10,19 @@
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use axum::Router;
|
||||
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;
|
||||
use rustls::pki_types::pem::PemObject;
|
||||
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;
|
||||
|
||||
use crate::config::TlsConfig;
|
||||
|
||||
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// Hot-swappable server certificate.
|
||||
#[derive(Debug)]
|
||||
pub struct ReloadableCert {
|
||||
@@ -131,109 +122,3 @@ pub fn spawn_sighup_reload(
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// 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).
|
||||
pub async fn serve_tls(
|
||||
listener: tokio::net::TcpListener,
|
||||
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() => {}
|
||||
r = listener.accept() => {
|
||||
let (tcp, peer) = match r {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
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;
|
||||
}
|
||||
};
|
||||
let acceptor = acceptor.clone();
|
||||
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)) => {
|
||||
tracing::debug!(%peer, error = %e, "TLS handshake failed");
|
||||
return;
|
||||
}
|
||||
Err(_) => {
|
||||
tracing::debug!(%peer, "TLS handshake timed out");
|
||||
return;
|
||||
}
|
||||
};
|
||||
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() => {
|
||||
if let Err(e) = r { tracing::debug!(%peer, error = %e, "connection error"); }
|
||||
}
|
||||
_ = stop.cancelled() => {
|
||||
conn.as_mut().graceful_shutdown();
|
||||
if let Err(e) = conn.as_mut().await {
|
||||
tracing::debug!(%peer, error = %e, "connection error");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
drop(listener);
|
||||
while conns.join_next().await.is_some() {}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user