From 4cae725edf88a4a399fc758e6e286ceaaa247625 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 20 Jul 2023 20:55:27 -0700 Subject: [PATCH] lib/encoding/zstd: switch back from atomic.Pointer to atomic.Value for map[...]... The map[...]... is already a pointer type, so atomic.Pointer[map[...]...] results in double pointer. This is a follow-up for 140e7b6b74475f87c279bcdbf6d69748137edff4 --- lib/encoding/zstd/zstd_pure.go | 14 +++++++------- lib/tenantmetrics/counter_map.go | 4 +++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/encoding/zstd/zstd_pure.go b/lib/encoding/zstd/zstd_pure.go index 5336337e3..a6aa98056 100644 --- a/lib/encoding/zstd/zstd_pure.go +++ b/lib/encoding/zstd/zstd_pure.go @@ -15,13 +15,13 @@ var ( decoder *zstd.Decoder mu sync.Mutex - av atomic.Pointer[registry] + + // do not use atomic.Pointer, since the stored map there is already a pointer type. + av atomic.Value ) -type registry map[int]*zstd.Encoder - func init() { - r := make(registry) + r := make(map[int]*zstd.Encoder) av.Store(r) var err error @@ -45,7 +45,7 @@ func CompressLevel(dst, src []byte, compressionLevel int) []byte { } func getEncoder(compressionLevel int) *zstd.Encoder { - r := av.Load() + r := av.Load().(map[int]*zstd.Encoder) e := r[compressionLevel] if e != nil { return e @@ -54,10 +54,10 @@ 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. - r1 := av.Load() + r1 := av.Load().(map[int]*zstd.Encoder) if e = r1[compressionLevel]; e == nil { e = newEncoder(compressionLevel) - r2 := make(registry) + r2 := make(map[int]*zstd.Encoder) for k, v := range r1 { r2[k] = v } diff --git a/lib/tenantmetrics/counter_map.go b/lib/tenantmetrics/counter_map.go index 3ab106679..cc47c423c 100644 --- a/lib/tenantmetrics/counter_map.go +++ b/lib/tenantmetrics/counter_map.go @@ -18,7 +18,9 @@ type TenantID struct { // CounterMap is a map of counters keyed by tenant. type CounterMap struct { metric string - m atomic.Value + + // do not use atomic.Pointer, since the stored map there is already a pointer type. + m atomic.Value } // NewCounterMap creates new CounterMap for the given metric.