/
safemap.go
66 lines (57 loc) · 1.19 KB
/
safemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package worc
import (
"sync"
"google.golang.org/grpc"
)
type safeMap struct {
lock *sync.RWMutex
sm map[string]*grpc.ClientConn
}
// NewSafeMap return a new thread safe map
func newSafeMap() *safeMap {
return &safeMap{
lock: new(sync.RWMutex),
sm: make(map[string]*grpc.ClientConn),
}
}
// Get from maps return the k's value
func (m *safeMap) Get(k string) *grpc.ClientConn {
m.lock.RLock()
defer m.lock.RUnlock()
if val, ok := m.sm[k]; ok {
return val
}
return nil
}
// Set Maps the given key and value. Returns false if the key is already in the map and changes nothing.
func (m *safeMap) Set(k string, v *grpc.ClientConn) bool {
m.lock.Lock()
defer m.lock.Unlock()
if val, ok := m.sm[k]; !ok {
m.sm[k] = v
} else if val != v {
m.sm[k] = v
} else {
return false
}
return true
}
// Check returns true if k is exist in the map.
func (m *safeMap) Check(k string) bool {
m.lock.RLock()
defer m.lock.RUnlock()
if _, ok := m.sm[k]; ok {
return true
}
return false
}
func (m *safeMap) Delete(k string) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.sm, k)
}
func (m *safeMap) List() map[string]*grpc.ClientConn {
m.lock.RLock()
defer m.lock.RUnlock()
return m.sm
}