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

+9 -1
View File
@@ -43,7 +43,13 @@ async fn start(auth: bool) -> Harness {
let shutdown = state.shutdown.clone();
let state_db = state.db.clone();
let app = server::build_router(state);
let join = tokio::spawn(server::serve_listener(listener, app, shutdown.clone()));
let join = tokio::spawn(server::serve_listener(
listener,
None,
app,
shutdown.clone(),
server::Limits::default(),
));
Harness {
state_db,
port,
@@ -212,8 +218,10 @@ async fn docker_unavailable_does_not_affect_systemd() {
let shutdown = state.shutdown.clone();
tokio::spawn(server::serve_listener(
listener,
None,
server::build_router(state),
shutdown.clone(),
server::Limits::default(),
));
let (st, _, _) = http(port, "GET", "/api/v1/docker/ps", &[], None, SHORT).await;
assert_eq!(st, 503);
+104
View File
@@ -0,0 +1,104 @@
//! Plain-HTTP listener limits: the same accept loop as TLS, so the same
//! connection cap, header timeout and graceful drain apply.
use std::time::Duration;
use skyg_linux_tracker::config::Config;
use skyg_linux_tracker::server::{self, Limits};
use skyg_linux_tracker::sources::fake::fake_state;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
async fn plain(limits: Limits) -> (u16, CancellationToken, JoinHandle<anyhow::Result<()>>) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let mut config = Config::default();
config.auth.enabled = false;
let state = fake_state(config);
let shutdown = state.shutdown.clone();
let join = tokio::spawn(server::serve_listener(
listener,
None,
server::build_router(state),
shutdown.clone(),
limits,
));
(port, shutdown, join)
}
/// Connect and complete one `/healthz` exchange, keeping the connection open.
async fn served(port: u16) -> TcpStream {
let mut c = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
.await
.unwrap();
let mut buf = [0u8; 1024];
let n = c.read(&mut buf).await.unwrap();
assert!(String::from_utf8_lossy(&buf[..n]).contains("200 OK"));
c
}
#[tokio::test]
async fn slowloris_header_is_closed_after_timeout() {
let (port, shutdown, _j) = plain(Limits {
max_conns: 16,
header_timeout: Duration::from_millis(500),
})
.await;
let mut c = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
// A partial request line, then silence.
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: loc")
.await
.unwrap();
let start = std::time::Instant::now();
let mut buf = [0u8; 256];
let r = tokio::time::timeout(Duration::from_secs(5), async {
loop {
match c.read(&mut buf).await {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
}
})
.await;
assert!(r.is_ok(), "slowloris connection was not closed");
assert!(start.elapsed() < Duration::from_secs(4));
shutdown.cancel();
}
#[tokio::test]
async fn connection_cap_is_enforced() {
let (port, shutdown, _j) = plain(Limits {
max_conns: 2,
header_timeout: Duration::from_secs(30),
})
.await;
let c1 = served(port).await;
let c2 = served(port).await;
let third = tokio::time::timeout(Duration::from_millis(800), served(port)).await;
assert!(third.is_err(), "third connection was served beyond the cap");
drop(c1);
let c3 = tokio::time::timeout(Duration::from_secs(5), served(port)).await;
assert!(c3.is_ok(), "slot was not released after a close");
drop(c2);
shutdown.cancel();
}
#[tokio::test]
async fn graceful_shutdown_drains_idle_connections() {
let (port, shutdown, join) = plain(Limits::default()).await;
let mut c = served(port).await;
// The idle keep-alive connection is still open; shutdown must complete.
shutdown.cancel();
let r = tokio::time::timeout(Duration::from_secs(5), join)
.await
.expect("serve did not return after shutdown");
r.unwrap().unwrap();
let mut buf = [0u8; 64];
let n = tokio::time::timeout(Duration::from_secs(2), c.read(&mut buf))
.await
.expect("idle connection left open");
assert!(matches!(n, Ok(0) | Err(_)));
}
+5 -5
View File
@@ -567,7 +567,7 @@ async fn sigterm_with_open_tls_connections_exits_promptly() {
/// In-process TLS listener with explicit limits.
async fn inproc(
pair: &Pair,
limits: skyg_linux_tracker::tls::Limits,
limits: skyg_linux_tracker::server::Limits,
) -> (u16, tokio_util::sync::CancellationToken, tempdir::Dir) {
use skyg_linux_tracker::{config::Config, server, sources::fake::fake_state, tls};
let dir = tempdir::Dir::new("inproc");
@@ -584,9 +584,9 @@ async fn inproc(
let app = server::build_router(state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(tls::serve_tls(
tokio::spawn(server::serve_listener(
listener,
acceptor,
Some(acceptor),
app,
shutdown.clone(),
limits,
@@ -609,7 +609,7 @@ async fn silent_connection_closed_after_header_timeout() {
let pair = gen_pair();
let (port, shutdown, _d) = inproc(
&pair,
skyg_linux_tracker::tls::Limits {
skyg_linux_tracker::server::Limits {
max_conns: 16,
header_timeout: Duration::from_millis(500),
},
@@ -637,7 +637,7 @@ async fn connection_cap_is_enforced() {
let pair = gen_pair();
let (port, shutdown, _d) = inproc(
&pair,
skyg_linux_tracker::tls::Limits {
skyg_linux_tracker::server::Limits {
max_conns: 2,
header_timeout: Duration::from_secs(30),
},