reuse gzip writers with a sync pool

This commit is contained in:
Azareal 2020-04-13 14:54:47 +10:00
parent 952a58176b
commit 584a50b461
2 changed files with 28 additions and 4 deletions

View File

@ -957,6 +957,8 @@ func isLocalHost(h string) bool {
return h=="localhost" || h=="127.0.0.1" || h=="::1" return h=="localhost" || h=="127.0.0.1" || h=="::1"
} }
var gzipPool = sync.Pool{}
// TODO: Pass the default path or config struct to the router rather than accessing it via a package global // TODO: Pass the default path or config struct to the router rather than accessing it via a package global
// TODO: SetDefaultPath // TODO: SetDefaultPath
// TODO: GetDefaultPath // TODO: GetDefaultPath
@ -1271,12 +1273,22 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
h := w.Header() h := w.Header()
h.Set("Content-Encoding", "gzip") h.Set("Content-Encoding", "gzip")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w} var ii = gzipPool.Get()
var igzw *gzip.Writer
if ii == nil {
igzw = gzip.NewWriter(w)
} else {
igzw = ii.(*gzip.Writer)
igzw.Reset(w)
}
gzw := c.GzipResponseWriter{Writer: igzw, ResponseWriter: w}
defer func() { defer func() {
//h := w.Header() //h := w.Header()
if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" { if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" {
//log.Print("push gzip close") //log.Print("push gzip close")
gzw.Writer.(*gzip.Writer).Close() igzw := gzw.Writer.(*gzip.Writer)
igzw.Close()
gzipPool.Put(igzw)
} }
}() }()
w = gzw w = gzw

View File

@ -628,6 +628,8 @@ func isLocalHost(h string) bool {
return h=="localhost" || h=="127.0.0.1" || h=="::1" return h=="localhost" || h=="127.0.0.1" || h=="::1"
} }
var gzipPool = sync.Pool{}
// TODO: Pass the default path or config struct to the router rather than accessing it via a package global // TODO: Pass the default path or config struct to the router rather than accessing it via a package global
// TODO: SetDefaultPath // TODO: SetDefaultPath
// TODO: GetDefaultPath // TODO: GetDefaultPath
@ -942,12 +944,22 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
h := w.Header() h := w.Header()
h.Set("Content-Encoding", "gzip") h.Set("Content-Encoding", "gzip")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w} var ii = gzipPool.Get()
var igzw *gzip.Writer
if ii == nil {
igzw = gzip.NewWriter(w)
} else {
igzw = ii.(*gzip.Writer)
igzw.Reset(w)
}
gzw := c.GzipResponseWriter{Writer: igzw, ResponseWriter: w}
defer func() { defer func() {
//h := w.Header() //h := w.Header()
if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" { if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" {
//log.Print("push gzip close") //log.Print("push gzip close")
gzw.Writer.(*gzip.Writer).Close() igzw := gzw.Writer.(*gzip.Writer)
igzw.Close()
gzipPool.Put(igzw)
} }
}() }()
w = gzw w = gzw