-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
64 lines (48 loc) · 1.12 KB
/
storage.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
package socketify
import "sync"
// TODO: Some methods should not be exported when we are allowing direct external access to clients
type storage struct {
m sync.Mutex
clients map[string]*Connection
}
func newStorage() *storage {
return &storage{
clients: map[string]*Connection{},
}
}
func (s *storage) GetClientByID(clientID string) *Connection {
s.m.Lock()
defer s.m.Unlock()
return s.clients[clientID]
}
func (s *storage) GetClientsByAttributeValue(key, value string) []*Connection {
s.m.Lock()
defer s.m.Unlock()
var clients []*Connection
for index, client := range s.clients {
if val, exists := client.GetAttribute(key); exists {
if val == value {
clients = append(clients, s.clients[index])
}
}
}
return clients
}
func (s *storage) ClientIDs() (ids []string) {
s.m.Lock()
defer s.m.Unlock()
for _, client := range s.clients {
ids = append(ids, client.id)
}
return ids
}
func (s *storage) addClient(c *Connection) {
s.m.Lock()
defer s.m.Unlock()
s.clients[c.id] = c
}
func (s *storage) removeClientByID(clientID string) {
s.m.Lock()
defer s.m.Unlock()
delete(s.clients, clientID)
}