feat(statuspage): fetch every served page every five minutes
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Canceled after 5m23s
ci / test (pull_request) Canceled after 5m23s
ci / build (pull_request) Canceled after 0s
ci / image (pull_request) Canceled after 0s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Canceled after 5m23s
ci / test (pull_request) Canceled after 5m23s
ci / build (pull_request) Canceled after 0s
ci / image (pull_request) Canceled after 0s
A status page server only knew the pages visitors had asked it for, so during an API outage a server that had never seen a page could not serve it. The agent API now lists the pages a server serves (GET /agent/v1/pages/index), following the same rule as single pages, and status page servers fetch all of them on start and every five minutes, also dropping those no longer listed. Pages visited within the last ten minutes are still refreshed every 30 seconds; the outdated notice now appears after ten minutes, twice the sync interval. 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
c0239e15bb
commit
4e9a198b2f
10 files changed
+217
-15
No files matched your search
@@ -35,14 +35,22 @@ import (
|
||||
const (
|
||||
// freshFor is how long a snapshot is used without asking the API.
|
||||
freshFor = 30 * time.Second
|
||||
// staleNoticeAfter is when visitors are told the state may be outdated.
|
||||
staleNoticeAfter = 5 * time.Minute
|
||||
refreshInterval = 30 * time.Second
|
||||
// refreshInterval is how often pages visited within recentFor are
|
||||
// refreshed.
|
||||
refreshInterval = 30 * time.Second
|
||||
recentFor = 10 * time.Minute
|
||||
// syncInterval is how often every page the API lists is fetched, so
|
||||
// each one is cached before its first visit.
|
||||
syncInterval = 5 * time.Minute
|
||||
// staleNoticeAfter is when visitors are told the state may be outdated;
|
||||
// a page nobody visited is up to syncInterval old.
|
||||
staleNoticeAfter = 2 * syncInterval
|
||||
)
|
||||
|
||||
// Source fetches snapshots; *agentapi.Client implements it.
|
||||
type Source interface {
|
||||
Page(ctx context.Context, host, slug string) (agentapi.PageSnapshot, error)
|
||||
Pages(ctx context.Context) ([]agentapi.PageRef, error)
|
||||
}
|
||||
|
||||
// Server renders pages for one status page agent.
|
||||
@@ -60,6 +68,8 @@ type Server struct {
|
||||
mu sync.Mutex
|
||||
cache map[string]entry
|
||||
refreshing map[string]bool
|
||||
// visited holds when each page was last visited.
|
||||
visited map[string]time.Time
|
||||
// background tracks refreshes started by visits.
|
||||
background sync.WaitGroup
|
||||
}
|
||||
@@ -88,7 +98,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{},
|
||||
now: time.Now, cache: map[string]entry{}, refreshing: map[string]bool{}, visited: map[string]time.Time{},
|
||||
}
|
||||
for _, h := range o.Hosts {
|
||||
s.ownHosts[strings.ToLower(h)] = true
|
||||
@@ -308,6 +318,7 @@ func (s *Server) localizeRequest(c *gin.Context) {
|
||||
func (s *Server) lookup(ctx context.Context, key, host, slug string) (entry, bool) {
|
||||
s.mu.Lock()
|
||||
e, cached := s.cache[key]
|
||||
s.visited[key] = s.now()
|
||||
s.mu.Unlock()
|
||||
if cached {
|
||||
if s.now().Sub(e.Fetched) >= freshFor {
|
||||
@@ -361,25 +372,64 @@ func (s *Server) refresh(ctx context.Context, key, host, slug string) {
|
||||
// Wait returns once the background refreshes started by visits are done.
|
||||
func (s *Server) Wait() { s.background.Wait() }
|
||||
|
||||
// Run keeps every known page fresh until ctx ends, so pages stay current
|
||||
// even without visitors and the cache is warm when the API goes away.
|
||||
// Run keeps pages fresh until ctx ends: every page the API lists every
|
||||
// syncInterval, so all are cached before their first visit and survive an
|
||||
// outage of the API, and recently visited ones every refreshInterval.
|
||||
func (s *Server) Run(ctx context.Context) {
|
||||
tick := time.NewTicker(refreshInterval)
|
||||
defer tick.Stop()
|
||||
refresh := time.NewTicker(refreshInterval)
|
||||
defer refresh.Stop()
|
||||
listAll := time.NewTicker(syncInterval)
|
||||
defer listAll.Stop()
|
||||
s.Sync(ctx)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-tick.C:
|
||||
case <-listAll.C:
|
||||
s.Sync(ctx)
|
||||
case <-refresh.C:
|
||||
s.mu.Lock()
|
||||
entries := make([]entry, 0, len(s.cache))
|
||||
for _, e := range s.cache {
|
||||
entries = append(entries, e)
|
||||
var recent []entry
|
||||
for key, e := range s.cache {
|
||||
if s.now().Sub(s.visited[key]) < recentFor {
|
||||
recent = append(recent, e)
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
for _, e := range entries {
|
||||
for _, e := range recent {
|
||||
s.refresh(ctx, e.Key, e.Host, e.Slug)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sync fetches every page the API lists and every cached one, which drops
|
||||
// those the API no longer serves. While the API is unreachable the cache
|
||||
// stays as it is.
|
||||
func (s *Server) Sync(ctx context.Context) {
|
||||
ctx, cancel := context.WithTimeout(ctx, syncInterval)
|
||||
defer cancel()
|
||||
refs, err := s.src.Pages(ctx)
|
||||
if err != nil {
|
||||
s.log.WarnContext(ctx, "listing status pages failed", "error", err)
|
||||
return
|
||||
}
|
||||
todo := map[string]entry{}
|
||||
s.mu.Lock()
|
||||
for key, e := range s.cache {
|
||||
todo[key] = e
|
||||
}
|
||||
s.mu.Unlock()
|
||||
for _, r := range refs {
|
||||
todo["slug:"+r.Slug] = entry{Key: "slug:" + r.Slug, Slug: r.Slug}
|
||||
if r.Host != "" {
|
||||
todo["host:"+r.Host] = entry{Key: "host:" + r.Host, Host: r.Host}
|
||||
}
|
||||
}
|
||||
for _, e := range todo {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
s.refresh(ctx, e.Key, e.Host, e.Slug)
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,21 @@ func (f *fakeSource) Page(ctx context.Context, host, slug string) (agentapi.Page
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Pages lists the fake's pages by their keys: "host|" or "|slug".
|
||||
func (f *fakeSource) Pages(context.Context) ([]agentapi.PageRef, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
}
|
||||
var out []agentapi.PageRef
|
||||
for k := range f.pages {
|
||||
host, slug, _ := strings.Cut(k, "|")
|
||||
out = append(out, agentapi.PageRef{Host: host, Slug: slug})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f *fakeSource) set(fn func(*fakeSource)) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
@@ -464,3 +479,32 @@ func TestNestedGroupsAndComponentDetails(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSyncCachesPagesBeforeTheirFirstVisit(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
now := time.Now()
|
||||
src := newSource()
|
||||
h := newHarness(t, src, dir, &now)
|
||||
h.srv.Sync(context.Background())
|
||||
|
||||
// Nobody visited them, yet both survive an outage and a restart.
|
||||
now = now.Add(syncInterval)
|
||||
restarted := newHarness(t, &fakeSource{err: errors.New("down")}, dir, &now)
|
||||
if code, body := restarted.get("status.example.test", "/acme"); code != 200 || !strings.Contains(body, "Acme status") || strings.Contains(body, "Live-Aktualisierung") {
|
||||
t.Errorf("GET of a synced page with the API down = %d, want it without stale notice", code)
|
||||
}
|
||||
if code, body := restarted.get("status.acme.test", "/"); code != 200 || !strings.Contains(body, "Acme on own domain") {
|
||||
t.Errorf("GET of a synced custom domain with the API down = %d", code)
|
||||
}
|
||||
restarted.srv.Sync(context.Background())
|
||||
if code, _ := restarted.get("status.example.test", "/acme"); code != 200 {
|
||||
t.Errorf("a failed sync dropped the cache: %d", code)
|
||||
}
|
||||
|
||||
// A page the API no longer lists is dropped by the next sync.
|
||||
src.set(func(f *fakeSource) { delete(f.pages, "|acme") })
|
||||
h.srv.Sync(context.Background())
|
||||
if code, _ := h.get("status.example.test", "/acme"); code != 404 {
|
||||
t.Errorf("GET after the page left the list = %d, want 404", code)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user