mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-23 20:37:12 +01: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
@ -53,14 +53,16 @@ func getEncoder(compressionLevel int) *zstd.Encoder {
|
|||||||
mu.Lock()
|
mu.Lock()
|
||||||
// Create the encoder under lock in order to prevent from wasted work
|
// Create the encoder under lock in order to prevent from wasted work
|
||||||
// when concurrent goroutines create encoder for the same compressionLevel.
|
// when concurrent goroutines create encoder for the same compressionLevel.
|
||||||
e = newEncoder(compressionLevel)
|
|
||||||
r1 := av.Load().(registry)
|
r1 := av.Load().(registry)
|
||||||
r2 := make(registry)
|
if e = r1[compressionLevel]; e == nil {
|
||||||
for k, v := range r1 {
|
e = newEncoder(compressionLevel)
|
||||||
r2[k] = v
|
r2 := make(registry)
|
||||||
|
for k, v := range r1 {
|
||||||
|
r2[k] = v
|
||||||
|
}
|
||||||
|
r2[compressionLevel] = e
|
||||||
|
av.Store(r2)
|
||||||
}
|
}
|
||||||
r2[compressionLevel] = e
|
|
||||||
av.Store(r2)
|
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
return e
|
return e
|
||||||
|
Loading…
Reference in New Issue
Block a user