Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Bound idle gzip compression memory

pulse-triage[bot]a8b53ca20568
diff --git a/pkg/server/gzip_middleware.go b/pkg/server/gzip_middleware.go
index 3248c3af22..7b0614f69e 100644
--- a/pkg/server/gzip_middleware.go
+++ b/pkg/server/gzip_middleware.go
@@ -6,7 +6,6 @@ import (
"net/http"
"strconv"
"strings"
- "sync"
"github.com/gorilla/websocket"
)
@@ -16,16 +15,55 @@ import (
// no declared Content-Length (e.g. streamed JSON) are still compressed.
const gzipMinContentLength = 1024
-var gzipWriterPool = sync.Pool{
- New: func() any {
+// gzipWriterPoolCapacity bounds idle compression state retained after bursts.
+// A BestSpeed writer owns roughly a MiB of working memory. sync.Pool has no
+// cardinality limit, so a burst of concurrent API responses can leave hundreds
+// of MiB resident until enough GC/scavenger cycles happen to release it. Eight
+// writers cover ordinary parallel browser requests without making a past
+// concurrency spike the server's steady-state memory floor.
+const gzipWriterPoolCapacity = 8
+
+type boundedGzipWriterPool struct {
+ idle chan *gzip.Writer
+}
+
+func newBoundedGzipWriterPool(capacity int) *boundedGzipWriterPool {
+ if capacity < 0 {
+ capacity = 0
+ }
+ return &boundedGzipWriterPool{idle: make(chan *gzip.Writer, capacity)}
+}
+
+func (p *boundedGzipWriterPool) get() *gzip.Writer {
+ select {
+ case gw := <-p.idle:
+ return gw
+ default:
// BestSpeed keeps CPU cost low on multi-megabyte state payloads while
// still collapsing repetitive monitoring JSON by roughly an order of
- // magnitude.
- gw, _ := gzip.NewWriterLevel(io.Discard, gzip.BestSpeed)
+ // magnitude. The level is a constant accepted by compress/gzip.
+ gw, err := gzip.NewWriterLevel(io.Discard, gzip.BestSpeed)
+ if err != nil {
+ panic("server: unsupported gzip compression level")
+ }
return gw
- },
+ }
+}
+
+func (p *boundedGzipWriterPool) put(gw *gzip.Writer) {
+ if gw == nil {
+ return
+ }
+ gw.Reset(io.Discard)
+ select {
+ case p.idle <- gw:
+ default:
+ // Do not retain compression state beyond the explicit idle budget.
+ }
}
+var gzipWriterPool = newBoundedGzipWriterPool(gzipWriterPoolCapacity)
+
// gzipCompressibleContentTypes lists the response types worth compressing.
// Everything else (images, fonts, event streams, already-compressed archives)
// passes through untouched. text/event-stream is deliberately absent: SSE
@@ -259,7 +297,7 @@ func (w *gzipResponseWriter) ensureGzipWriter() {
if w.gz != nil {
return
}
- gz := gzipWriterPool.Get().(*gzip.Writer)
+ gz := gzipWriterPool.get()
gz.Reset(w.ResponseWriter)
w.gz = gz
}
@@ -274,7 +312,6 @@ func (w *gzipResponseWriter) close() {
return
}
_ = w.gz.Close()
- w.gz.Reset(io.Discard)
- gzipWriterPool.Put(w.gz)
+ gzipWriterPool.put(w.gz)
w.gz = nil
}
diff --git a/pkg/server/gzip_middleware_test.go b/pkg/server/gzip_middleware_test.go
index cf054bfd74..0ce5807574 100644
--- a/pkg/server/gzip_middleware_test.go
+++ b/pkg/server/gzip_middleware_test.go
@@ -58,6 +58,44 @@ func TestWithGzipCompressesJSONForAcceptingClients(t *testing.T) {
}
}
+func TestBoundedGzipWriterPoolLimitsIdleCompressionState(t *testing.T) {
+ const capacity = 2
+ pool := newBoundedGzipWriterPool(capacity)
+ writers := make([]*gzip.Writer, 0, capacity+3)
+ for range capacity + 3 {
+ writer := pool.get()
+ // Force allocation of the writer's BestSpeed working state. The pool's
+ // contract is about bounding these initialized writers, not just shells.
+ if _, err := writer.Write([]byte(strings.Repeat("state-payload", 1024))); err != nil {
+ t.Fatalf("prime gzip writer: %v", err)
+ }
+ writers = append(writers, writer)
+ }
+
+ for _, writer := range writers {
+ if err := writer.Close(); err != nil {
+ t.Fatalf("close gzip writer: %v", err)
+ }
+ pool.put(writer)
+ }
+
+ if got := len(pool.idle); got != capacity {
+ t.Fatalf("idle gzip writers = %d, want bounded capacity %d", got, capacity)
+ }
+}
+
+func TestBoundedGzipWriterPoolSupportsNoIdleRetention(t *testing.T) {
+ pool := newBoundedGzipWriterPool(0)
+ writer := pool.get()
+ if err := writer.Close(); err != nil {
+ t.Fatalf("close gzip writer: %v", err)
+ }
+ pool.put(writer)
+ if got := len(pool.idle); got != 0 {
+ t.Fatalf("idle gzip writers = %d, want 0", got)
+ }
+}
+
func TestWithGzipPassesThroughWithoutAcceptEncoding(t *testing.T) {
payload := strings.Repeat("plain body ", 500)
handler := withGzip(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {