Admission control
Without a cap, a burst of requests lands on a worker's event loop all at once and every request waits behind every other, including health checks and quick management calls. Admission control bounds that. Each worker admits up to a fixed number of requests, holds a bounded queue of further requests for a short time, and rejects the rest immediately with a 503 that tells the client to retry. Clients see a fast, honest failure under overload instead of a timeout, and the retry goes to a worker with capacity.
A real-world scenario
A marketing site launches a chat assistant and traffic triples in a minute. Without a cap, each gateway worker takes every request onto its event loop, latency climbs for everyone, health probes time out and the orchestrator restarts healthy pods. With max_in_flight_requests_per_worker: 64 and a one second queue, each worker serves 64 requests at a time, holds the next 64 for up to a second, and answers the rest with a fast 503 and Retry-After: 1. The assistant's client retries, the retries land on workers with room, probes keep answering, and the on-call engineer reads rejected_requests from /health/backlog to decide how many pods to add.
Configuration
general_settings:
max_in_flight_requests_per_worker: 64
max_queued_requests_per_worker: 64
admission_queue_timeout_seconds: 1.0
| Key | Default | Meaning |
|---|---|---|
max_in_flight_requests_per_worker | unset, which turns admission control off | Turning it on protects a busy gateway: instead of piling up requests until every caller is slow, each worker runs at most this many at once and the extra ones wait briefly or get a quick 503 they can retry. Off, every request is accepted however busy the worker is. Must be greater than 0. The platform owner who sizes the gateway should set it. |
max_queued_requests_per_worker | same as in flight | Requests allowed to wait for a slot. 0 rejects as soon as the worker is full. Only used while admission control is on. |
admission_queue_timeout_seconds | 1.0 | Longest a queued request waits before it is rejected with 503. Only used while admission control is on. |
The three keys can be set in config.yaml or in the Admin UI under Router Settings, General, where each field shows the same help. A change made there applies on the next request without a restart, the cap included. Requests already running or waiting when the cap changes finish under the old cap.
The middleware runs before routing and authentication, so rejected requests cost almost nothing. The liveness, readiness, backlog and drain health routes and /metrics are exempt so monitoring keeps working under load. Invalid values, such as a negative queue size, are logged and disable admission control rather than crashing the worker. A request that the gateway replays internally, for example the hosted admin MCP server calling the management API, inherits the lease of the outer request rather than taking a second slot.
What clients see
{
"error": {
"message": "Worker at capacity: 64 in-flight, 64 queued requests. Retry later.",
"type": "overloaded_error",
"code": "503"
}
}
The response carries Retry-After: 1. Rejections happen for two reasons, queue_full when the queue is at its limit and queue_timeout when a queued request waited longer than the timeout.
Observing it
GET /health/backlog reports the worker that answered.
{"in_flight_requests": 71, "admitted_requests": 64, "queued_requests": 7, "rejected_requests": 1203}
Prometheus exposes spanway_admission_admitted_requests and spanway_admission_queued_requests as gauges and spanway_admission_rejected_requests_total as a counter with a reason label, summed across workers when PROMETHEUS_MULTIPROC_DIR is set. Size the cap from those numbers. A queue that is often full with low CPU means the cap is too low for the worker count, and rejections with high CPU mean more workers or instances are needed.
Step by step
-
In the Admin UI open Router Settings, then General, set max_in_flight_requests_per_worker to
64, max_queued_requests_per_worker to64and admission_queue_timeout_seconds to1, and save each one. They apply without a restart. Alternatively add the three keys togeneral_settingsinconfig.yamland roll the gateway. -
Read the counters of a worker. The route needs a valid key:
curl http://localhost:4000/health/backlog -H "Authorization: Bearer $SPANWAY_API_KEY"Expected result:
{"in_flight_requests": 71, "admitted_requests": 64, "queued_requests": 7, "rejected_requests": 1203} -
Send more concurrent requests than the cap, for example with a load tool, and watch some answers come back as 503 with
Retry-After: 1and theoverloaded_errorbody shown above. -
Read
spanway_admission_rejected_requests_totalbyreasonfrom/metrics, and in the Admin UI open Logs to confirm that rejected requests never reached a provider and cost nothing. -
Size the cap: a queue that is often full with low CPU means the cap is too low for the worker count, rejections with high CPU mean more workers or instances.
Errors
| Status or symptom | Meaning | Fix |
|---|---|---|
503 Worker at capacity: ... Retry later. with Retry-After: 1 | The worker is full and the queue is full (queue_full) or the request waited longer than the timeout (queue_timeout). | Retry after a second. Raise the cap or add workers when it is frequent. |
| Admission control is silently off | A value is invalid, for example a negative queue size or a cap of 0, and the gateway logged it and disabled the feature. | Fix the value, every cap must be greater than 0. |
/health/backlog shows admitted_requests: 0 under load | max_in_flight_requests_per_worker is unset, so no admission control runs. | Set it. |
| Health probes fail under load | Something other than admission control is saturating the worker, the probes themselves are exempt. | Check CPU and the backlog counters, see production checklist. |
Logs
| Level | Line | Meaning |
|---|---|---|
| ERROR | Ignoring invalid admission control settings, per-worker admission control is disabled: ... | A cap or queue value is invalid. Fix it, no request is limited until then. |
The log reference and the error reference list these entries too.
Related
- Health checks for
/health/backlog,/health/drainand the probes that stay exempt. - Prometheus for the admission metrics.
- Production checklist for workers and instances.
- Diagnostics endpoints for telling request load from background work.