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
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
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
This commit is contained in:
1 parent
27338f0557
commit
538c43b563
21 files changed
+1091
-45
No files matched your search
@@ -49,6 +49,8 @@ const (
|
||||
// without asking the API again.
|
||||
missFor = time.Minute
|
||||
maxMissing = 10_000
|
||||
// refreshTimeout bounds fetching one page.
|
||||
refreshTimeout = 30 * time.Second
|
||||
// unknownPerSecond bounds the lookups of hosts and slugs that are
|
||||
// neither cached nor known to be missing, so a flood of made-up names
|
||||
// is not passed on to the API. Every page the API lists is cached by
|
||||
@@ -79,6 +81,9 @@ type Server struct {
|
||||
mu sync.Mutex
|
||||
cache map[string]entry
|
||||
refreshing map[string]bool
|
||||
// again holds pages that changed while being refreshed, which are
|
||||
// fetched once more.
|
||||
again map[string]bool
|
||||
// visited holds when each cached page was last visited.
|
||||
visited map[string]time.Time
|
||||
// missing holds when a key was last found to have no page.
|
||||
@@ -118,7 +123,7 @@ type Options struct {
|
||||
func New(src Source, catalog *i18n.Catalog, log *slog.Logger, o Options) (*Server, error) {
|
||||
s := &Server{
|
||||
src: src, catalog: catalog, log: log, ownHosts: map[string]bool{}, dir: o.CacheDir,
|
||||
now: time.Now, cache: map[string]entry{}, refreshing: map[string]bool{}, visited: map[string]time.Time{},
|
||||
now: time.Now, cache: map[string]entry{}, refreshing: map[string]bool{}, again: map[string]bool{}, visited: map[string]time.Time{},
|
||||
missing: map[string]time.Time{},
|
||||
}
|
||||
for _, h := range o.Hosts {
|
||||
@@ -419,7 +424,8 @@ func (s *Server) fetch(ctx context.Context, key, host, slug string) (entry, bool
|
||||
return e, true
|
||||
}
|
||||
|
||||
// refresh fetches key unless another refresh of it is running.
|
||||
// refresh fetches key unless another refresh of it is running, and once
|
||||
// more if it changed meanwhile.
|
||||
func (s *Server) refresh(ctx context.Context, key, host, slug string) {
|
||||
s.mu.Lock()
|
||||
if s.refreshing[key] {
|
||||
@@ -428,14 +434,50 @@ func (s *Server) refresh(ctx context.Context, key, host, slug string) {
|
||||
}
|
||||
s.refreshing[key] = true
|
||||
s.mu.Unlock()
|
||||
defer func() {
|
||||
for {
|
||||
fctx, cancel := context.WithTimeout(ctx, refreshTimeout)
|
||||
s.fetch(fctx, key, host, slug)
|
||||
cancel()
|
||||
s.mu.Lock()
|
||||
delete(s.refreshing, key)
|
||||
again := s.again[key] && ctx.Err() == nil
|
||||
delete(s.again, key)
|
||||
if !again {
|
||||
delete(s.refreshing, key)
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
s.fetch(ctx, key, host, slug)
|
||||
}
|
||||
}
|
||||
|
||||
// OnEvent handles the events the API sends over its stream: pages that
|
||||
// changed are fetched at once instead of on their next refresh.
|
||||
func (s *Server) OnEvent(event string, body []byte) {
|
||||
if event != agentapi.EventPages {
|
||||
return
|
||||
}
|
||||
var ev agentapi.PagesEvent
|
||||
if err := json.Unmarshal(body, &ev); err != nil {
|
||||
s.log.Warn("ignoring unreadable event", "event", event, "error", err)
|
||||
return
|
||||
}
|
||||
for _, r := range ev.Pages {
|
||||
s.changed("slug:"+r.Slug, "", r.Slug)
|
||||
if r.Host != "" {
|
||||
s.changed("host:"+r.Host, r.Host, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) changed(key, host, slug string) {
|
||||
s.mu.Lock()
|
||||
if s.refreshing[key] {
|
||||
s.again[key] = true
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
s.mu.Unlock()
|
||||
s.background.Go(func() { s.refresh(context.Background(), key, host, slug) })
|
||||
}
|
||||
|
||||
// Wait returns once the background refreshes started by visits are done.
|
||||
|
||||
@@ -555,3 +555,35 @@ func TestRefreshSkipsUnchangedPages(t *testing.T) {
|
||||
t.Errorf("after a change: renamed %t, unchanged answers %d, want 3", strings.Contains(body, "Renamed"), unchanged)
|
||||
}
|
||||
}
|
||||
|
||||
// Pages the API says changed are fetched at once, and once more when they
|
||||
// change during that fetch.
|
||||
func TestEventsRefreshPagesAtOnce(t *testing.T) {
|
||||
now := time.Now()
|
||||
src := newSource()
|
||||
h := newHarness(t, src, t.TempDir(), &now)
|
||||
if code, body := h.get("status.example.test", "/acme"); code != 200 || !strings.Contains(body, "Acme status") {
|
||||
t.Fatalf("first GET = %d", code)
|
||||
}
|
||||
src.set(func(f *fakeSource) {
|
||||
f.pages["|acme"] = agentapi.PageSnapshot{Title: "Acme renamed", Status: agentapi.PageOperational}
|
||||
f.delay = 200 * time.Millisecond
|
||||
f.calls = 0
|
||||
})
|
||||
event := []byte(`{"pages":[{"slug":"acme"}]}`)
|
||||
h.srv.OnEvent(agentapi.EventPages, event)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
// Arrives while the first fetch runs.
|
||||
h.srv.OnEvent(agentapi.EventPages, event)
|
||||
h.srv.OnEvent("unknown", nil)
|
||||
h.srv.Wait()
|
||||
if _, body := h.get("status.example.test", "/acme"); !strings.Contains(body, "Acme renamed") {
|
||||
t.Error("page not refreshed after the event")
|
||||
}
|
||||
src.mu.Lock()
|
||||
calls := src.calls
|
||||
src.mu.Unlock()
|
||||
if calls != 2 {
|
||||
t.Errorf("fetches = %d, want 2: one for the event, one for the change during it", calls)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user