# the-sarge/turn architecture

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

- Interactive diagram: https://gitdiagram.com/the-sarge/turn
- Repository: https://github.com/the-sarge/turn

## Overview

#### Purpose and Public Surface
`the-sarge/turn` is an owned Go UDP-only TURN client library for GridSwarm consumers such as wiremux. Its public lifecycle is: construct a client over a caller-owned `net.PacketConn`, allocate a relay, prepare peers, exchange relayed datagrams, and close. Primary API and exported error contracts reside in `client.go`, `allocation.go`, and `errors.go`.

#### Client and Allocation Lifecycle
`Client.Allocate(ctx)` initiates TURN allocation against a canonical `netip.AddrPort` server and returns the single stateful `*Allocation` object. Allocation owns relay lifetime, refresh behavior, prepared-peer state, relay reads/writes, and teardown. Core public orchestration is in `client.go` and `allocation.go`; lifecycle implementation is concentrated in `internal/client/allocation.go`.

#### TURN Transaction and Authentication Boundary
The internal client sends STUN/TURN requests, tracks request/response transactions, and handles protocol errors/authentication retries before exposing outcomes to the public API. `internal/client/transaction.go` is the request registry/control boundary; `internal/client/client.go` coordinates client operations; `internal/client/errors.go` maps internal failures into library behavior.

#### Peer Preparation and Data Plane
`Allocation.PreparePeer(ctx, peer)` establishes permission followed by channel binding. This is a hard gate: `WriteTo` emits ChannelData only for a confirmed prepared binding, with no Send-indication fallback. Permission and binding state machines are implemented in `internal/client/permission.go` and `internal/client/binding.go`; prepared-only write/read transport handling is in `internal/client/udp_conn.go`.

#### Refresh, Timers, and Failure Semantics
Allocation maintenance uses periodic timers to refresh allocations and likely related TURN resources. Refresh failure is material: it transitions the allocation into a typed failure condition (`ErrAllocationRefreshFailed`), while failed channel setup has distinct typed errors. The timing/control mechanics live in `internal/client/periodic_timer.go` and `internal/client/allocation.go`; public error identities are defined in `errors.go`.

#### Protocol Codec Boundary
TURN/STUN wire representation is isolated under `internal/proto`. It provides typed attributes and packet forms for allocation requests, channel data, addresses, lifetimes, and requested transport/family rather than leaking wire parsing into lifecycle code. Key paths are `internal/proto/proto.go`, `internal/proto/stun_conn.go`, and `internal/proto/chandata.go`. The library interoperates with a TURN server over caller-supplied UDP transport and uses `pion/stun` in its protocol/test ecosystem.

#### Verification Fixture and Delivery
`turntest` is an exported but test-only-in-practice in-process scripted TURN responder for this client and downstream integration tests; it is not a production TURN server. Its responder boundary is `turntest/turntest.go` and `turntest/respond.go`. CI/release automation is GitHub Actions-based, with ordinary validation in `.github/workflows/ci.yml`, deeper certification in `.github/workflows/deep-checks.yml`, and release packaging in `.github/workflows/release.yml`.

Main entry points: [`internal/client/allocation.go`](https://github.com/the-sarge/turn/blob/main/internal/client/allocation.go) (Allocation Lifecycle), [`client.go`](https://github.com/the-sarge/turn/blob/main/client.go) (Client API), [`internal/client/transaction.go`](https://github.com/the-sarge/turn/blob/main/internal/client/transaction.go) (Transaction Registry), [`turntest/turntest.go`](https://github.com/the-sarge/turn/blob/main/turntest/turntest.go) (Scripted TURN Responder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public Library"]
  node_client_api["Client API<br/>Go API<br/>[client.go]"]
  node_allocation_api["Allocation API<br/>Go API<br/>[allocation.go]"]
  node_public_errors["Public Errors<br/>error contracts<br/>[errors.go]"]
end

subgraph group_lifecycle["Client Lifecycle"]
  node_client_core["Client Coordinator<br/>internal client"]
  node_allocation_core["Allocation Lifecycle<br/>stateful allocation<br/>[allocation.go]"]
  node_transactions["Transaction Registry<br/>request control<br/>[transaction.go]"]
  node_permissions["Permission State<br/>peer authorization<br/>[permission.go]"]
  node_bindings["Channel Binding State<br/>peer channel<br/>[binding.go]"]
  node_relay_io["Prepared Relay I/O<br/>UDP data plane<br/>[udp_conn.go]"]
  node_timers(("Periodic Refresh Timer<br/>maintenance<br/>[periodic_timer.go]"))
  node_internal_errors["Internal Error Mapping<br/>error translation<br/>[errors.go]"]
end

subgraph group_protocol["Protocol Codec"]
  node_proto["STUN/TURN Types<br/>wire codec<br/>[proto.go]"]
  node_stun_conn["STUN Connection<br/>wire transport<br/>[stun_conn.go]"]
  node_channel_data["ChannelData Codec<br/>wire packet<br/>[chandata.go]"]
end

subgraph group_verification["Verification &amp; Delivery"]
  node_turntest["Scripted TURN Responder<br/>test fixture<br/>[turntest.go]"]
  node_turntest_responder["Responder Logic<br/>test protocol handler<br/>[respond.go]"]
  node_ci["CI Validation<br/>GitHub Actions<br/>[ci.yml]"]
  node_deep_checks["Deep Certification<br/>GitHub Actions<br/>[deep-checks.yml]"]
  node_release["Release Packaging<br/>GitHub Actions<br/>[release.yml]"]
end

node_packetconn{{"Caller UDP PacketConn<br/>transport"}}
node_turn_server{{"TURN Server<br/>external service"}}

node_packetconn -->|"constructs over"| node_client_api
node_client_api -->|"Allocate returns"| node_allocation_api
node_client_api -->|"delegates"| node_client_core
node_allocation_api -->|"controls"| node_allocation_core
node_client_core -->|"issues requests"| node_transactions
node_allocation_core -->|"allocation requests"| node_transactions
node_allocation_core -->|"PreparePeer: permission"| node_permissions
node_permissions -->|"then bind channel"| node_bindings
node_bindings -->|"confirmed binding enables"| node_relay_io
node_allocation_core -->|"schedules refresh"| node_timers
node_timers -->|"refreshes allocation"| node_allocation_core
node_allocation_core -->|"maps failures"| node_internal_errors
node_internal_errors -->|"exposes typed errors"| node_public_errors
node_transactions -->|"encodes and decodes"| node_proto
node_proto -->|"uses"| node_stun_conn
node_relay_io -->|"uses"| node_channel_data
node_stun_conn -->|"sends via"| node_packetconn
node_packetconn -->|"UDP"| node_turn_server
node_turntest -->|"implements"| node_turntest_responder
node_ci -.->|"validates with"| node_turntest
node_deep_checks -.->|"certifies with"| node_turntest
node_release -.->|"release validation"| node_ci

click node_client_api "https://github.com/the-sarge/turn/blob/main/client.go"
click node_allocation_api "https://github.com/the-sarge/turn/blob/main/allocation.go"
click node_public_errors "https://github.com/the-sarge/turn/blob/main/errors.go"
click node_allocation_core "https://github.com/the-sarge/turn/blob/main/internal/client/allocation.go"
click node_transactions "https://github.com/the-sarge/turn/blob/main/internal/client/transaction.go"
click node_permissions "https://github.com/the-sarge/turn/blob/main/internal/client/permission.go"
click node_bindings "https://github.com/the-sarge/turn/blob/main/internal/client/binding.go"
click node_relay_io "https://github.com/the-sarge/turn/blob/main/internal/client/udp_conn.go"
click node_timers "https://github.com/the-sarge/turn/blob/main/internal/client/periodic_timer.go"
click node_internal_errors "https://github.com/the-sarge/turn/blob/main/internal/client/errors.go"
click node_proto "https://github.com/the-sarge/turn/blob/main/internal/proto/proto.go"
click node_stun_conn "https://github.com/the-sarge/turn/blob/main/internal/proto/stun_conn.go"
click node_channel_data "https://github.com/the-sarge/turn/blob/main/internal/proto/chandata.go"
click node_turntest "https://github.com/the-sarge/turn/blob/main/turntest/turntest.go"
click node_turntest_responder "https://github.com/the-sarge/turn/blob/main/turntest/respond.go"
click node_ci "https://github.com/the-sarge/turn/blob/main/.github/workflows/ci.yml"
click node_deep_checks "https://github.com/the-sarge/turn/blob/main/.github/workflows/deep-checks.yml"
click node_release "https://github.com/the-sarge/turn/blob/main/.github/workflows/release.yml"

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_client_api,node_allocation_api,node_public_errors toneBlue
class node_client_core,node_allocation_core,node_transactions,node_permissions,node_bindings,node_relay_io,node_timers,node_internal_errors toneAmber
class node_proto,node_stun_conn,node_channel_data toneMint
class node_turntest,node_turntest_responder,node_ci,node_deep_checks,node_release toneRose
class node_packetconn,node_turn_server toneNeutral
```

## Components

### Public Library

- [Client API](https://github.com/the-sarge/turn/blob/main/client.go): Go API · `client.go`
- [Allocation API](https://github.com/the-sarge/turn/blob/main/allocation.go): Go API · Allocation, peer preparation, relay I/O, and close surface. · `allocation.go`
- [Public Errors](https://github.com/the-sarge/turn/blob/main/errors.go): error contracts · Includes typed allocation refresh and channel setup failures. · `errors.go`

### Client Lifecycle

- **Client Coordinator**: internal client · Coordinates allocation and TURN operations.
- [Allocation Lifecycle](https://github.com/the-sarge/turn/blob/main/internal/client/allocation.go): stateful allocation · Owns relay lifetime, refresh, peers, and teardown. · `internal/client/allocation.go`
- [Transaction Registry](https://github.com/the-sarge/turn/blob/main/internal/client/transaction.go): request control · Tracks TURN requests, responses, protocol errors, and auth retries. · `internal/client/transaction.go`
- [Permission State](https://github.com/the-sarge/turn/blob/main/internal/client/permission.go): peer authorization · `internal/client/permission.go`
- [Channel Binding State](https://github.com/the-sarge/turn/blob/main/internal/client/binding.go): peer channel · Confirmed bindings gate relay writes. · `internal/client/binding.go`
- [Prepared Relay I/O](https://github.com/the-sarge/turn/blob/main/internal/client/udp_conn.go): UDP data plane · Reads and writes ChannelData only for prepared peers. · `internal/client/udp_conn.go`
- [Periodic Refresh Timer](https://github.com/the-sarge/turn/blob/main/internal/client/periodic_timer.go): maintenance · `internal/client/periodic_timer.go`
- [Internal Error Mapping](https://github.com/the-sarge/turn/blob/main/internal/client/errors.go): error translation · `internal/client/errors.go`

### Protocol Codec

- [STUN/TURN Types](https://github.com/the-sarge/turn/blob/main/internal/proto/proto.go): wire codec · Typed requests, attributes, addresses, lifetimes, and transport settings. · `internal/proto/proto.go`
- [STUN Connection](https://github.com/the-sarge/turn/blob/main/internal/proto/stun_conn.go): wire transport · `internal/proto/stun_conn.go`
- [ChannelData Codec](https://github.com/the-sarge/turn/blob/main/internal/proto/chandata.go): wire packet · `internal/proto/chandata.go`

### Verification & Delivery

- [Scripted TURN Responder](https://github.com/the-sarge/turn/blob/main/turntest/turntest.go): test fixture · In-process responder for client and downstream integration tests; not a production server. · `turntest/turntest.go`
- [Responder Logic](https://github.com/the-sarge/turn/blob/main/turntest/respond.go): test protocol handler · `turntest/respond.go`
- [CI Validation](https://github.com/the-sarge/turn/blob/main/.github/workflows/ci.yml): GitHub Actions · `.github/workflows/ci.yml`
- [Deep Certification](https://github.com/the-sarge/turn/blob/main/.github/workflows/deep-checks.yml): GitHub Actions · `.github/workflows/deep-checks.yml`
- [Release Packaging](https://github.com/the-sarge/turn/blob/main/.github/workflows/release.yml): GitHub Actions · `.github/workflows/release.yml`

### Other components

- **Caller UDP PacketConn**: transport · Caller-owned UDP transport.
- **TURN Server**: external service

## Connections

- Caller UDP PacketConn → Client API: constructs over
- Client API → Allocation API: Allocate returns
- Client API → Client Coordinator: delegates
- Allocation API → Allocation Lifecycle: controls
- Client Coordinator → Transaction Registry: issues requests
- Allocation Lifecycle → Transaction Registry: allocation requests
- Allocation Lifecycle → Permission State: PreparePeer: permission
- Permission State → Channel Binding State: then bind channel
- Channel Binding State → Prepared Relay I/O: confirmed binding enables
- Allocation Lifecycle → Periodic Refresh Timer: schedules refresh
- Periodic Refresh Timer → Allocation Lifecycle: refreshes allocation
- Allocation Lifecycle → Internal Error Mapping: maps failures
- Internal Error Mapping → Public Errors: exposes typed errors
- Transaction Registry → STUN/TURN Types: encodes and decodes
- STUN/TURN Types → STUN Connection: uses
- Prepared Relay I/O → ChannelData Codec: uses
- STUN Connection → Caller UDP PacketConn: sends via
- Caller UDP PacketConn → TURN Server: UDP
- Scripted TURN Responder → Responder Logic: implements
- CI Validation → Scripted TURN Responder: validates with
- Deep Certification → Scripted TURN Responder: certifies with
- Release Packaging → CI Validation: release validation
