Skip to content

Commit

Permalink
pass level param through
Browse files Browse the repository at this point in the history
  • Loading branch information
lizthegrey committed Oct 1, 2021
1 parent 712b6bb commit c9e4131
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func compress(cc CompressionCodec, level int, data []byte) ([]byte, error) {
}
return buf.Bytes(), nil
case CompressionZSTD:
return zstdCompress(nil, data)
return zstdCompress(level, nil, data)
default:
return nil, PacketEncodingError{fmt.Sprintf("unsupported compression codec (%d)", cc)}
}
Expand Down
25 changes: 22 additions & 3 deletions zstd.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package sarama

import (
"sync"

"github.com/klauspost/compress/zstd"
)

var zstdEncMap sync.Map

var (
zstdDec, _ = zstd.NewReader(nil)
zstdEnc, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true))
)

func getEncoder(level int) *zstd.Encoder {
if ret, ok := zstdEncMap.Load(level); ok {
return ret.(*zstd.Encoder)
}
// It's possible to race and create multiple new writers.
// Only one will survive GC after use.
encoderLevel := zstd.SpeedDefault
if level != CompressionLevelDefault {
encoderLevel = zstd.EncoderLevelFromZstd(level)
}
zstdEnc, _ := zstd.NewWriter(nil, zstd.WithZeroFrames(true),
zstd.WithEncoderLevel(encoderLevel))
zstdEncMap.Store(level, zstdEnc)
return zstdEnc
}

func zstdDecompress(dst, src []byte) ([]byte, error) {
return zstdDec.DecodeAll(src, dst)
}

func zstdCompress(dst, src []byte) ([]byte, error) {
return zstdEnc.EncodeAll(src, dst), nil
func zstdCompress(level int, dst, src []byte) ([]byte, error) {
return getEncoder(level).EncodeAll(src, dst), nil
}

0 comments on commit c9e4131

Please sign in to comment.