Expand description
Graceful shutdown, connection drain, and server hygiene for Chio HTTP services.
Every Chio process that binds an axum::serve listener shares the same three
obligations when the platform stops it (SIGTERM on a deploy, scale event, or
node rotation):
- Notice the stop signal instead of dying on the runtime default action.
- Stop accepting, let in-flight requests finish, then exit, all inside a bounded window so a stuck request cannot hold the process open forever.
- Refuse abusive load (oversized bodies, slow requests, connection floods) with an explicit denial rather than unbounded resource growth.
This crate centralizes those obligations so the serve sites converge on one implementation instead of drifting apart:
ShutdownControllerinstalls a single signal handler and fans the result out over atokio::sync::watchchannel that the serve loop and every cooperating background task observe.apply_server_hygienewraps aRouterwith a request timeout, a global concurrency limit fronted by load shedding, and an optional body-size cap.MaxConnListenercaps the number of simultaneously accepted connections with back-pressure at the accept loop.run_until_drainedruns the serve future, bounds only the post-signal drain (never the healthy uptime), and then runs a flush hook so a store with a queued write is either made durable or surfaced as a loud error.
The posture is fail-closed throughout. A signal handler that cannot be installed logs and degrades to the platform default rather than crash-looping at startup, and each hygiene limit denies (413 / 408 / 503) rather than accepting work it cannot bound.
Structs§
- Capped
Peer Addr - Peer socket address for a connection accepted through a
MaxConnListenerover aTcpListener. - MaxConn
Listener - Wrap any
axum::serve::Listenerwith a hard cap on concurrent connections. - Permitted
Io - Connection IO that carries the connection-cap permit alongside it, so the permit is released exactly when the connection is dropped. The read and write halves forward to the wrapped IO unchanged.
- Serve
Hygiene Config - Bounds and hygiene knobs for one serve site. The
Defaultis a conservative, fail-closed posture: a request that exceeds a bound is denied, never queued. - Shutdown
Controller - Owns the shutdown watch channel that fans one stop signal out to the serve loop and every cooperating background task. The receivers it hands out are cheap to clone.
Enums§
- Drain
Outcome - How a bounded drain finished.
- Serve
Error - Terminal error from a bounded serve. Either variant is a non-zero process exit; neither is ever swallowed.
Constants§
- DEFAULT_
DRAIN_ TIMEOUT - Wall-clock ceiling on the drain: how long to wait for in-flight requests to
finish after the listener stops accepting. Operators must size the unit
TimeoutStopSecat least this high plus a flush margin. - DEFAULT_
MAX_ CONCURRENT_ REQUESTS - Maximum concurrent in-flight requests before surplus load sheds with 503.
- DEFAULT_
MAX_ CONNECTIONS - Maximum simultaneously accepted TCP connections.
- DEFAULT_
REQUEST_ TIMEOUT - Per-request processing ceiling before the request is denied with 408.
Functions§
- apply_
server_ hygiene - Wrap
routerwith the configured request timeout, concurrency limit fronted by load shedding, and optional body-size cap. - run_
until_ drained - Run a graceful serve future to completion, bounding only the post-signal drain, then run a flush hook.
- shutdown_
signal - Resolve on the first SIGTERM (unix) or Ctrl-C / SIGINT (all platforms).