Skip to content

Commit

Permalink
Keep encoder pools reusable for compression
Browse files Browse the repository at this point in the history
  • Loading branch information
rnishtala-sumo committed Oct 3, 2024
1 parent 18d5c7b commit bfe8fb4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config/configcompression/compressiontype.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (ct *TypeWithLevel) UnmarshalText(in []byte) error {
parts := strings.Split(string(in), "/")
compressionTyp = Type(parts[0])
level = zlib.DefaultCompression
if len(parts) > 1 {
if len(parts) == 2 {
level, err = strconv.Atoi(parts[1])
if err != nil {
return fmt.Errorf("invalid compression level: %q", parts[1])
Expand Down
45 changes: 33 additions & 12 deletions config/confighttp/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,47 @@ type compressor struct {
pool sync.Pool
}

var (
gZipCompressor = &compressor{}
snappyCompressor = &compressor{}
zstdCompressor = &compressor{}
zlibCompressor = &compressor{}
_ writeCloserReset = (*gzip.Writer)(nil)
_ writeCloserReset = (*snappy.Writer)(nil)
_ writeCloserReset = (*zstd.Encoder)(nil)
_ writeCloserReset = (*zlib.Writer)(nil)
)

// writerFactory defines writer field in CompressRoundTripper.
// The validity of input is already checked when NewCompressRoundTripper was called in confighttp,
func newCompressor(compressionType configcompression.TypeWithLevel) (*compressor, error) {
// compressionType, compressionLevel := getCompression(compressionType)

switch compressionType.Type {
case configcompression.TypeGzip:
var _ writeCloserReset = (*gzip.Writer)(nil)
return &compressor{pool: sync.Pool{New: func() any { w, _ := gzip.NewWriterLevel(nil, int(compressionType.Level)); return w }}}, nil
if gZipCompressor.pool.Get() == nil {
gZipCompressor.pool = sync.Pool{New: func() any { w, _ := gzip.NewWriterLevel(nil, int(compressionType.Level)); return w }}
return gZipCompressor, nil
}
return gZipCompressor, nil
case configcompression.TypeSnappy:
var _ writeCloserReset = (*snappy.Writer)(nil)
return &compressor{pool: sync.Pool{New: func() any { return snappy.NewBufferedWriter(nil) }}}, nil
if snappyCompressor.pool.Get() == nil {
snappyCompressor.pool = sync.Pool{New: func() any { return snappy.NewBufferedWriter(nil) }}
return snappyCompressor, nil
}
return snappyCompressor, nil

Check warning on line 55 in config/confighttp/compressor.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/compressor.go#L55

Added line #L55 was not covered by tests
case configcompression.TypeZstd:
var _ writeCloserReset = (*zstd.Encoder)(nil)
compression := zstd.EncoderLevelFromZstd(int(compressionType.Level))
encoderLevel := zstd.WithEncoderLevel(compression)
return &compressor{pool: sync.Pool{New: func() any { zw, _ := zstd.NewWriter(nil, zstd.WithEncoderConcurrency(1), encoderLevel); return zw }}}, nil
if zstdCompressor.pool.Get() == nil {
compression := zstd.EncoderLevelFromZstd(int(compressionType.Level))
encoderLevel := zstd.WithEncoderLevel(compression)
zstdCompressor.pool = sync.Pool{New: func() any { zw, _ := zstd.NewWriter(nil, zstd.WithEncoderConcurrency(1), encoderLevel); return zw }}
return zstdCompressor, nil
}
return zstdCompressor, nil

Check warning on line 63 in config/confighttp/compressor.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/compressor.go#L63

Added line #L63 was not covered by tests
case configcompression.TypeZlib, configcompression.TypeDeflate:
var _ writeCloserReset = (*zlib.Writer)(nil)
return &compressor{pool: sync.Pool{New: func() any { w, _ := zlib.NewWriterLevel(nil, int(compressionType.Level)); return w }}}, nil
if zlibCompressor.pool.Get() == nil {
zlibCompressor.pool = sync.Pool{New: func() any { w, _ := zlib.NewWriterLevel(nil, int(compressionType.Level)); return w }}
return zlibCompressor, nil
}
return zlibCompressor, nil
}
return nil, errors.New("unsupported compression type")

Check warning on line 71 in config/confighttp/compressor.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/compressor.go#L71

Added line #L71 was not covered by tests
}
Expand Down

0 comments on commit bfe8fb4

Please sign in to comment.