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 ¶
- Variables
- type Deps
- type Hub
- func (h *Hub) CloseAll(reason string)
- func (h *Hub) ConnectedIDs() []string
- func (h *Hub) DialLocal(ctx context.Context, token string) (wire.Conn, error)
- func (h *Hub) Disconnect(agentID string, code wire.CloseCode, reason string)
- func (h *Hub) HandleUpgrade(w http.ResponseWriter, r *http.Request)
- func (h *Hub) IsConnected(agentID string) bool
- func (h *Hub) PushDesiredStateSite(ctx context.Context, siteID string)
- func (h *Hub) PushSnapshotRequest(agentID string, req pcfg.SnapshotRequest) bool
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
ConnectedIDs returns the agent IDs with a live session, for the offline sweeper's exclusion list.
func (*Hub) DialLocal ¶
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 ¶
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 ¶
IsConnected reports whether an agent currently holds a live session.
func (*Hub) PushDesiredStateSite ¶
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).