perf(statuspages): page, search and compress public status pages
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Failing after 13m31s
ci / test (pull_request) Successful in 13m30s
ci / build (pull_request) Skipped
ci / image (pull_request) Skipped

A status page with 3128 components was 30 MB of uncompressed HTML: all
components at once, each with 90 bars carrying a tooltip.

- The status page server gzips text responses.
- Status pages list 50 components at a time (?page=N) and offer a search
  (?q=) over component and group names once they have more than one
  page. Group headings keep the status and count of the whole group.
  Single-file exports still list every component.
- Rows linking a detail page draw their bars as one path per status,
  without tooltips; the detail page keeps them.

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-27 00:10:27 +01:00
1 parent 52d2ce25c9
commit b051f6c430
12 files changed
+4059 -3094

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
package platform
import (
"compress/gzip"
"io"
"mime"
"net/http"
"strconv"
"strings"
"sync"
"github.com/gin-gonic/gin"
)
// compressMin is the smallest body worth compressing, for responses that
// state their length.
const compressMin = 1024
var gzipWriters = sync.Pool{New: func() any {
w, _ := gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
return w
}}
// Compress gzips text responses (HTML, CSS, JavaScript, JSON, XML, SVG) for
// clients that accept it. Status pages list thousands of components in
// markup that repeats itself, so this shrinks them many times over.
func Compress() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Add("Vary", "Accept-Encoding")
if c.Request.Method == http.MethodHead || !acceptsGzip(c.Request.Header.Get("Accept-Encoding")) {
c.Next()
return
}
w := &gzipWriter{ResponseWriter: c.Writer}
c.Writer = w
defer w.finish()
c.Next()
}
}
// acceptsGzip reports whether an Accept-Encoding header allows gzip.
func acceptsGzip(header string) bool {
for _, part := range strings.Split(header, ",") {
name, params, _ := strings.Cut(part, ";")
name = strings.ToLower(strings.TrimSpace(name))
if name != "gzip" && name != "*" {
continue
}
for _, p := range strings.Split(params, ";") {
k, v, _ := strings.Cut(strings.TrimSpace(p), "=")
if strings.EqualFold(k, "q") {
if q, err := strconv.ParseFloat(strings.TrimSpace(v), 64); err == nil && q == 0 {
return false
}
}
}
return true
}
return false
}
func compressible(contentType string) bool {
mt, _, err := mime.ParseMediaType(contentType)
if err != nil {
return false
}
switch {
case strings.HasPrefix(mt, "text/"):
return true
case mt == "application/json", mt == "application/javascript", mt == "image/svg+xml",
mt == "application/xml", mt == "application/rss+xml", mt == "application/atom+xml":
return true
}
return strings.HasSuffix(mt, "+json") || strings.HasSuffix(mt, "+xml")
}
// gzipWriter decides at the first write whether to compress, when the
// handler has set the status and headers.
type gzipWriter struct {
gin.ResponseWriter
decided bool
gz *gzip.Writer
}
func (w *gzipWriter) decide() {
w.decided = true
h := w.Header()
switch status := w.Status(); {
case status < 200, status == http.StatusNoContent, status == http.StatusNotModified, status == http.StatusPartialContent:
return
}
if h.Get("Content-Encoding") != "" || !compressible(h.Get("Content-Type")) {
return
}
if n, err := strconv.Atoi(h.Get("Content-Length")); err == nil && n < compressMin {
return
}
h.Set("Content-Encoding", "gzip")
h.Del("Content-Length")
if etag := h.Get("ETag"); etag != "" && !strings.HasPrefix(etag, "W/") {
h.Set("ETag", "W/"+etag)
}
w.gz = gzipWriters.Get().(*gzip.Writer)
w.gz.Reset(w.ResponseWriter)
}
func (w *gzipWriter) Write(b []byte) (int, error) {
if !w.decided {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(b))
}
w.decide()
}
if w.gz == nil {
return w.ResponseWriter.Write(b)
}
return w.gz.Write(b)
}
func (w *gzipWriter) WriteString(s string) (int, error) {
return w.Write([]byte(s))
}
func (w *gzipWriter) Flush() {
if w.gz != nil {
_ = w.gz.Flush()
}
w.ResponseWriter.Flush()
}
func (w *gzipWriter) finish() {
if w.gz == nil {
return
}
_ = w.gz.Close()
w.gz.Reset(io.Discard)
gzipWriters.Put(w.gz)
w.gz = nil
}
+86
View File
@@ -0,0 +1,86 @@
package platform
import (
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func TestCompress(t *testing.T) {
gin.SetMode(gin.TestMode)
page := strings.Repeat("<li>component</li>", 2000)
r := gin.New()
r.Use(Compress())
r.GET("/page", func(c *gin.Context) {
c.Header("Content-Type", "text/html; charset=utf-8")
c.Status(http.StatusOK)
for i := 0; i < 2000; i++ {
_, _ = c.Writer.WriteString("<li>component</li>")
}
})
r.GET("/small", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
r.GET("/sized", func(c *gin.Context) {
c.Header("Content-Length", strconv.Itoa(len("tiny")))
c.Data(http.StatusOK, "text/plain", []byte("tiny"))
})
r.GET("/png", func(c *gin.Context) { c.Data(http.StatusOK, "image/png", []byte(page)) })
r.GET("/json", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"page": page}) })
r.GET("/notmodified", func(c *gin.Context) { c.Status(http.StatusNotModified) })
get := func(path, accept string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodGet, path, nil)
if accept != "" {
req.Header.Set("Accept-Encoding", accept)
}
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
return rec
}
gunzip := func(t *testing.T, rec *httptest.ResponseRecorder) string {
t.Helper()
zr, err := gzip.NewReader(rec.Body)
if err != nil {
t.Fatalf("gzip.NewReader() error = %v", err)
}
b, err := io.ReadAll(zr)
if err != nil {
t.Fatalf("reading gzip body error = %v", err)
}
return string(b)
}
rec := get("/page", "br, gzip;q=0.8")
if rec.Header().Get("Content-Encoding") != "gzip" || rec.Header().Get("Vary") != "Accept-Encoding" {
t.Fatalf("headers = %v, want gzip with Vary", rec.Header())
}
if rec.Body.Len() >= len(page)/10 {
t.Errorf("compressed size = %d, want far below %d", rec.Body.Len(), len(page))
}
if got := gunzip(t, rec); got != page {
t.Errorf("decompressed body differs: %d bytes, want %d", len(got), len(page))
}
if rec := get("/json", "gzip"); rec.Header().Get("Content-Encoding") != "gzip" || !strings.Contains(gunzip(t, rec), "component") {
t.Errorf("JSON not compressed: %v", rec.Header())
}
for _, tc := range []struct{ path, accept string }{
{"/page", ""},
{"/page", "gzip;q=0, deflate"},
{"/page", "identity"},
{"/sized", "gzip"},
{"/png", "gzip"},
{"/notmodified", "gzip"},
} {
if rec := get(tc.path, tc.accept); rec.Header().Get("Content-Encoding") != "" {
t.Errorf("GET %s with %q compressed, want plain", tc.path, tc.accept)
}
}
if rec := get("/page", "*"); rec.Header().Get("Content-Encoding") != "gzip" {
t.Errorf("Accept-Encoding * not compressed")
}
}