# hashicorp/yamux architecture

> Architecture diagram of the GitHub repository hashicorp/yamux, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hashicorp/yamux
- Repository: https://github.com/hashicorp/yamux

## Overview

#### 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`](https://github.com/hashicorp/yamux/blob/master/mux.go) (Frame multiplexing loop), [`session.go`](https://github.com/hashicorp/yamux/blob/master/session.go) (Session), [`stream.go`](https://github.com/hashicorp/yamux/blob/master/stream.go) (Logical stream), [`const.go`](https://github.com/hashicorp/yamux/blob/master/const.go) (Frame protocol)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public API"]
  node_caller(("Embedding Go caller<br/>consumer"))
  node_mux_api["Session construction<br/>API<br/>[mux.go]"]
  node_session_api["Session API<br/>[session.go]"]
  node_stream_api["Stream API<br/>[stream.go]"]
end

subgraph group_session["Session Control"]
  node_session{{"Session<br/>connection coordinator<br/>[session.go]"}}
  node_mux_loop["Frame multiplexing loop<br/>read/write coordinator<br/>[mux.go]"]
  node_stream_registry["Active stream registry<br/>concurrent state<br/>[session.go]"]
  node_shutdown["Error &amp; shutdown propagation<br/>lifecycle handling<br/>[session.go]"]
end

subgraph group_protocol["Protocol &amp; Transport"]
  node_frame_protocol["Frame protocol<br/>wire format<br/>[const.go]"]
  node_protocol_spec["Protocol specification<br/>protocol contract<br/>[spec.md]"]
  node_transport(("Reliable ordered transport<br/>net.Conn"))
  node_addressing["Address support<br/>net.Addr support<br/>[addr.go]"]
  node_module["Go module<br/>package metadata<br/>[go.mod]"]
end

subgraph group_streams["Logical Streams"]
  node_stream{{"Logical stream<br/>bidirectional channel<br/>[stream.go]"}}
  node_stream_io["Stream read/write paths<br/>data plane<br/>[stream.go]"]
  node_flow_control["Flow control<br/>backpressure state<br/>[stream.go]"]
end

node_caller -->|"creates session"| node_mux_api
node_caller -->|"provides"| node_transport
node_mux_api -->|"constructs"| node_session
node_session_api -->|"controls"| node_session
node_stream_api -->|"operates"| node_stream
node_session -->|"runs"| node_mux_loop
node_session -->|"manages"| node_stream_registry
node_session -->|"coordinates"| node_shutdown
node_session -->|"wraps"| node_transport
node_mux_loop -->|"reads and serializes frames"| node_transport
node_mux_loop -->|"validates and encodes"| node_frame_protocol
node_mux_loop -->|"routes frames"| node_stream_registry
node_stream_registry -->|"tracks and accepts"| node_stream
node_stream -->|"implements"| node_stream_io
node_stream_io -->|"outbound data frames"| node_mux_loop
node_mux_loop -->|"inbound payloads"| node_stream_io
node_stream -->|"maintains"| node_flow_control
node_flow_control -->|"window updates and credit"| node_mux_loop
node_mux_loop -->|"protocol or transport errors"| node_shutdown
node_shutdown -->|"unblocks and closes"| node_stream
node_protocol_spec -.->|"specifies"| node_frame_protocol
node_addressing -.->|"exposes addresses"| node_stream

click node_mux_api "https://github.com/hashicorp/yamux/blob/master/mux.go"
click node_session_api "https://github.com/hashicorp/yamux/blob/master/session.go"
click node_stream_api "https://github.com/hashicorp/yamux/blob/master/stream.go"
click node_session "https://github.com/hashicorp/yamux/blob/master/session.go"
click node_mux_loop "https://github.com/hashicorp/yamux/blob/master/mux.go"
click node_stream_registry "https://github.com/hashicorp/yamux/blob/master/session.go"
click node_shutdown "https://github.com/hashicorp/yamux/blob/master/session.go"
click node_stream "https://github.com/hashicorp/yamux/blob/master/stream.go"
click node_stream_io "https://github.com/hashicorp/yamux/blob/master/stream.go"
click node_flow_control "https://github.com/hashicorp/yamux/blob/master/stream.go"
click node_frame_protocol "https://github.com/hashicorp/yamux/blob/master/const.go"
click node_protocol_spec "https://github.com/hashicorp/yamux/blob/master/spec.md"
click node_addressing "https://github.com/hashicorp/yamux/blob/master/addr.go"
click node_module "https://github.com/hashicorp/yamux/blob/master/go.mod"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_caller,node_mux_api,node_session_api,node_stream_api toneBlue
class node_session,node_mux_loop,node_stream_registry,node_shutdown toneAmber
class node_frame_protocol,node_protocol_spec,node_transport,node_addressing,node_module toneMint
class node_stream,node_stream_io,node_flow_control toneRose
```

## Components

### Public API

- **Embedding Go caller**: consumer · Supplies a reliable ordered net.Conn and uses streams.
- [Session construction](https://github.com/hashicorp/yamux/blob/master/mux.go): API · Creates client or server sessions over a connection. · `mux.go`
- [Session API](https://github.com/hashicorp/yamux/blob/master/session.go): API · Opens local streams and accepts peer-created streams. · `session.go`
- [Stream API](https://github.com/hashicorp/yamux/blob/master/stream.go): API · Provides net.Conn-like read, write, and close behavior. · `stream.go`

### Session Control

- [Session](https://github.com/hashicorp/yamux/blob/master/session.go): connection coordinator · Owns the physical connection, stream registry, role-based IDs, and shutdown. · `session.go`
- [Frame multiplexing loop](https://github.com/hashicorp/yamux/blob/master/mux.go): read/write coordinator · Reads, validates, routes, and serializes protocol frames. · `mux.go`
- [Active stream registry](https://github.com/hashicorp/yamux/blob/master/session.go): concurrent state · Coordinates active streams and peer stream acceptance. · `session.go`
- [Error & shutdown propagation](https://github.com/hashicorp/yamux/blob/master/session.go): lifecycle handling · Unblocks streams and pending operations on failure or closure. · `session.go`

### Protocol & Transport

- [Frame protocol](https://github.com/hashicorp/yamux/blob/master/const.go): wire format · Defines headers, stream IDs, flags, windows, and control messages. · `const.go`
- [Protocol specification](https://github.com/hashicorp/yamux/blob/master/spec.md): protocol contract · Documents Yamux framing and semantics; it is not SPDY. · `spec.md`
- **Reliable ordered transport**: net.Conn · Caller-provided TCP, Unix socket, or equivalent byte connection.
- [Address support](https://github.com/hashicorp/yamux/blob/master/addr.go): net.Addr support · `addr.go`
- [Go module](https://github.com/hashicorp/yamux/blob/master/go.mod): package metadata · Library package with no required service infrastructure. · `go.mod`

### Logical Streams

- [Logical stream](https://github.com/hashicorp/yamux/blob/master/stream.go): bidirectional channel · Independent multiplexed channel that either peer can create. · `stream.go`
- [Stream read/write paths](https://github.com/hashicorp/yamux/blob/master/stream.go): data plane · Consumes inbound payloads and emits outbound data frames. · `stream.go`
- [Flow control](https://github.com/hashicorp/yamux/blob/master/stream.go): backpressure state · Tracks windows and sends updates as reads restore capacity. · `stream.go`

## Connections

- 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
