Import Anubis v1.28.2
Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3). Masyunis customizations are re-applied in the following commits.
This commit is contained in:
1 parent
eb3e46d8d0
commit
e07de898cd
573 files changed
+35846
-6807
No files matched your search
+44
-5
@@ -2,11 +2,48 @@ package internal
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// gzipWriterPools holds one *sync.Pool of *gzip.Writer per compression level.
|
||||
//
|
||||
// The pools are process-global on purpose. GzipMiddleware is constructed
|
||||
// per-request at its RenderIndex call site (the wrapped handler embeds the
|
||||
// request-specific challenge page), so a closure-local pool would be created
|
||||
// empty, used once, and garbage collected on every challenge render; never
|
||||
// actually reusing a writer. Keeping the pools here lets the ~1.18 MiB deflate
|
||||
// buffers inside each *gzip.Writer survive across requests regardless of how
|
||||
// often the middleware is rebuilt.
|
||||
var gzipWriterPools sync.Map // map[int]*sync.Pool
|
||||
|
||||
func gzipWriterPool(level int) *sync.Pool {
|
||||
if p, ok := gzipWriterPools.Load(level); ok {
|
||||
return p.(*sync.Pool)
|
||||
}
|
||||
p := &sync.Pool{
|
||||
New: func() any {
|
||||
gz, _ := gzip.NewWriterLevel(io.Discard, level)
|
||||
return gz
|
||||
},
|
||||
}
|
||||
actual, _ := gzipWriterPools.LoadOrStore(level, p)
|
||||
return actual.(*sync.Pool)
|
||||
}
|
||||
|
||||
func GzipMiddleware(level int, next http.Handler) http.Handler {
|
||||
// Validate the level with the same range check gzip.NewWriterLevel uses,
|
||||
// but without allocating a throwaway ~1.18 MiB writer on every (per-request)
|
||||
// middleware construction. gzip only rejects out-of-range levels.
|
||||
if level < gzip.HuffmanOnly || level > gzip.BestCompression {
|
||||
panic(fmt.Sprintf("gzip: invalid compression level: %d", level))
|
||||
}
|
||||
|
||||
pool := gzipWriterPool(level)
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -14,11 +51,13 @@ func GzipMiddleware(level int, next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz, err := gzip.NewWriterLevel(w, level)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer gz.Close()
|
||||
gz := pool.Get().(*gzip.Writer)
|
||||
gz.Reset(w)
|
||||
defer func() {
|
||||
_ = gz.Close()
|
||||
gz.Reset(io.Discard)
|
||||
pool.Put(gz)
|
||||
}()
|
||||
|
||||
grw := gzipResponseWriter{ResponseWriter: w, sink: gz}
|
||||
next.ServeHTTP(grw, r)
|
||||
|
||||
Reference in new issue
Block a user