[influxd] Use a sync.Pool to reuse gzip.Writer across requests

This brings alloc_space down from ~20200M to ~10700M in a run of
go test ./cmd/influxd/run -bench=Server -memprofile=mem.out -run='^$'
pull/7948/head
Carlo Alberto Ferraris 2017-02-05 08:47:50 +09:00
parent 98e19f257c
commit a6a7782e04
2 changed files with 15 additions and 2 deletions

View File

@ -3,6 +3,7 @@
### Features
- [#7776](https://github.com/influxdata/influxdb/issues/7776): Add system information to /debug/vars.
- [#7948](https://github.com/influxdata/influxdb/pull/7948): Reduce memory allocations by reusing gzip.Writers across requests
## v1.2.1 [unreleased]

View File

@ -8,6 +8,7 @@ import (
"expvar"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/pprof"
@ -15,6 +16,7 @@ import (
"runtime/debug"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
@ -1087,13 +1089,23 @@ func gzipFilter(inner http.Handler) http.Handler {
inner.ServeHTTP(w, r)
return
}
gz := gzip.NewWriter(w)
defer gz.Close()
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
defer func() {
gz.Close()
gzipWriterPool.Put(gz)
}()
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
inner.ServeHTTP(gzw, r)
})
}
var gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(ioutil.Discard)
},
}
// cors responds to incoming requests and adds the appropriate cors headers
// TODO: corylanou: add the ability to configure this in our config
func cors(inner http.Handler) http.Handler {