This is an automated email from the ASF dual-hosted git repository.

dcelasun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 077b5fc  go: Use sync.Pool for gzip in HTTP transport
077b5fc is described below

commit 077b5fce825e79d84592fff893639b92b637eec7
Author: Duru Can Celasun <[email protected]>
AuthorDate: Wed Sep 30 07:25:51 2020 +0100

    go: Use sync.Pool for gzip in HTTP transport
    
    b67cad4 introduced transparent gzip support for the HTTP transport but
    calling gzip.NewWriter() with every request causes a large number of
    memory allocations [1] and can create GC pressure.
    
    Avoid this by using a sync.Pool for gzip writers.
    
    [1] 
https://old.reddit.com/r/golang/comments/9uejp4/usage_of_syncpool_for_gzipwriter_in_http_handlers/e94jh8c/
---
 lib/go/thrift/http_transport.go | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/lib/go/thrift/http_transport.go b/lib/go/thrift/http_transport.go
index 66f0f38..bc69227 100644
--- a/lib/go/thrift/http_transport.go
+++ b/lib/go/thrift/http_transport.go
@@ -24,6 +24,7 @@ import (
        "io"
        "net/http"
        "strings"
+       "sync"
 )
 
 // NewThriftHandlerFunc is a function that create a ready to use Apache Thrift 
Handler function
@@ -40,14 +41,24 @@ func NewThriftHandlerFunc(processor TProcessor,
 
 // gz transparently compresses the HTTP response if the client supports it.
 func gz(handler http.HandlerFunc) http.HandlerFunc {
+       sp := &sync.Pool{
+               New: func() interface{} {
+                       return gzip.NewWriter(nil)
+               },
+       }
+
        return func(w http.ResponseWriter, r *http.Request) {
                if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
                        handler(w, r)
                        return
                }
                w.Header().Set("Content-Encoding", "gzip")
-               gz := gzip.NewWriter(w)
-               defer gz.Close()
+               gz := sp.Get().(*gzip.Writer)
+               gz.Reset(w)
+               defer func() {
+                       _ = gz.Close()
+                       sp.Put(gz)
+               }()
                gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
                handler(gzw, r)
        }

Reply via email to