Skip to content

Commit 2007f9d

Browse files
fix(platform): use built-in sync.Map
relates to JanDeDobbeleer#4116
1 parent 8554fb6 commit 2007f9d

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/platform/concurrent_map.go

+19-31
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,35 @@ package platform
22

33
import "sync"
44

5-
type ConcurrentMap struct {
6-
values map[string]interface{}
7-
sync.RWMutex
8-
}
9-
105
func NewConcurrentMap() *ConcurrentMap {
11-
return &ConcurrentMap{
12-
values: make(map[string]interface{}),
13-
}
6+
var cm ConcurrentMap
7+
return &cm
148
}
159

16-
func (c *ConcurrentMap) Set(key string, value interface{}) {
17-
c.Lock()
18-
defer c.Unlock()
19-
c.values[key] = value
10+
type ConcurrentMap sync.Map
11+
12+
func (cm *ConcurrentMap) Set(key string, value any) {
13+
(*sync.Map)(cm).Store(key, value)
2014
}
2115

22-
func (c *ConcurrentMap) Get(key string) (interface{}, bool) {
23-
c.RLock()
24-
defer c.RUnlock()
25-
if val, ok := c.values[key]; ok {
26-
return val, true
27-
}
28-
return "", false
16+
func (cm *ConcurrentMap) Get(key string) (any, bool) {
17+
return (*sync.Map)(cm).Load(key)
2918
}
3019

31-
func (c *ConcurrentMap) Delete(key string) {
32-
c.RLock()
33-
defer c.RUnlock()
34-
delete(c.values, key)
20+
func (cm *ConcurrentMap) Delete(key string) {
21+
(*sync.Map)(cm).Delete(key)
3522
}
3623

37-
func (c *ConcurrentMap) List() map[string]interface{} {
38-
return c.values
24+
func (cm *ConcurrentMap) Contains(key string) bool {
25+
_, ok := (*sync.Map)(cm).Load(key)
26+
return ok
3927
}
4028

41-
func (c *ConcurrentMap) Contains(key string) bool {
42-
c.RLock()
43-
defer c.RUnlock()
44-
if _, ok := c.values[key]; ok {
29+
func (cm *ConcurrentMap) List() map[string]any {
30+
list := make(map[string]any)
31+
(*sync.Map)(cm).Range(func(key, value any) bool {
32+
list[key.(string)] = value
4533
return true
46-
}
47-
return false
34+
})
35+
return list
4836
}

0 commit comments

Comments
 (0)