Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "github.com/klauspost/compress/zstd" |
| 5 | "sync" |
| 6 | ) |
| 7 | |
| 8 | var ( |
| 9 | zstdDec *zstd.Decoder |
| 10 | zstdEnc *zstd.Encoder |
| 11 | |
| 12 | zstdEncOnce, zstdDecOnce sync.Once |
| 13 | ) |
| 14 | |
| 15 | func zstdDecompress(dst, src []byte) ([]byte, error) { |
| 16 | zstdDecOnce.Do(func() { |
| 17 | zstdDec, _ = zstd.NewReader(nil) |
| 18 | }) |
| 19 | return zstdDec.DecodeAll(src, dst) |
| 20 | } |
| 21 | |
| 22 | func zstdCompress(dst, src []byte) ([]byte, error) { |
| 23 | zstdEncOnce.Do(func() { |
| 24 | zstdEnc, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true)) |
| 25 | }) |
| 26 | return zstdEnc.EncodeAll(src, dst), nil |
| 27 | } |