Build one streaming endpoint
A stream holds a worker slot until completion or disconnect. The client can receive headers before it receives usable text.
Before you start
Use the Day 1 pool and the supplied mock worker. Start one worker first. Add the second after the direct stream works.
Read and predict 1 hour including video
Inference Engineering · §7.5 pp. 204–207; §7.2.3 pp. 190–191
Use the printed page numbers. In the supplied PDF, add two to reach the viewer page. Write a prediction for the question below before opening its answer.
Google SRE · Handling Overload · Designing Data-Intensive Applications
Build and test 3 hours
Build a streaming POST /v1/chat/completions proxy and two delayed mock workers. Give each request one monotonic deadline covering queueing and execution.
Start two mock workers
Make one worker emit “one”, wait 100 ms, then emit “two”. Use curl -N against it. Each chunk should appear separately.
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.
Forward one stream
Point the gateway at that worker. Use the example curl command in Gateway. Compare the order and timing with the direct call.
Fill the queue
Hold both workers busy, enqueue four waiting requests, then send another. Log arrival, admission and rejection times.
Troubleshooting
If the extra caller waits, look for await queue.put(). Use put_nowait() at admission and handle QueueFull.
Save your result 1 hour
Save gateway.py and a direct-versus-proxied stream trace. Record the queue capacity, active limit and rejection status.
Explain from memory 1 hour
Why can a semaphore of size two still leave a process out of memory?
Check your reasoning
It caps active holders. Callers waiting to acquire it can still retain request bodies and sockets. Bound admission as well.
Fill a four-slot queue, observe a 429 and show slots are released after disconnect.