This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
clients.go
73 lines (63 loc) · 1.7 KB
/
clients.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
67
68
69
70
71
72
73
package websockets
import "sync"
// Clients stores all synchronized websocket clients in a thread-safe map
var Clients = ClientMap{
// Start with 100 to avoid reallocations on server start
clients: make(map[*Client]SyncID, 100),
}
// ClientMap is a thread-safe store for all clients connected to this server
// instance
type ClientMap struct {
// Map of clients to the threads or boards they are synced to
clients map[*Client]SyncID
sync.RWMutex
}
// SyncID contains the board and thread the client are currently synced to. If
// the client is on the board page, thread = 0.
type SyncID struct {
OP uint64
Board string
}
// Add adds a client to the map
func (c *ClientMap) add(cl *Client, syncID SyncID) {
c.Lock()
defer c.Unlock()
c.clients[cl] = syncID
cl.synced = true
}
// ChangeSync changes the thread or board ID the client is synchronized to
func (c *ClientMap) changeSync(cl *Client, syncID SyncID) {
c.Lock()
defer c.Unlock()
c.clients[cl] = syncID
}
// Remove removes a client from the map
func (c *ClientMap) remove(cl *Client) {
c.Lock()
defer c.Unlock()
delete(c.clients, cl)
}
// CountByIP returns the number of unique IPs synchronized with the server
func (c *ClientMap) CountByIP() int {
c.RLock()
ips := make(map[string]bool, len(c.clients))
for cl := range c.clients {
ips[cl.ip] = true
}
c.RUnlock()
return len(ips)
}
// Clear removes all clients from the map
func (c *ClientMap) Clear() {
c.Lock()
defer c.Unlock()
c.clients = make(map[*Client]SyncID)
}
// GetSync returns if the current client is synced and the thread and board it
// is synced to.
func (c *ClientMap) GetSync(cl *Client) (bool, SyncID) {
c.RLock()
defer c.RUnlock()
sync, ok := c.clients[cl]
return ok, sync
}