fix(statuspages): encode a page once for all servers, gzipped
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m55s
ci / test (pull_request) Successful in 13m30s
ci / build (pull_request) Successful in 19m20s
ci / image (pull_request) Successful in 5m37s

Every status page server asking for a page got it encoded anew: the
snapshot was marshalled once for its checksum and again for the answer,
then copied into a stream frame. A page of several thousand components
is about 20 MB of JSON, so servers fetching it together, as they do when
told it changed, ran both API pods out of their 512 MiB.

The shared build now also encodes the page once, as JSON and gzipped,
and every server asking at the same time gets those bytes. Servers ask
for gzip and unpack it, over HTTP and the stream; older ones still get
plain JSON. A 20 MB page shrinks to about 1 MB. Uptime ratios in the
snapshot are rounded to six decimals instead of up to 18 characters,
never to 100% for a component that failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-29 21:58:14 +01:00
1 parent 3bc927a5f7
commit 9424824416
6 files changed
+182 -22

No files matched your search

+74
View File
@@ -1,6 +1,7 @@
package api_test
import (
"compress/gzip"
"context"
"encoding/json"
"errors"
@@ -9,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"slices"
"strconv"
"strings"
"testing"
"time"
@@ -481,3 +483,75 @@ func TestStatusPageGroupsAndDetails(t *testing.T) {
}
}
}
// TestAgentPageEncoding fetches a page as a status page server would, with
// and without gzip, and through both the HTTP API and the client.
func TestAgentPageEncoding(t *testing.T) {
e := newEnv(t)
ctx := context.Background()
owner := e.browser(t)
owner.signup("o@"+uniq("enc")+".test", uniq("enc-"))
slug := uniq("enc-")
_, body := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Encoded"})
owner.do("PUT", "/api/v1/org/statuspages/"+body["id"].(string), map[string]any{"slug": slug, "title": "Encoded", "published": true})
tok, params := api.NewAgentToken(nil, "statuspage", "sp-"+uuid.NewString()[:6], nil)
if err := tenant.New(e.dbPool).Tx(ctx, tenant.System(), func(q *store.Queries) error {
_, err := q.CreateAgent(ctx, params)
return err
}); err != nil {
t.Fatal(err)
}
// No transparent decompression: the test sees what the API sends.
hc := &http.Client{Transport: &http.Transport{DisableCompression: true, TLSClientConfig: e.srv.Client().Transport.(*http.Transport).TLSClientConfig}}
get := func(header map[string]string) *http.Response {
t.Helper()
req, _ := http.NewRequest(http.MethodGet, e.srv.URL+"/agent/v1/pages?slug="+slug, nil)
req.Header.Set("Authorization", "Bearer "+tok)
req.Header.Set(agentapi.VersionHeader, strconv.Itoa(agentapi.Version))
req.Header.Set(agentapi.InstanceHeader, uuid.NewString())
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := hc.Do(req)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = resp.Body.Close() })
return resp
}
plain := get(nil)
raw, _ := io.ReadAll(plain.Body)
var snap agentapi.PageSnapshot
if plain.StatusCode != http.StatusOK || plain.Header.Get("Content-Encoding") != "" || json.Unmarshal(raw, &snap) != nil || snap.Title != "Encoded" {
t.Fatalf("plain page = %d %q %.200s", plain.StatusCode, plain.Header.Get("Content-Encoding"), raw)
}
etag := plain.Header.Get("ETag")
if etag != `"`+snap.Checksum()+`"` {
t.Errorf("ETag = %s, want the checksum %s", etag, snap.Checksum())
}
packed := get(map[string]string{"Accept-Encoding": "gzip"})
if packed.Header.Get("Content-Encoding") != "gzip" || packed.Header.Get("ETag") != etag {
t.Fatalf("gzip page encoding %q, ETag %q; want gzip and %s", packed.Header.Get("Content-Encoding"), packed.Header.Get("ETag"), etag)
}
zr, err := gzip.NewReader(packed.Body)
if err != nil {
t.Fatal(err)
}
var unpacked agentapi.PageSnapshot
if err := json.NewDecoder(zr).Decode(&unpacked); err != nil || unpacked.Checksum() != snap.Checksum() {
t.Errorf("the gzipped page differs from the plain one: %v", err)
}
if st := get(map[string]string{"If-None-Match": etag, "Accept-Encoding": "gzip"}).StatusCode; st != http.StatusNotModified {
t.Errorf("page with the current ETag = %d, want 304", st)
}
// The client asks for gzip and unpacks it, over plain HTTP too.
client := agentapi.NewClient(e.srv.URL, tok, uuid.New(), "test").WithHTTPClient(e.srv.Client())
if got, err := client.Page(ctx, "", slug); err != nil || got.Checksum() != snap.Checksum() {
t.Errorf("client Page() = %q, %v; want the same page", got.Title, err)
}
}