feat(agents): talk to the API over a WebSocket and push page changes #161

Merged
Sebi merged 1 commits from feat/agent-websocket into main 2026-11-26 16:48:37 +00:00
Owner

Checkers and status page servers now talk to the API over one WebSocket each. The API pushes status page changes to the servers.

Stacked on #160 (base perf/console-many-monitors). Merge #160 first.

How it works

  • /agent/v1/stream: an agent opens one WebSocket. Each message is a JSON header line plus the body. Requests carry id, method and path, replies carry id and status, and a cancel frame withdraws a request (e.g. a long poll).
  • Same protocol: the API runs every request through its normal agent routes, as if it had come over HTTP. Agent auth runs again per request, a revoked token closes the stream, and the access log shows the requests as before. The agent's identity (token, instance, release, forwarded-for) always comes from the stream, never from the request.
  • Client side: agentapi.Client.UseStream swaps in an http.RoundTripper that tunnels requests over the stream, so Jobs, Report, PageIfChanged, storage and certificates are unchanged.
  • Fallback: while the stream is down, requests go over plain HTTP, and a request that never reached the API is resent over HTTP. An API without the stream (404) is asked again every 5 minutes, so new agents work with old APIs and the other way round.
  • Keepalive and limits: pings every 20 s (agent) and 30 s (API), permessage-deflate, 128 requests in flight per stream.
  • Draining: an instance shutting down refuses new requests on its streams, lets running ones finish for up to 5 s and closes. The agent reconnects to another instance.

Push for status pages

  • When: pagesChanged(org) is published on the Redis channel uptimesebi:pages-changed when
    • a region of a monitor changes between up and down, or reports for the first time (detected in insertResults),
    • an incident opens or resolves,
    • any change is made through the organization API (middleware on /api/v1/org); admin changes count for all organizations.
  • Delivery: every API instance subscribes per status page stream, finds the affected pages with ServablePages (now with org_id) and sends {"event":"pages"} with their slugs and hosts. Changes are collected for 1 s, and a stream gets at most one event per 5 s, so a flapping monitor does not trigger rebuild after rebuild.
  • Status page server: fetches the named pages at once, and once more if a page changes during that fetch. Polling every 30 s and the 5-minute sync stay as the fallback. The fetch timeout is now 30 s instead of 15 s.

Tests

  • TestAgentStream covers:
    • snapshot, 304 and 404 over the stream,
    • an edit of the page pushes an event for it,
    • a revoked token gives ErrUnauthorized and closes the stream.
  • startChecker in the API tests now uses the stream, so every end-to-end checker test (jobs, results, release, drain) runs over the WebSocket.
  • TestStreamFallsBackToHTTP, TestFrameRoundTrip and TestEventsRefreshPagesAtOnce (statuspage: refetch at once, and once more when the page changes mid-fetch).
  • go test -p 1 ./... green, golangci-lint v2.15.0: 0 issues.
  • Dev stack: both checkers and the status page server log connected to the API over WebSocket, and results keep arriving in latest_results.

Deploy

  • Traefik passes WebSocket upgrades by default. Other proxies in front of the API must allow them. Without that, agents simply stay on HTTP.
  • Agents and API can be updated in any order.

🤖 Generated with Claude Code

https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3

Checkers and status page servers now talk to the API over one WebSocket each. The API pushes status page changes to the servers. **Stacked on #160** (base `perf/console-many-monitors`). Merge #160 first. ## How it works - **`/agent/v1/stream`:** an agent opens one WebSocket. Each message is a JSON header line plus the body. Requests carry `id`, `method` and `path`, replies carry `id` and `status`, and a `cancel` frame withdraws a request (e.g. a long poll). - **Same protocol:** the API runs every request through its normal agent routes, as if it had come over HTTP. Agent auth runs again per request, a revoked token closes the stream, and the access log shows the requests as before. The agent's identity (token, instance, release, forwarded-for) always comes from the stream, never from the request. - **Client side:** `agentapi.Client.UseStream` swaps in an `http.RoundTripper` that tunnels requests over the stream, so `Jobs`, `Report`, `PageIfChanged`, storage and certificates are unchanged. - **Fallback:** while the stream is down, requests go over plain HTTP, and a request that never reached the API is resent over HTTP. An API without the stream (404) is asked again every 5 minutes, so new agents work with old APIs and the other way round. - **Keepalive and limits:** pings every 20 s (agent) and 30 s (API), permessage-deflate, 128 requests in flight per stream. - **Draining:** an instance shutting down refuses new requests on its streams, lets running ones finish for up to 5 s and closes. The agent reconnects to another instance. ## Push for status pages - **When:** `pagesChanged(org)` is published on the Redis channel `uptimesebi:pages-changed` when - a region of a monitor changes between up and down, or reports for the first time (detected in `insertResults`), - an incident opens or resolves, - any change is made through the organization API (middleware on `/api/v1/org`); admin changes count for all organizations. - **Delivery:** every API instance subscribes per status page stream, finds the affected pages with `ServablePages` (now with `org_id`) and sends `{"event":"pages"}` with their slugs and hosts. Changes are collected for 1 s, and a stream gets at most one event per 5 s, so a flapping monitor does not trigger rebuild after rebuild. - **Status page server:** fetches the named pages at once, and once more if a page changes during that fetch. Polling every 30 s and the 5-minute sync stay as the fallback. The fetch timeout is now 30 s instead of 15 s. ## Tests - `TestAgentStream` covers: - snapshot, `304` and `404` over the stream, - an edit of the page pushes an event for it, - a revoked token gives `ErrUnauthorized` and closes the stream. - `startChecker` in the API tests now uses the stream, so every end-to-end checker test (jobs, results, release, drain) runs over the WebSocket. - `TestStreamFallsBackToHTTP`, `TestFrameRoundTrip` and `TestEventsRefreshPagesAtOnce` (statuspage: refetch at once, and once more when the page changes mid-fetch). - `go test -p 1 ./...` green, golangci-lint v2.15.0: 0 issues. - **Dev stack:** both checkers and the status page server log `connected to the API over WebSocket`, and results keep arriving in `latest_results`. ## Deploy - Traefik passes WebSocket upgrades by default. Other proxies in front of the API must allow them. Without that, agents simply stay on HTTP. - Agents and API can be updated in any order. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Sebi changed target branch from perf/console-many-monitors to main 2026-11-26 16:47:39 +00:00
Sebi added 1 commit 2026-11-26 16:47:40 +00:00
feat(agents): talk to the API over a WebSocket and push page changes
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
538c43b563
Checkers and status page servers keep one WebSocket open to the API at
/agent/v1/stream and send their requests over it. The API answers them
through the agent API as if they had come over HTTP, each authenticated
again, so the protocol stays the same and a revoked token ends the
stream. While the stream is down agents fall back to plain HTTP, and an
API without it is asked again every five minutes.

Over the stream the API tells status page servers which of their pages
changed, so they fetch them at once instead of on their next refresh:
when a region of a monitor changes between up and down, an incident
opens or resolves, or anything of the organization is edited. Changes
are announced through Redis to every API instance and sent at most every
five seconds per stream. A page that changes while it is fetched is
fetched once more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Sebi merged commit df39b765cb into main 2026-11-26 16:48:37 +00:00
Sebi deleted branch feat/agent-websocket 2026-11-26 16:48:40 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ZB-Server/uptimesebi#161