Go Binary Protocols

From stream fragments to one complete frame with explicit buffer ownership.

1 / 9

Decode the length

AC 02 contributes 44 + (2 << 7) = 300.

  • Low seven bits carry data.
  • High bit requests another group.
  • Payload length excludes the prefix.
  • This complete frame consumes 302 bytes.
2 / 9

Tenth-byte boundary

  • Nine groups use 63 bits.
  • Tenth byte must terminate: only 00 or 01 is allowed.
  • Nine 80 bytes: incomplete.
  • Ten 80 bytes: malformed immediately.
  • Non-minimal 80 00: valid zero in this contract.
3 / 9

Receive-buffer trace

For prefix 82 01, length is 130:

Available bytes Action
82 Need more, consume 0
Prefix + 40-byte body Need more, consume 0
Prefix + 130-byte body Return body, consume 132

A retry receives the complete unconsumed buffer again.

4 / 9

Validate before slicing

  1. Incomplete or malformed prefix?
  2. Complete length over maxLen?
  3. After safe conversion, length > len(buf)-prefixBytes?
  4. Only then calculate the end and return a slice.

prefixBytes + length can overflow if checked too early.

5 / 9

One frame at a time

02 h i 01 ! contains two frames.

  • First returns hi, consumed 3.
  • Decode the remaining bytes after processing the alias.
  • Second returns !, consumed 2.
  • Every error returns nil,0,error.
6 / 9

Alias lifetime

  • payload shares buf's backing array.
  • Overwriting or pooling the buffer can change a saved payload.
  • Process before reuse, transfer ownership, or copy deliberately.
  • A short slice can retain a large allocation.
7 / 9

Measure correct implementations

  • Compare custom parsing with an appropriately adapted standard helper.
  • Allocate inputs before timing; preserve observable results.
  • Measure lengths, partial inputs, errors, time, and allocations.
  • Zero allocations alone does not establish speed or safe ownership.
8 / 9

Predict before implementing

85 01 encodes what length? What is the complete consumed count? Why does [05] with limit 4 differ from [80] with the same limit?

Check the lesson's reasoning, implement the decoder, then take the checkpoint.

9 / 9
←/→ or click edges to navigate · ? help · N notes · F fullscreen