feat(statuspage): refresh pages by checksum
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m46s
ci / test (pull_request) Successful in 14m46s
ci / build (pull_request) Successful in 18m49s
ci / image (pull_request) Successful in 3m24s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m46s
ci / test (pull_request) Successful in 14m46s
ci / build (pull_request) Successful in 18m49s
ci / image (pull_request) Successful in 3m24s
Each refresh sent the whole page again even when nothing had changed. Status page servers now send the checksum of their copy (If-None-Match) and the API answers 304 when the page, leaving out when it was generated, is unchanged; the server then only marks its copy current. The checksum is kept on disk with the page, so it survives restarts. 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
4e9a198b2f
commit
fcf615dce7
6 files changed
+120
-8
No files matched your search
@@ -49,7 +49,9 @@ const (
|
||||
|
||||
// Source fetches snapshots; *agentapi.Client implements it.
|
||||
type Source interface {
|
||||
Page(ctx context.Context, host, slug string) (agentapi.PageSnapshot, error)
|
||||
// PageIfChanged answers agentapi.ErrNotModified when the page still
|
||||
// has checksum.
|
||||
PageIfChanged(ctx context.Context, host, slug, checksum string) (agentapi.PageSnapshot, error)
|
||||
Pages(ctx context.Context) ([]agentapi.PageRef, error)
|
||||
}
|
||||
|
||||
@@ -81,6 +83,9 @@ type entry struct {
|
||||
Slug string `json:"slug,omitempty"`
|
||||
Snap agentapi.PageSnapshot `json:"snapshot"`
|
||||
Fetched time.Time `json:"fetched"`
|
||||
// Checksum is the snapshot's, sent so the API can answer that nothing
|
||||
// changed instead of sending it again.
|
||||
Checksum string `json:"checksum,omitempty"`
|
||||
}
|
||||
|
||||
// Options configure a Server.
|
||||
@@ -330,8 +335,20 @@ func (s *Server) lookup(ctx context.Context, key, host, slug string) (entry, boo
|
||||
}
|
||||
|
||||
func (s *Server) fetch(ctx context.Context, key, host, slug string) (entry, bool) {
|
||||
snap, err := s.src.Page(ctx, host, slug)
|
||||
s.mu.Lock()
|
||||
old, known := s.cache[key]
|
||||
s.mu.Unlock()
|
||||
snap, err := s.src.PageIfChanged(ctx, host, slug, old.Checksum)
|
||||
switch {
|
||||
case errors.Is(err, agentapi.ErrNotModified) && known:
|
||||
// Unchanged: the copy is current as of now.
|
||||
old.Fetched = s.now()
|
||||
old.Snap.GeneratedAt = old.Fetched
|
||||
s.mu.Lock()
|
||||
s.cache[key] = old
|
||||
s.mu.Unlock()
|
||||
s.persist(old)
|
||||
return old, true
|
||||
case errors.Is(err, agentapi.ErrNotFound):
|
||||
s.forget(key)
|
||||
return entry{}, false
|
||||
@@ -342,7 +359,7 @@ func (s *Server) fetch(ctx context.Context, key, host, slug string) (entry, bool
|
||||
s.mu.Unlock()
|
||||
return e, ok
|
||||
}
|
||||
e := entry{Key: key, Host: host, Slug: slug, Snap: snap, Fetched: s.now()}
|
||||
e := entry{Key: key, Host: host, Slug: slug, Snap: snap, Fetched: s.now(), Checksum: snap.Checksum()}
|
||||
s.mu.Lock()
|
||||
s.cache[key] = e
|
||||
s.mu.Unlock()
|
||||
|
||||
@@ -20,12 +20,27 @@ import (
|
||||
type fakeSource struct {
|
||||
mu sync.Mutex
|
||||
calls int
|
||||
err error
|
||||
delay time.Duration
|
||||
pages map[string]agentapi.PageSnapshot
|
||||
// unchanged counts the answers that the page had not changed.
|
||||
unchanged int
|
||||
err error
|
||||
delay time.Duration
|
||||
pages map[string]agentapi.PageSnapshot
|
||||
}
|
||||
|
||||
func (f *fakeSource) Page(ctx context.Context, host, slug string) (agentapi.PageSnapshot, error) {
|
||||
// PageIfChanged counts calls and answers like the API, sending pages
|
||||
// only when their checksum changed.
|
||||
func (f *fakeSource) PageIfChanged(ctx context.Context, host, slug, checksum string) (agentapi.PageSnapshot, error) {
|
||||
p, err := f.page(ctx, host, slug)
|
||||
if err == nil && checksum != "" && p.Checksum() == checksum {
|
||||
f.mu.Lock()
|
||||
f.unchanged++
|
||||
f.mu.Unlock()
|
||||
return agentapi.PageSnapshot{}, agentapi.ErrNotModified
|
||||
}
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (f *fakeSource) page(ctx context.Context, host, slug string) (agentapi.PageSnapshot, error) {
|
||||
f.mu.Lock()
|
||||
f.calls++
|
||||
err, delay, p, ok := f.err, f.delay, f.pages[host+"|"+slug], false
|
||||
@@ -508,3 +523,35 @@ func TestSyncCachesPagesBeforeTheirFirstVisit(t *testing.T) {
|
||||
t.Errorf("GET after the page left the list = %d, want 404", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshSkipsUnchangedPages(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
now := time.Now()
|
||||
src := newSource()
|
||||
h := newHarness(t, src, dir, &now)
|
||||
h.srv.Sync(context.Background())
|
||||
now = now.Add(syncInterval)
|
||||
h.srv.Sync(context.Background())
|
||||
src.mu.Lock()
|
||||
unchanged := src.unchanged
|
||||
src.mu.Unlock()
|
||||
if unchanged != 2 {
|
||||
t.Errorf("unchanged answers on the second sync = %d, want 2", unchanged)
|
||||
}
|
||||
if _, body := h.get("status.example.test", "/acme"); !strings.Contains(body, "Acme status") || strings.Contains(body, "Live-Aktualisierung") {
|
||||
t.Error("an unchanged page is not served as current")
|
||||
}
|
||||
|
||||
// A changed page is sent again, and the checksum survives a restart.
|
||||
src.set(func(f *fakeSource) {
|
||||
f.pages["|acme"] = agentapi.PageSnapshot{Title: "Renamed", Status: agentapi.PageOperational}
|
||||
})
|
||||
restarted := newHarness(t, src, dir, &now)
|
||||
restarted.srv.Sync(context.Background())
|
||||
src.mu.Lock()
|
||||
unchanged = src.unchanged
|
||||
src.mu.Unlock()
|
||||
if _, body := restarted.get("status.example.test", "/acme"); !strings.Contains(body, "Renamed") || unchanged != 3 {
|
||||
t.Errorf("after a change: renamed %t, unchanged answers %d, want 3", strings.Contains(body, "Renamed"), unchanged)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user