mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/encoding/zstd: prevent from possible encoder leak when concurrent goroutines create encoders for the same compressionLevel
Thanks to @klauspost for the pointer to this issue. See https://github.com/klauspost/compress/issues/195 for details.
This commit is contained in:
parent
bf4413e58d
commit
6a1499efa3
1 changed files with 8 additions and 6 deletions
|
@ -53,14 +53,16 @@ func getEncoder(compressionLevel int) *zstd.Encoder {
|
|||
mu.Lock()
|
||||
// Create the encoder under lock in order to prevent from wasted work
|
||||
// when concurrent goroutines create encoder for the same compressionLevel.
|
||||
e = newEncoder(compressionLevel)
|
||||
r1 := av.Load().(registry)
|
||||
if e = r1[compressionLevel]; e == nil {
|
||||
e = newEncoder(compressionLevel)
|
||||
r2 := make(registry)
|
||||
for k, v := range r1 {
|
||||
r2[k] = v
|
||||
}
|
||||
r2[compressionLevel] = e
|
||||
av.Store(r2)
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
return e
|
||||
|
|
Loading…
Reference in a new issue