[influxd] add utility functions to make it harder to misuse gzipWriterPool

pull/7948/head
Carlo Alberto Ferraris 2017-02-09 09:31:11 +09:00
parent a6a7782e04
commit 005e480b55
1 changed files with 14 additions and 8 deletions

View File

@ -8,7 +8,6 @@ import (
"expvar"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/pprof"
@ -1089,12 +1088,8 @@ func gzipFilter(inner http.Handler) http.Handler {
inner.ServeHTTP(w, r)
return
}
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
defer func() {
gz.Close()
gzipWriterPool.Put(gz)
}()
gz := getGzipWriter(w)
defer putGzipWriter(gz)
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
inner.ServeHTTP(gzw, r)
})
@ -1102,10 +1097,21 @@ func gzipFilter(inner http.Handler) http.Handler {
var gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(ioutil.Discard)
return gzip.NewWriter(nil)
},
}
func getGzipWriter(w io.Writer) *gzip.Writer {
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
return gz
}
func putGzipWriter(gz *gzip.Writer) {
gz.Close()
gzipWriterPool.Put(gz)
}
// 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 {