agentws

package
v0.0.0-...-3f647e7 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 19 Imported by: ? 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 agentws is the server side of the persistent agent WebSocket channel that replaced the HTTP POST telemetry endpoint. Each agent holds one long-lived connection: it authenticates with its bearer token, sends a Hello, then streams telemetry Packets (acked in order) and HostSnapshots up, while the server pushes DesiredState and SnapshotRequests down the same pipe. Liveness is connection-driven: an open, ping-answering socket means the agent is online *now*, so config changes and snapshot requests reach it instantly instead of riding the next telemetry ack.

This file isolates the coder/websocket dependency of the session machinery. hub.serve and session work against wire.Conn; wsConn adapts a server-accepted WebSocket to that interface (owning the codec fixed by the negotiated subprotocol and translating close codes both ways). The in-process pipe used by DialLocal implements the same wire.Conn without touching this file.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("agentws: hub closed")

ErrClosed is returned by DialLocal after CloseAll: the hub is shutting down and accepts no further sessions.

Functions

This section is empty.

Types

type Deps

type Deps struct {
	Registry *registry.Service
	Ingest   *ingest.Service
	Config   *config.Service
	HostLive *hostlive.Store  // in-memory live snapshots (never persisted)
	OpIssue  *opissue.Service // operational-issue engine (monitor status + host re-eval)
	Bus      *eventbus.Bus    // source of TopicConfigChanged pushes
}

Deps are the services the hub drives on behalf of connected agents.

type Hub

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

Hub tracks the one live session per agent and fans server-initiated pushes (config changes, host snapshot requests) out to them.

Incident evidence is deliberately absent from that list. Traceroutes and incident scenes are both decided, executed and delivered by the agent inside telemetry packets, so nothing about either crosses this hub: the agent worth asking during a network fault is the one whose session just died, and a push to it is a no-op precisely when the answer matters.

func New

func New(d Deps) *Hub

New constructs the hub and subscribes it to config changes so edited targets reach connected agents without waiting for anything agent-initiated.

func (*Hub) CloseAll

func (h *Hub) CloseAll(reason string)

CloseAll closes every session with CloseGoingAway for graceful shutdown. http.Server.Shutdown does NOT wait for (or close) hijacked WebSocket connections, so without this they would stall the shutdown deadline. Each close performs the close handshake (blocking up to its internal timeout on a dead peer), so they run in parallel and CloseAll waits for every session to fully tear down — including any in-flight ingest — before returning, so the caller's later db.Close cannot race a live writer.

It also latches the hub closed in the same critical section that snapshots the sessions, so a connection racing the sweep is refused rather than surviving it (see serve and DialLocal). Connections still mid-handshake — admitted but not yet registered — are cut loose (unblocking a parked Hello read immediately instead of at helloTimeout) and then awaited via h.serving, so no Hello side effect can write through a DB the caller closes after CloseAll returns. The latch is permanent: a Hub is never reused.

func (*Hub) ConnectedIDs

func (h *Hub) ConnectedIDs() []string

ConnectedIDs returns the agent IDs with a live session, for the offline sweeper's exclusion list.

func (*Hub) DialLocal

func (h *Hub) DialLocal(ctx context.Context, token string) (wire.Conn, error)

DialLocal authenticates the token and, on success, attaches an in-process agent link: it creates a frame pipe, serves the server end on its own goroutine (same lifecycle as a WebSocket session — registered in the hub, kicked on supersede, closed by CloseAll), and returns the agent end. The desktop's bundled agent connects through this instead of a loopback WebSocket, so telemetry never leaves the process.

It returns ErrClosed once CloseAll has run: the server is shutting down and its DB is about to close, so failing fast lets the bundled agent's reconnect loop reach the replacement server instead of latching onto this one.

func (*Hub) Disconnect

func (h *Hub) Disconnect(agentID string, code wire.CloseCode, reason string)

Disconnect synchronously evicts an agent's live session (no-op when it has none), waiting for the session to fully tear down before returning. The agent-delete path calls this BEFORE purging: the session authenticated at upgrade time, so without an explicit evict it would keep ingesting under the captured identity and could recreate series the purge just removed. Waiting on s.closed guarantees any in-flight ingest has finished before the caller purges.

func (*Hub) HandleUpgrade

func (h *Hub) HandleUpgrade(w http.ResponseWriter, r *http.Request)

HandleUpgrade is the GET /agent/ws handler: it authenticates the bearer token, upgrades to WebSocket, checks the negotiated subprotocol, then hands the connection to serve (which owns the Hello handshake and session loop).

func (*Hub) IsConnected

func (h *Hub) IsConnected(agentID string) bool

IsConnected reports whether an agent currently holds a live session.

func (*Hub) PushDesiredStateSite

func (h *Hub) PushDesiredStateSite(ctx context.Context, siteID string)

PushDesiredStateSite rebuilds and pushes DesiredState to every connected agent of a site (per-agent, since group scoping resolves per agent).

func (*Hub) PushSnapshotRequest

func (h *Hub) PushSnapshotRequest(agentID string, req pcfg.SnapshotRequest) bool

PushSnapshotRequest sends a live-snapshot request to the agent's session, reporting false when the agent is not connected (the caller then tells the console the agent is offline instead of leaving a request dangling).

Jump to

Keyboard shortcuts

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