TASK
Implementation
Implement application-level TCP keep-alive detection. If a client goes silent for more than 30 seconds, detect it and close the connection. Use periodic ping messages instead of OS-level keep-alive.
Implement handlers:
Request: {"type": "ka_register", "msg_id": 1, "client_id": "c1", "timeout_ms": 30000, "ping_interval_ms": 10000}
Response: {"type": "ka_register_ok", "in_reply_to": 1}
Request: {"type": "ka_heartbeat", "msg_id": 2, "client_id": "c1"}
Response: {"type": "ka_heartbeat_ok", "in_reply_to": 2, "last_seen_ms_ago": 0}
Request: {"type": "ka_check", "msg_id": 3, "current_time_ms": 45000}
Response: {"type": "ka_check_ok", "in_reply_to": 3, "expired": ["c1"], "active": ["c2"]}
Request: {"type": "ka_status", "msg_id": 4}
Response: {"type": "ka_status_ok", "in_reply_to": 4, "connections": [
{"client_id": "c2", "last_seen_ms_ago": 5000, "status": "active"}
]}Sample Test Cases
Register a client for keep-aliveTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"ka_register","msg_id":2,"client_id":"c1","timeout_ms":30000,"ping_interval_ms":10000}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "ka_register_ok", "in_reply_to": 2, "msg_id": 1}}
Heartbeat resets last_seenTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"ka_register","msg_id":2,"client_id":"c1","timeout_ms":30000,"ping_interval_ms":10000}}
{"src":"c1","dest":"n1","body":{"type":"ka_heartbeat","msg_id":3,"client_id":"c1"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "ka_register_ok", "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "ka_heartbeat_ok", "in_reply_to": 3, "last_seen_ms_ago": 0, "msg_id": 2}}
Hints
Hint 1▾
Send periodic ping messages to detect silent client failures
Hint 2▾
Track the last activity timestamp for each connection
Hint 3▾
If no activity for > 30s, send a ping. If no pong within 5s, close the connection
Hint 4▾
Do not rely on OS-level keep-alive; implement at the application layer
Hint 5▾
Maintain a connection state table with last_seen timestamps
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
keep-aliveheartbeatconnection healthidle detection
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()