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
- type Conn
- func (c *Conn) Close(code websocket.StatusCode, reason string)
- func (c *Conn) Context() context.Context
- func (c *Conn) Done() <-chan struct{}
- func (c *Conn) End()
- func (c *Conn) Read() (websocket.MessageType, []byte, error)
- func (c *Conn) Send(m Message) bool
- func (c *Conn) SendBinary(p []byte) bool
- func (c *Conn) SendBinaryWait(p []byte) bool
- func (c *Conn) SendData(v any) bool
- func (c *Conn) SendDataWait(v any) bool
- func (c *Conn) SendError(code, message string) bool
- type Message
- type Options
Constants ¶
const ( TypeData = "data" TypeError = "error" TypeEnd = "end" )
Message types of the text-frame envelope.
const ( ReasonSlowConsumer = "slow consumer" ReasonServerClose = "server closing" )
Close reasons sent to the client.
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 ¶
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) 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 ¶
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 ¶
SendBinary queues a binary frame (exec output). Empty payloads are ignored.
func (*Conn) SendBinaryWait ¶
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) SendDataWait ¶
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.
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.