ws

package
v0.2.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ws is the shared WebSocket layer for streams (ADR-0007): logs, stats, events, job progress and exec.

Security: Accept rejects cross-origin upgrades (the browser's Origin must match Host), which is the CSRF protection for WebSocket; session and permission checks run as regular route middleware before the upgrade.

Flow control: Send never blocks the producer. Each connection has a bounded queue; a client that cannot keep up is disconnected with StatusPolicyViolation instead of slowing down containerd readers.

Index

Constants

View Source
const (
	TypeData  = "data"
	TypeError = "error"
	TypeEnd   = "end"
)

Message types of the text-frame envelope.

View Source
const (
	ReasonSlowConsumer = "slow consumer"
	ReasonServerClose  = "server closing"
)

Close reasons sent to the client.

View Source
const StatusTryAgainLater = websocket.StatusTryAgainLater

StatusTryAgainLater (1013) tells clients to reconnect, e.g. after falling behind.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn is a WebSocket stream with a non-blocking send queue.

func Accept

func Accept(w http.ResponseWriter, r *http.Request, opts Options) (*Conn, error)

Accept upgrades the request. On failure the response has already been written (403 for cross-origin requests) and an error is returned.

func (*Conn) Close

func (c *Conn) Close(code websocket.StatusCode, reason string)

Close closes the connection with code and reason after a close handshake (which waits up to a few seconds for an unresponsive peer). It is safe to call more than once.

func (*Conn) Context

func (c *Conn) Context() context.Context

Context is canceled when the connection ends for any reason.

func (*Conn) Done

func (c *Conn) Done() <-chan struct{}

Done is closed when the connection has been closed.

func (*Conn) End

func (c *Conn) End()

End sends the end message, closes the connection normally after it was delivered and returns when the connection is closed. It waits for queue space: the stream is over, and a full queue must not turn the end into a slow-consumer close.

func (*Conn) Read

func (c *Conn) Read() (websocket.MessageType, []byte, error)

Read returns the next incoming message (Bidirectional connections only).

func (*Conn) Send

func (c *Conn) Send(m Message) bool

Send queues a JSON message. It never blocks: when the queue is full the client is disconnected as a slow consumer and false is returned.

func (*Conn) SendBinary

func (c *Conn) SendBinary(p []byte) bool

SendBinary queues a binary frame (exec output). Empty payloads are ignored.

func (*Conn) SendBinaryWait

func (c *Conn) SendBinaryWait(p []byte) bool

SendBinaryWait queues a binary frame, waiting for queue space instead of disconnecting a slow client: exec output is paced to the client like logs (ADR-0014). The data is copied.

func (*Conn) SendData

func (c *Conn) SendData(v any) bool

SendData queues a data message.

func (*Conn) SendDataWait

func (c *Conn) SendDataWait(v any) bool

SendDataWait queues a data message, waiting while the queue is full instead of disconnecting the client. It is for producers that can pause without harm, such as log files read for this connection only, unlike shared containerd readers. It returns false when the connection has closed.

func (*Conn) SendError

func (c *Conn) SendError(code, message string) bool

SendError queues an error message; streams usually call End or Close afterwards.

type Message

type Message struct {
	Type  string                `json:"type"`
	Data  any                   `json:"data,omitempty"`
	Error *httpjson.ErrorDetail `json:"error,omitempty"`
	TS    time.Time             `json:"ts"`
}

Message is the JSON envelope of text frames:

{"type": "data", "data": ..., "ts": "2026-11-11T12:00:00Z"}
{"type": "error", "error": {"code": "...", "message": "..."}, "ts": "..."}
{"type": "end", "ts": "..."}

type Options

type Options struct {
	// SendQueue is the number of queued outgoing frames before the client is considered
	// too slow and disconnected (default 256).
	SendQueue int
	// PingInterval is the keep-alive interval (default 30s).
	PingInterval time.Duration
	// WriteTimeout bounds a single frame write or ping (default 10s).
	WriteTimeout time.Duration
	// ReadLimit is the maximum size of an incoming message (default 64 KiB).
	ReadLimit int64
	// Bidirectional keeps incoming data messages for Read (exec). Otherwise incoming data
	// closes the connection with StatusPolicyViolation and Context ends when the client
	// disconnects.
	Bidirectional bool
}

Options configures a connection. Zero values select the defaults.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL