How hashicorp/yamux works
Purpose and Entry Points
Yamux is a Go library that multiplexes many bidirectional, net.Conn-like logical streams over one reliable ordered transport (for example TCP or Unix sockets). Public construction starts with client/server session setup and exposes stream open/accept operations through:
• session.go
• stream.go
• mux.go
Protocol Boundary
The library owns framing, stream IDs, flags, flow-control windows, and protocol error handling above the underlying connection. The transport only supplies ordered, reliable bytes; Yamux is not an HTTP/SPDY implementation or interoperable with SPDY.
• const.go
• addr.go
• spec.md
Session Lifecycle and Control Plane
A Session wraps the physical connection, assigns stream identifiers according to client/server role, coordinates shutdown, and manages concurrently active streams. It provides the control boundary for opening local streams and accepting peer-created streams.
• session.go
• mux.go
Frame Multiplexing Loop
The session reads framed protocol messages from the shared transport, validates headers, routes data/control frames to the target stream, and serializes outbound frames back onto the one underlying connection. This is the principal concurrency and ordering boundary: individual streams may operate concurrently, while transport writes must remain coherent.
• session.go
• mux.go
• const.go
Logical Stream Data Plane
Each Stream implements stream-oriented connection behavior for callers: reads consume inbound payloads, writes emit framed data, and close/reset semantics terminate one logical channel without necessarily closing the session. Streams support creation by either peer, enabling server push and NAT-traversal-style bidirectional initiation.
• stream.go
• session.go
Flow Control and Backpressure
Per-stream/session protocol state tracks receive windows and window-update signaling so senders cannot indefinitely overwhelm receivers. Read progress drives capacity updates; write paths respect available flow-control credit. This protects fairness across thousands of low-overhead logical streams sharing one connection.
• stream.go
• session.go
• const.go
Keepalive, Errors, and Infrastructure
Session control frames support keepalives for persistent connections, including connections passing through load balancers. Protocol violations, transport failures, and session closure propagate to affected streams and unblock pending operations. The module is a Go package with no service runtime, database, or required external infrastructure; callers provide the underlying net.Conn.
• session.go
• go.mod
• spec.md
Main entry points: mux.go Frame multiplexing loop · session.go Session · stream.go Logical stream · const.go Frame protocol
Components 16
Public API
- Embedding Go caller · Supplies a reliable ordered net.Conn and uses streams.
- Session construction · Creates client or server sessions over a connection.
mux.go - Session API · Opens local streams and accepts peer-created streams.
session.go - Stream API · Provides net.Conn-like read, write, and close behavior.
stream.go
Session Control
- Session · Owns the physical connection, stream registry, role-based IDs, and shutdown.
session.go - Frame multiplexing loop · Reads, validates, routes, and serializes protocol frames.
mux.go - Active stream registry · Coordinates active streams and peer stream acceptance.
session.go - Error & shutdown propagation · Unblocks streams and pending operations on failure or closure.
session.go
Protocol & Transport
- Frame protocol · Defines headers, stream IDs, flags, windows, and control messages.
const.go - Protocol specification · Documents Yamux framing and semantics; it is not SPDY.
spec.md - Reliable ordered transport · Caller-provided TCP, Unix socket, or equivalent byte connection.
- Address support
addr.go - Go module · Library package with no required service infrastructure.
go.mod
Logical Streams
- Logical stream · Independent multiplexed channel that either peer can create.
stream.go - Stream read/write paths · Consumes inbound payloads and emits outbound data frames.
stream.go - Flow control · Tracks windows and sends updates as reads restore capacity.
stream.go
Connections 22
- Embedding Go caller → Session construction: creates session
- Embedding Go caller → Reliable ordered transport: provides
- Session construction → Session: constructs
- Session API → Session: controls
- Stream API → Logical stream: operates
- Session → Frame multiplexing loop: runs
- Session → Active stream registry: manages
- Session → Error & shutdown propagation: coordinates
- Session → Reliable ordered transport: wraps
- Frame multiplexing loop → Reliable ordered transport: reads and serializes frames
- Frame multiplexing loop → Frame protocol: validates and encodes
- Frame multiplexing loop → Active stream registry: routes frames
- Active stream registry → Logical stream: tracks and accepts
- Logical stream → Stream read/write paths: implements
- Stream read/write paths → Frame multiplexing loop: outbound data frames
- Frame multiplexing loop → Stream read/write paths: inbound payloads
- Logical stream → Flow control: maintains
- Flow control → Frame multiplexing loop: window updates and credit
- Frame multiplexing loop → Error & shutdown propagation: protocol or transport errors
- Error & shutdown propagation → Logical stream: unblocks and closes
- Protocol specification → Frame protocol: specifies
- Address support → Logical stream: exposes addresses