Skip to content

Commit

Permalink
constructor for sync.Pool (#1303)
Browse files Browse the repository at this point in the history
Since sync.Pools are garbage collected, the current implementation where the pools are warmed up at `init` will be purged after few cycles.
This implementation changes to not pre-create pools but rather use the constructor to create them on demand.

Signed-off-by: Samuel Lang <gh@lang-sam.de>
  • Loading branch information
universam1 committed Feb 13, 2020
1 parent c586ad4 commit 40bcdc2
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions filters/builtin/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"io"
"math"
"net/http"
"runtime"
"sort"
"strconv"
"strings"
"sync"

log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
)

Expand Down Expand Up @@ -54,30 +54,21 @@ var defaultCompressMIME = []string{
}

var (
gzipPool = &sync.Pool{}
deflatePool = &sync.Pool{}
)

func init() {
// #cpu * 4: pool size decided based on some
// simple tests, checking performance by binary
// steps
for i := 0; i < runtime.NumCPU()*4; i++ {
gzipPool = &sync.Pool{New: func() interface{} {
ge, err := newEncoder("gzip", flate.BestSpeed)
if err != nil {
panic(err)
log.Error(err)
}

gzipPool.Put(ge)

return ge
}}
deflatePool = &sync.Pool{New: func() interface{} {
fe, err := newEncoder("deflate", flate.BestSpeed)
if err != nil {
panic(err)
log.Error(err)
}

deflatePool.Put(fe)
}
}
return fe
}}
)

func (e encodings) Len() int { return len(e) }
func (e encodings) Less(i, j int) bool { return e[i].q > e[j].q } // higher first
Expand Down

0 comments on commit 40bcdc2

Please sign in to comment.