Documentation
¶
Overview ¶
Package transport provides a long-lived WSS client with automatic reconnect, exponential backoff with jitter, and native ping/pong heartbeat. It is the binary's outbound network primitive to the relay.
The package is generic over frame payload. It accepts and emits []byte and knows nothing about pyrycode's protocol envelope, handshake, or routing. Protocol semantics live in internal/dispatch (future ticket); the wire-format types live in internal/protocol.
The single source of truth for the reconnect cadence and heartbeat constants is docs/protocol-mobile.md (§ Heartbeat, § Reconnect). When that document changes, this package changes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotConnected is returned by Send when there is no live conn. ErrNotConnected = errors.New("transport: not connected") // ErrClosed is returned by Send and Receive after Close (or the // parent context cancellation) has shut the client down. ErrClosed = errors.New("transport: client closed") // ErrDisconnected is returned by Receive when the underlying conn // dropped while Receive was blocked, or when no conn is currently // live. Callers observing this should NOT treat it as a re-handshake // trigger directly — observe Connected() for that. ErrDisconnected // means "your current Receive call returned because the wire dropped, // not because data arrived." ErrDisconnected = errors.New("transport: connection lost") // ErrFatalClose wraps a websocket close error whose status is in // Config.FatalCloseCodes. Returned by Connect; the underlying status // is recoverable via websocket.CloseStatus(err). ErrFatalClose = errors.New("transport: fatal close code") )
Sentinel errors.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client maintains a single long-lived WSS connection with auto-reconnect. Methods are concurrency-safe. The zero value is not usable — call New.
func (*Client) Connect ¶
Connect runs the dial-and-serve lifecycle until ctx is cancelled or Close is called. It returns ctx.Err() on shutdown or ErrClosed if Close was called; it never returns nil. Callers run it in its own goroutine.
Reconnect mechanics:
- On each failed dial, sleep backoff(attempt) (1s/2s/4s/8s/16s/30s cap, ±20% jitter). Attempt counter increments per attempt.
- On each successful dial, serve the conn (pump send/recv, ping every 30s). When the conn drops, record uptime; if uptime ≥ 60s reset the attempt counter to 1, otherwise increment.
- ctx cancellation breaks out of any sleep, any dial, any pump.
func (*Client) Connected ¶
func (c *Client) Connected() <-chan struct{}
Connected returns a channel that emits a value on every successful underlying conn. Buffer is 1 with drop-on-full semantics: a slow observer sees the most recent connect event, not every connect since boot. Multiple observers are NOT supported.
func (*Client) DropConn ¶
func (c *Client) DropConn()
DropConn force-closes the live conn (if any) abruptly (1006-like), without sending a close frame. The serve loop sees the closed conn, returns to the dial loop, and reconnects via backoff. DropConn does NOT halt the dial loop. Idempotent (safe to call when no conn is live).
CloseNow is used in preference to Close(status, reason) so the caller is not blocked for up to 10s waiting on a close handshake when the only purpose is to recycle the conn. Callers wishing to communicate a status to the peer should use Close on the application channel before invoking DropConn.
func (*Client) Receive ¶
Receive blocks until the next frame arrives, ctx is cancelled, the client is closed, or the underlying conn drops. Returns ErrDisconnected when the conn drops (or when no conn is currently live); the caller observes Connected() to learn when a fresh conn becomes available and re-runs any application-layer handshake on it.
func (*Client) Send ¶
Send writes a single frame to the relay. Returns ErrNotConnected if no live conn at call time, ErrClosed if Close was called, or ErrDisconnected if the live conn drops while Send is blocked enqueuing the frame. A nil return means the frame was queued for the send pump, not that it has hit the wire.
type Config ¶
type Config struct {
URL string
Headers http.Header
WriteTimeout time.Duration
// Logger receives structured lifecycle logs (dial, reconnect, ping
// timeout). Required; nil panics at New() time.
Logger *slog.Logger
// FatalCloseCodes lists WS close codes that terminate Connect's
// reconnect loop with ErrFatalClose. Empty (default) preserves the
// generic "reconnect on every drop" behaviour. The relay layer (#248)
// passes []websocket.StatusCode{4409} so a server-id conflict halts
// immediately rather than spinning in backoff.
FatalCloseCodes []websocket.StatusCode
}
Config carries the static configuration for a Client. The caller supplies the relay URL and any request headers (server-id, binary-version, protocol-versions); this package does not construct headers. WriteTimeout bounds per-frame send I/O — it is NOT an inactivity timeout; the inactivity contract is the ping/pong heartbeat.