ws

package
v2.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accept added in v2.7.0

func Accept(w http.ResponseWriter, r *http.Request, checkOrigin func(*http.Request) bool) (*websocket.Conn, error)

Accept validates the request Origin with checkOrigin and upgrades it to a WebSocket. The origin check is the CSRF barrier for cookie-authenticated upgrades; a nil checkOrigin skips it and must only be used for endpoints authenticated by tokens or mTLS rather than browser cookies.

func CountWorkerGoroutines

func CountWorkerGoroutines() int

CountWorkerGoroutines returns the number of websocket worker goroutines belonging to this package. Intended for diagnostics endpoints only.

func ForwardLines

func ForwardLines(ctx context.Context, hub *Hub, lines <-chan string)

ForwardLines forwards plain text lines to the hub.

func ForwardLogJSON

func ForwardLogJSON(ctx context.Context, hub *Hub, logs <-chan LogMessage)

ForwardLogJSON sends each LogMessage as its own JSON object frame.

func ForwardLogJSONBatched

func ForwardLogJSONBatched(ctx context.Context, hub *Hub, logs <-chan LogMessage, maxBatch int, flushInterval time.Duration)

ForwardLogJSONBatched batches log messages into a JSON array frame to reduce frame count. Flushes when maxBatch reached or flushInterval elapsed.

func IsExpectedClose added in v2.7.0

func IsExpectedClose(err error) bool

IsExpectedClose reports whether err is a WebSocket close error carrying one of the benign statuses peers send on ordinary disconnect.

func NormalizeContainerLine

func NormalizeContainerLine(raw string) (level string, msg string, timestamp string)

NormalizeContainerLine parses a raw container log line into level + cleaned message. It extracts Docker's timestamp if present (when timestamps=true in Docker API).

func NormalizeProjectLine

func NormalizeProjectLine(raw string) (level, service, msg, timestamp string)

NormalizeProjectLine additionally extracts service (pattern: service | message). Returns level, service, message, timestamp (RFC3339Nano) — timestamp may be empty.

func NowRFC3339

func NowRFC3339() string

func ProxyHTTP

func ProxyHTTP(w http.ResponseWriter, r *http.Request, remoteWS string, header http.Header, checkOrigin func(*http.Request) bool) error

ProxyHTTP upgrades the incoming client connection and bridges it to remoteWS.

checkOrigin must be the same Origin validator the local WebSocket endpoints use. It is required: this upgrade is reached with the caller's session cookie already validated, so accepting any Origin would let an attacker-controlled page open a terminal or log stream in a remote environment.

func ServeClientWithOnRemove

func ServeClientWithOnRemove(ctx context.Context, hub *Hub, conn *websocket.Conn, onRemove func()) bool

ServeClientWithOnRemove registers the client with the hub and starts read/write pumps. Caller is responsible for creating/closing the websocket.Conn. If onRemove is non-nil, it is invoked when the client is removed from the hub.

It reports whether registration succeeded. Registration fails when the hub's Run has already exited (or ctx is done) — landing on that boundary used to block forever on the unbuffered register channel, leaking the caller's goroutine and the socket. On failure nothing is registered and onRemove is never invoked, so the caller can retry against a fresh hub with the same connection, or close it and release its own bookkeeping.

Types

type Client

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

Client represents a single WebSocket connection.

func NewClient

func NewClient(conn *websocket.Conn, sendBuffer int) *Client

type Hub

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

func NewHub

func NewHub(buffer int) *Hub

func (*Hub) Broadcast

func (h *Hub) Broadcast(msg []byte)

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

func (*Hub) SetOnActive

func (h *Hub) SetOnActive(fn func())

func (*Hub) SetOnEmpty

func (h *Hub) SetOnEmpty(fn func())

func (*Hub) SetOnFirstClient

func (h *Hub) SetOnFirstClient(fn func())

type LogMessage

type LogMessage struct {
	Seq         uint64 `json:"seq"`
	Level       string `json:"level,omitempty"`
	Message     string `json:"message"`
	Timestamp   string `json:"timestamp"` // RFC3339(9) string
	Service     string `json:"service,omitempty"`
	ContainerID string `json:"containerId,omitempty"`
}

type WebSocketMetrics added in v2.1.0

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

WebSocketMetrics tracks active WebSocket connections and their counts.

func NewWebSocketMetrics added in v2.1.0

func NewWebSocketMetrics() *WebSocketMetrics

NewWebSocketMetrics creates a new WebSocketMetrics instance.

func (*WebSocketMetrics) Connections added in v2.1.0

Connections returns a snapshot of all tracked WebSocket connections.

func (*WebSocketMetrics) RegisterConnection added in v2.1.0

func (m *WebSocketMetrics) RegisterConnection(info systemtypes.WebSocketConnectionInfo) string

RegisterConnection adds a connection to the tracker and increments the appropriate kind counter. Returns the assigned connection ID.

func (*WebSocketMetrics) Snapshot added in v2.1.0

Snapshot returns a point-in-time copy of the active connection counts.

func (*WebSocketMetrics) UnregisterConnection added in v2.1.0

func (m *WebSocketMetrics) UnregisterConnection(id string)

UnregisterConnection removes a connection from the tracker and decrements the appropriate kind counter.

Jump to

Keyboard shortcuts

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