-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_store.go
48 lines (37 loc) · 896 Bytes
/
conn_store.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
package internal
import (
"net"
"sync"
)
type ConnectionStore struct {
conns map[string]*WsConnAdapter
mu sync.RWMutex
}
func NewConnectionStore() *ConnectionStore {
return &ConnectionStore{
conns: make(map[string]*WsConnAdapter),
}
}
// ConnCacheSet store connection in cache.
func (cs *ConnectionStore) ConnCacheSet(addr net.Addr, conn *WsConnAdapter) {
cs.mu.Lock()
defer cs.mu.Unlock()
cs.conns[addr.String()] = conn
}
// ConnCacheRemove remove connection from cache.
func (cs *ConnectionStore) ConnCacheRemove(addr net.Addr) bool {
cs.mu.Lock()
defer cs.mu.Unlock()
delete(cs.conns, addr.String())
return true
}
// ConnCacheGet get connection from cache.
func (cs *ConnectionStore) ConnCacheGet(addr net.Addr) (*WsConnAdapter, bool) {
cs.mu.Lock()
defer cs.mu.Unlock()
conn, ok := cs.conns[addr.String()]
if !ok {
return nil, false
}
return conn, true
}