Expand description
Bounded handler-thread admission for the clear-text HTTP accept loop.
Slice 1 of issue #570 / parent #569. The synchronous HTTP transport
spawns one OS thread per accepted connection. Without an admission
cap the server can degrade into thread-storm and lock starvation
under load. HttpConnectionLimiter is a single AtomicUsize-backed
semaphore consulted in the accept loop before parsing or handler
work. A rejected connection gets a static 503 + Retry-After written
and the socket closed without ever entering the runtime.
Hard cap for this slice is (2 * available_parallelism).clamp(8, 256).
Config knobs (env / CLI) land in slice 5 per the parent brief.
Beyond admission, the limiter keeps a single rejection counter and an
injectable monotonic clock (issue #620). Every try_acquire that hits
the cap bumps the counter; observe() snapshots-and-resets it against
the elapsed wall to derive a rejection rate. v1 ships a constant
Retry-After; the rate signal is what a future v2 will use to make
Retry-After adaptive. The clock is a trait so tests drive the rate
deterministically without sleeping.
Structs§
- Handler
Deadline - Per-handler total wall-clock deadline (issue #621), armed against the
same
MonotonicClockabstraction the limiter uses. The clear-text (and TLS) HTTP handler arms one of these at spawn and pollsexpiredat coarse boundaries (between parse, route dispatch, and write). Production wiresSystemMonotonicClock, so the deadline tracks real wall time; tests inject a fake clock to drive expiry deterministically withoutsleep(). - Http
Connection Limiter - Http
Connection Permit - Permit handle — owns one slot of the limiter. Dropping the permit
returns the slot. The permit is intentionally
!Cloneso the slot accounting can’t drift. - Limiter
Observation - Snapshot returned by
HttpConnectionLimiter::observe: the rejections accumulated since the previous observe, the wall elapsed across that window, and the derived rate.rejections_per_secis0.0for a zero-length window (no time has passed) so callers never divide by zero. - System
Monotonic Clock - Real monotonic clock: nanoseconds since the limiter’s construction.
Traits§
- Monotonic
Clock - Monotonic clock abstraction. Production uses
SystemMonotonicClock(a process-startInstantbaseline); tests inject a fake that can be advanced by hand so the rejection-rate derivation is deterministic.