diff --git a/go.mod b/go.mod index 314ecea..1331228 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/api/server.go b/internal/api/server.go index 9fc4f78..3163b38 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -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. diff --git a/internal/api/statuspages.go b/internal/api/statuspages.go index c55137d..093ca42 100644 --- a/internal/api/statuspages.go +++ b/internal/api/statuspages.go @@ -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 diff --git a/internal/store/incidents.sql.go b/internal/store/incidents.sql.go index ca071d0..1adc2af 100644 --- a/internal/store/incidents.sql.go +++ b/internal/store/incidents.sql.go @@ -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 } diff --git a/internal/store/queries/incidents.sql b/internal/store/queries/incidents.sql index d7aa8e8..a27c945 100644 --- a/internal/store/queries/incidents.sql +++ b/internal/store/queries/incidents.sql @@ -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;