TASK
Implementation
Implement gRPC streaming RPCs:
- Server streaming: Client sends one request, server sends a stream of responses
- Bidirectional streaming: Both sides send streams of messages concurrently
Use a log-watching service as the example:
Request: {"type": "grpc_server_stream", "msg_id": 1, "service": "LogWatcher", "method": "WatchLogs", "request": {"filter": "ERROR", "limit": 3}}
Response: [
{"type": "grpc_stream_msg", "in_reply_to": 1, "seq": 1, "data": {"level": "ERROR", "msg": "disk full"}},
{"type": "grpc_stream_msg", "in_reply_to": 1, "seq": 2, "data": {"level": "ERROR", "msg": "connection reset"}},
{"type": "grpc_stream_msg", "in_reply_to": 1, "seq": 3, "data": {"level": "ERROR", "msg": "timeout"}},
{"type": "grpc_stream_end", "in_reply_to": 1, "status": "OK", "count": 3}
]
Request: {"type": "grpc_bidi_stream_open", "msg_id": 2, "service": "Chat", "method": "BiDiChat"}
Response: {"type": "grpc_bidi_stream_open_ok", "in_reply_to": 2, "stream_id": "s1"}
Request: {"type": "grpc_bidi_stream_send", "msg_id": 3, "stream_id": "s1", "data": {"text": "hello"}}
Response: {"type": "grpc_bidi_stream_recv", "in_reply_to": 3, "data": {"text": "echo: hello"}}Sample Test Cases
Server streaming returns multiple messagesTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"grpc_server_stream","msg_id":2,"service":"LogWatcher","method":"WatchLogs","request":{"filter":"ERROR","limit":2}}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Bidi stream open and sendTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"grpc_bidi_stream_open","msg_id":2,"service":"Chat","method":"BiDiChat"}}
{"src":"c1","dest":"n1","body":{"type":"grpc_bidi_stream_send","msg_id":3,"stream_id":"s1","data":{"text":"hello"}}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
Server streaming: client sends one request, server sends multiple responses
Hint 2▾
Bidirectional streaming: both sides send multiple messages over the same connection
Hint 3▾
Each message in the stream uses the same gRPC framing (compressed flag + length)
Hint 4▾
The server signals end-of-stream with gRPC trailers (grpc-status)
Hint 5▾
Track the stream state: OPEN, HALF_CLOSED, CLOSED
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
server streamingbidirectional streamingHTTP/2 streamsflow control
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()