getZstdEncoderChannel returns the buffered channel that retains idle zstd encoders for the given params. The slow path holds a single global mutex and re-checks the sync.Map under the lock so that a stampede of goroutines arriving for the same not-yet-seen ZstdEncoderParams cannot create multiple co
(params ZstdEncoderParams)
| 31 | // the previous size-1 cap can no longer force concurrent callers to |
| 32 | // allocate a fresh encoder per batch. |
| 33 | func getZstdEncoderChannel(params ZstdEncoderParams) chan *zstd.Encoder { |
| 34 | if c, ok := zstdAvailableEncoders.Load(params); ok { |
| 35 | return c.(chan *zstd.Encoder) |
| 36 | } |
| 37 | |
| 38 | zstdEncoderInitMu.Lock() |
| 39 | defer zstdEncoderInitMu.Unlock() |
| 40 | |
| 41 | if c, ok := zstdAvailableEncoders.Load(params); ok { |
| 42 | return c.(chan *zstd.Encoder) |
| 43 | } |
| 44 | |
| 45 | ch := make(chan *zstd.Encoder, max(runtime.GOMAXPROCS(0), 1)) |
| 46 | zstdAvailableEncoders.Store(params, ch) |
| 47 | return ch |
| 48 | } |
| 49 | |
| 50 | func newZstdEncoder(params ZstdEncoderParams) (*zstd.Encoder, error) { |
| 51 | encoderLevel := zstd.SpeedDefault |
no outgoing calls
no test coverage detected