OSDN Git Service

revert the api stuff
authorpaladz <colt@ColtdeMacBook-Pro.local>
Tue, 26 Jan 2021 08:54:41 +0000 (16:54 +0800)
committerpaladz <colt@ColtdeMacBook-Pro.local>
Tue, 26 Jan 2021 08:54:41 +0000 (16:54 +0800)
net/http/gzip/gzip.go

index d99a344..7d84116 100644 (file)
@@ -8,8 +8,22 @@ import (
        "net"
        "net/http"
        "strings"
+       "sync"
 )
 
+var pool = sync.Pool{
+       New: func() interface{} {
+               w, _ := gzip.NewWriterLevel(nil, gzip.BestSpeed) // #nosec
+               return w
+       },
+}
+
+func getWriter(w io.Writer) *gzip.Writer {
+       gz := pool.Get().(*gzip.Writer)
+       gz.Reset(w)
+       return gz
+}
+
 type Handler struct {
        Handler http.Handler
 }
@@ -21,10 +35,11 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
                return
        }
        w.Header().Set("Content-Encoding", "gzip")
-       gz, _ := gzip.NewWriterLevel(nil, gzip.BestSpeed)
+       gz := getWriter(w)
        w = &responseWriter{gz, w}
        h.Handler.ServeHTTP(w, r)
        gz.Close()
+       pool.Put(gz)
 }
 
 type responseWriter struct {