-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
62 lines (48 loc) · 1.15 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
package socketify
import "sync"
type storage struct {
m sync.Mutex
clients map[string]*Client
}
func newStorage() *storage {
return &storage{
clients: map[string]*Client{},
}
}
func (s *storage) addClient(c *Client) {
s.m.Lock()
defer s.m.Unlock()
s.clients[c.id] = c
}
func (s *storage) GetClientByID(clientID string) *Client {
s.m.Lock()
defer s.m.Unlock()
return s.clients[clientID]
}
func (s *storage) GetClientsByAttributeValue(key, value string) []*Client {
s.m.Lock()
defer s.m.Unlock()
var clients []*Client
for index, client := range s.clients {
if val, exists := client.GetAttribute(key); exists {
if val == value {
clients = append(clients, s.clients[index])
}
}
}
return clients
}
// SetClientForID Important: Don't use this method if you're not sure what you're doing
// This might replace the client with an existing alive client
func (s *storage) SetClientForID(id string, client *Client) {
s.m.Lock()
defer s.m.Unlock()
delete(s.clients, client.id)
client.id = id
s.clients[id] = client
}
func (s *storage) removeClientByID(clientID string) {
s.m.Lock()
defer s.m.Unlock()
delete(s.clients, clientID)
}