perf(statuspages): build page snapshots in time for large organizations
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 12m37s
ci / build (pull_request) Successful in 18m14s
ci / image (pull_request) Successful in 5m39s

The snapshot read the incidents of a page without the organization, which
row level security keeps from using an index, and every status page
server asking for a page built it on its own. Snapshots now filter by the
organization and servers asking at the same time share one build, which
does not end when one of them gives up.

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-26 17:04:00 +01:00
1 parent 1138d555aa
commit 27338f0557
5 files changed
+35 -6

No files matched your search

+1 -1
View File
@@ -34,6 +34,7 @@ require (
golang.org/x/crypto/x509roots/fallback v0.0.0-20261123165210-6c21c5864259
golang.org/x/net v0.63.0
golang.org/x/oauth2 v0.38.0
golang.org/x/sync v0.25.0
golang.org/x/text v0.44.0
google.golang.org/grpc v1.85.0
layeh.com/radius v0.0.0-20231213012653-1006025d24f8
@@ -116,7 +117,6 @@ require (
golang.org/x/arch v0.22.0 // indirect
golang.org/x/exp v0.0.0-20261109185158-a52e3aef5920 // indirect
golang.org/x/mod v0.43.0 // indirect
golang.org/x/sync v0.25.0 // indirect
golang.org/x/sys v0.50.0 // indirect
golang.org/x/tools v0.52.1-0.20261106145633-d407941a339a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20261110000943-09e21b237f2c // indirect
+4
View File
@@ -10,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/redis/go-redis/v9"
"golang.org/x/sync/singleflight"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
@@ -57,6 +58,9 @@ type Server struct {
support *versioned[supportSettings]
// draining, once closed, ends the long polls of checkers.
draining <-chan struct{}
// snapshots lets status page servers asking for the same page at once
// share one build of it.
snapshots singleflight.Group
}
// Deps are the external resources a Server needs.
+26 -2
View File
@@ -110,7 +110,7 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
return err
}
}
if incidents, err = q.IncidentsForMonitors(ctx, store.IncidentsForMonitorsParams{Ids: ids, Since: ptr(now.Add(-incidentLookback))}); err != nil {
if incidents, err = q.IncidentsForMonitors(ctx, store.IncidentsForMonitorsParams{OrgID: page.OrgID, Ids: ids, Since: ptr(now.Add(-incidentLookback))}); err != nil {
return err
}
incidentIDs := make([]uuid.UUID, len(incidents))
@@ -376,7 +376,7 @@ func (s *Server) agentPage(c *gin.Context) {
fail(c, http.StatusNotFound, "not_found")
return
}
snap, err := s.buildSnapshot(ctx, page)
snap, err := s.sharedSnapshot(ctx, page)
if err != nil {
internalError(c, err)
return
@@ -392,6 +392,30 @@ func (s *Server) agentPage(c *gin.Context) {
c.JSON(http.StatusOK, snap)
}
// snapshotTimeout bounds a shared snapshot build, which outlives the
// request that started it.
const snapshotTimeout = time.Minute
// sharedSnapshot builds page once for all status page servers asking for it
// at the same time, as they do when told that it changed. A server giving
// up does not cancel the build for the others.
func (s *Server) sharedSnapshot(ctx context.Context, page store.StatusPage) (agentapi.PageSnapshot, error) {
ch := s.snapshots.DoChan(page.ID.String(), func() (any, error) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), snapshotTimeout)
defer cancel()
return s.buildSnapshot(ctx, page)
})
select {
case <-ctx.Done():
return agentapi.PageSnapshot{}, ctx.Err()
case r := <-ch:
if r.Err != nil {
return agentapi.PageSnapshot{}, r.Err
}
return r.Val.(agentapi.PageSnapshot), nil
}
}
// agentServes finds the published page a status page agent serves at host
// or slug. The platform's servers leave the pages of organizations that
// serve them on their own servers to those, which receive only their own
+3 -2
View File
@@ -271,18 +271,19 @@ func (q *Queries) GetIncident(ctx context.Context, id uuid.UUID) (GetIncidentRow
const incidentsForMonitors = `-- name: IncidentsForMonitors :many
SELECT id, org_id, monitor_id, started_at, resolved_at, cause, title, status, status_id FROM incidents
WHERE monitor_id = ANY ($1::uuid[]) AND (resolved_at IS NULL OR resolved_at > $2)
WHERE org_id = $1 AND monitor_id = ANY ($2::uuid[]) AND (resolved_at IS NULL OR resolved_at > $3)
ORDER BY started_at DESC
LIMIT 50
`
type IncidentsForMonitorsParams struct {
OrgID uuid.UUID
Ids []uuid.UUID
Since *time.Time
}
func (q *Queries) IncidentsForMonitors(ctx context.Context, arg IncidentsForMonitorsParams) ([]Incident, error) {
rows, err := q.db.Query(ctx, incidentsForMonitors, arg.Ids, arg.Since)
rows, err := q.db.Query(ctx, incidentsForMonitors, arg.OrgID, arg.Ids, arg.Since)
if err != nil {
return nil, err
}
+1 -1
View File
@@ -88,7 +88,7 @@ SELECT * FROM incidents WHERE org_id = $1 AND resolved_at IS NULL;
-- name: IncidentsForMonitors :many
SELECT * FROM incidents
WHERE monitor_id = ANY (sqlc.arg(ids)::uuid[]) AND (resolved_at IS NULL OR resolved_at > sqlc.arg(since))
WHERE org_id = sqlc.arg(org_id) AND monitor_id = ANY (sqlc.arg(ids)::uuid[]) AND (resolved_at IS NULL OR resolved_at > sqlc.arg(since))
ORDER BY started_at DESC
LIMIT 50;