Load-aware gateway
Start with two mock workers on your computer. Add a real model after streaming, deadlines and cancellation pass locally.
client → gateway → bounded queue → ready worker → token stream
↑ ↓
planner ← queue + capacity + healthRun the mock workers
Download mock_worker.py. Run each line in a separate terminal from the download directory. The final argument sets the delay between chunks in seconds.
python3 mock_worker.py 8001 0.1 python3 mock_worker.py 8002 0.2
curl -N http://localhost:8001/v1/chat/completions -H 'Content-Type: application/json' -d '{"model":"lab","messages":[{"role":"user","content":"count"}],"stream":true}'Expect five content events followed by [DONE]. Each mock event represents one synthetic token. Real API chunks may contain zero or multiple tokens.
Worker contract
Use Python 3.11+, an async HTTP framework and an async client. Have each mock worker accept POST /v1/chat/completions, sleep through a configurable prefill delay and emit content chunks via server-sent events followed by [DONE]. Give one worker twice the decode delay. Keep the same model name across workers.
02 / Admit, reserve, release
Set queue capacity to four and each worker capacity to two streams. Reject excess arrivals with 429; return 503 when no backend can serve, and 504 for an expired deadline before response headers. Reserve in-flight capacity inside one critical section. Release exactly once after completion, timeout, cancellation or upstream failure.
03 / Test the wire contract
Pass through status, content type and chunks as they arrive. Propagate disconnects upstream. A retry before first output still risks duplicate compute: allow at most one bounded retry on a different healthy worker within the original deadline. Once output starts, terminate a failed stream visibly. A client must be able to distinguish it from normal completion.
curl -N http://localhost:9000/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{"model":"lab","stream":true,"messages":[{"role":"user","content":"Count to five."}]}'04 / Reconcile capacity
For the simulator, try desired = clamp(ceil((active + queued) / 2), 1, 4). Count ready and starting replicas toward desired. Drain before removal. A periodic loop must converge after a repeated observation. On Kubernetes publish a queue metric for an HPA or use one dedicated controller; assign a single writer to the replica count.
05 / Demonstrate the result
Ship a README, gateway, two worker profiles, local startup command, test suite, Kubernetes manifests and a results table. Include round-robin versus least-loaded under a slow replica. On a GPU point the gateway at vLLM with --served-model-name lab. Keep simulated and hardware reports separate.
Acceptance checks
Drive eight active or queued requests and send a ninth. Verify the bound. Kill a worker both before and after its first token. Cancel a queued request. Restart a worker and verify that routing resumes only after readiness. After the test, inflight and queued gauges must both return to zero.