Table of Contents
Exercises — Month 5 · Week 1 (gRPC, Architecture & Microservices)¶
Standard-library-only exercises that rebuild the machinery beneath gRPC: the protobuf wire format, length-prefixed streaming frames, and the canonical status-code error model. Run all tests:
Prompts¶
1. varint — protobuf base-128 varints¶
Implement AppendUvarint/ReadUvarint (the encoding behind every protobuf
field tag and VARINT value) plus ZigZagEncode/ZigZagDecode for signed
sint fields. Handle truncated and overlong (>10-byte) input with errors.
- Each output byte carries 7 payload bits, low group first; bit
0x80= "more". - ZigZag maps
-1 -> 1,1 -> 2, so small negatives stay small. - Cross-check byte counts against
encoding/binary.PutUvarint.
2. frames — gRPC length-prefixed framing¶
Implement WriteFrame/ReadFrame/ReadAll for the 5-byte gRPC frame:
[1 flag byte][4 big-endian length bytes][payload].
- Use
io.ReadFull; a clean end-of-stream isio.EOF, a cut mid-frame isio.ErrUnexpectedEOF. - Bound the payload allocation with a
maxSizeto reject hostile length prefixes. - Zero-length payloads must round-trip.
3. statuscode — gRPC canonical status model¶
Implement the Code type (0–16) with String(), a Status{Code, Message}
error whose Err() returns nil for OK, FromError (plain errors -> Unknown,
wrapped statuses recovered via errors.As), and HTTPStatus (the grpc-gateway
code→HTTP table).
Results¶
| Exercise | Focus | Status |
|---|---|---|
varint |
protobuf varint + zigzag encoding | ✅ green |
frames |
length-prefixed streaming frames | ✅ green |
statuscode |
gRPC status codes + HTTP mapping | ✅ green |