Skip to content

Commit

Permalink
Merge pull request #3 from remerge/CbBmFu37/69-race-condition-in-reme…
Browse files Browse the repository at this point in the history
…rge-gzip-causing-bidder-to-crash

Return gzip writer back to pool when everything is flushed only
  • Loading branch information
thekondor authored Oct 25, 2022
2 parents 9c6d50f + c4398b4 commit 120dc92
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
30 changes: 14 additions & 16 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@ import (
"net/http"
"path/filepath"
"strings"
"sync"

"github.com/gin-gonic/gin"
"github.com/klauspost/compress/gzip"
)

type gzipHandler struct {
*Options
gzPool sync.Pool
gzipLevel int
}

func newGzipHandler(level int, options ...Option) *gzipHandler {
handler := &gzipHandler{
Options: DefaultOptions,
gzPool: sync.Pool{
New: func() interface{} {
gz, err := gzip.NewWriterLevel(ioutil.Discard, level)
if err != nil {
panic(err)
}
return gz
},
},
Options: DefaultOptions,
gzipLevel: level,
}
for _, setter := range options {
setter(handler.Options)
Expand All @@ -45,14 +36,13 @@ func (g *gzipHandler) Handle(c *gin.Context) {
return
}

gz := g.gzPool.Get().(*gzip.Writer)
defer g.gzPool.Put(gz)
defer gz.Reset(ioutil.Discard)
gz := g.mustNewGzipWriter()
gz.Reset(c.Writer)

c.Header("Content-Encoding", "gzip")
c.Header("Vary", "Accept-Encoding")
c.Writer = &gzipWriter{c.Writer, gz}
c.Writer = &gzipWriter{ResponseWriter: c.Writer, writer: gz}

defer func() {
gz.Close()
c.Header("Content-Length", fmt.Sprint(c.Writer.Size()))
Expand Down Expand Up @@ -82,3 +72,11 @@ func (g *gzipHandler) isPathExcluded(path string) bool {
g.ExcludedPaths.Contains(path) ||
g.ExcludedPathesRegexs.Contains(path)
}

func (g *gzipHandler) mustNewGzipWriter() *gzip.Writer {
gz, err := gzip.NewWriterLevel(ioutil.Discard, g.gzipLevel)
if err != nil {
panic(err)
}
return gz
}

0 comments on commit 120dc92

Please sign in to comment.