TASK
Implementation
Implement graceful shutdown: when the server receives a shutdown signal, it should drain all in-flight requests before closing sockets. New connections are rejected during draining.
Implement handlers:
Request: {"type": "tcp_request", "msg_id": 1, "data": "process_this", "latency_ms": 500}
Response: {"type": "tcp_request_ok", "in_reply_to": 1, "result": "processed", "duration_ms": 500}
Request: {"type": "tcp_shutdown", "msg_id": 2, "drain_timeout_ms": 5000}
Response: {"type": "tcp_shutdown_ok", "in_reply_to": 2, "status": "draining", "in_flight": 2}
Request: {"type": "tcp_request", "msg_id": 3, "data": "new_request", "latency_ms": 100}
Response: {"type": "tcp_request_error", "in_reply_to": 3, "error": "server_shutting_down"}
Request: {"type": "tcp_drain_status", "msg_id": 4}
Response: {"type": "tcp_drain_status_ok", "in_reply_to": 4, "status": "drained", "remaining": 0, "took_ms": 500}Sample Test Cases
Normal request processingTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"tcp_request","msg_id":2,"data":"hello","latency_ms":0}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "tcp_request_ok", "in_reply_to": 2, "result": "processed", "duration_ms": 0, "msg_id": 1}}
Shutdown starts drainTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"tcp_shutdown","msg_id":2,"drain_timeout_ms":5000}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "tcp_shutdown_ok", "in_reply_to": 2, "status": "draining", "in_flight": 0, "msg_id": 1}}
Hints
Hint 1▾
On shutdown signal, stop accepting new connections immediately
Hint 2▾
Wait for all in-flight requests to complete before closing sockets
Hint 3▾
Track the number of in-flight requests with a counter
Hint 4▾
Set a maximum drain timeout: force-close after N seconds
Hint 5▾
Test by sending requests and initiating shutdown mid-flight
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
graceful shutdowndrainin-flight requestsconnection lifecycle
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()