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:
ariandClaude Sonnet 5.5 committed 2026-10-16 10:02:08 -04:00
1 parent d5a0373a75
commit f1d659965b
6 files changed
+245 -136

No files matched your search

+120 -6
View File
@@ -8,6 +8,15 @@ use tokio_util::sync::CancellationToken;
use tower_http::trace::TraceLayer;
use std::sync::Arc;
use std::time::Duration;
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use hyper_util::server::conn::auto::Builder;
use hyper_util::service::TowerToHyperService;
use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tokio::time::Instant;
use tokio_rustls::TlsAcceptor;
use axum::extract::{Request, State};
use axum::http::{header, StatusCode};
@@ -42,18 +51,123 @@ pub fn build_router(state: AppState) -> Router {
.layer(TraceLayer::new_for_http())
}
/// Serve `app` on `listener` until `shutdown` is cancelled.
/// Connection limits shared by the plain and TLS listeners.
#[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),
}
}
}
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
/// Serve `app` on `listener` (over TLS when `acceptor` is set) 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_listener(
listener: tokio::net::TcpListener,
acceptor: Option<TlsAcceptor>,
app: Router,
shutdown: CancellationToken,
limits: Limits,
) -> Result<()> {
let addr = listener.local_addr().context("local_addr")?;
tracing::info!(%addr, "listening");
axum::serve(listener, app)
.with_graceful_shutdown(async move { shutdown.cancelled().await })
.await
.context("server error")
tracing::info!(%addr, tls = acceptor.is_some(), "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;
match acceptor {
None => serve_conn(tcp, peer, svc, stop, limits).await,
Some(acceptor) => {
match tokio::time::timeout(HANDSHAKE_TIMEOUT, acceptor.accept(tcp)).await {
Ok(Ok(tls)) => serve_conn(tls, peer, svc, stop, limits).await,
Ok(Err(e)) => tracing::debug!(%peer, error = %e, "TLS handshake failed"),
Err(_) => tracing::debug!(%peer, "TLS handshake timed out"),
}
}
}
});
}
}
}
drop(listener);
while conns.join_next().await.is_some() {}
Ok(())
}
async fn serve_conn<I>(
io: I,
peer: std::net::SocketAddr,
svc: TowerToHyperService<Router>,
stop: CancellationToken,
limits: Limits,
) where
I: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
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(io), 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");
}
}
}
}
fn allowed_hosts(c: &Config) -> Arc<Vec<String>> {