From 2007f9d1aba11bda1b34f7135865248979344bbe Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 4 Aug 2023 08:17:30 +0200 Subject: [PATCH] fix(platform): use built-in sync.Map relates to #4116 --- src/platform/concurrent_map.go | 50 +++++++++++++--------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/platform/concurrent_map.go b/src/platform/concurrent_map.go index 45c446eb..5b4a780c 100644 --- a/src/platform/concurrent_map.go +++ b/src/platform/concurrent_map.go @@ -2,47 +2,35 @@ package platform import "sync" -type ConcurrentMap struct { - values map[string]interface{} - sync.RWMutex -} - func NewConcurrentMap() *ConcurrentMap { - return &ConcurrentMap{ - values: make(map[string]interface{}), - } + var cm ConcurrentMap + return &cm } -func (c *ConcurrentMap) Set(key string, value interface{}) { - c.Lock() - defer c.Unlock() - c.values[key] = value +type ConcurrentMap sync.Map + +func (cm *ConcurrentMap) Set(key string, value any) { + (*sync.Map)(cm).Store(key, value) } -func (c *ConcurrentMap) Get(key string) (interface{}, bool) { - c.RLock() - defer c.RUnlock() - if val, ok := c.values[key]; ok { - return val, true - } - return "", false +func (cm *ConcurrentMap) Get(key string) (any, bool) { + return (*sync.Map)(cm).Load(key) } -func (c *ConcurrentMap) Delete(key string) { - c.RLock() - defer c.RUnlock() - delete(c.values, key) +func (cm *ConcurrentMap) Delete(key string) { + (*sync.Map)(cm).Delete(key) } -func (c *ConcurrentMap) List() map[string]interface{} { - return c.values +func (cm *ConcurrentMap) Contains(key string) bool { + _, ok := (*sync.Map)(cm).Load(key) + return ok } -func (c *ConcurrentMap) Contains(key string) bool { - c.RLock() - defer c.RUnlock() - if _, ok := c.values[key]; ok { +func (cm *ConcurrentMap) List() map[string]any { + list := make(map[string]any) + (*sync.Map)(cm).Range(func(key, value any) bool { + list[key.(string)] = value return true - } - return false + }) + return list }