-
Notifications
You must be signed in to change notification settings - Fork 14
/
registry.go
52 lines (42 loc) · 1.11 KB
/
registry.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
package socket
import (
"sync"
"time"
)
const (
// Time allowed to write a broadcastMessage to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong broadcastMessage from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum broadcastMessage size allowed from peer.
maxMessageSize = 512
)
type Registry struct {
Register chan *Socket
Unregister chan *Socket
Sockets *sync.Map
}
func BuildAndRunSocketRegistry(registerHandler, unregisterHandler func(s *Socket)) *Registry {
registry := &Registry{
Register: make(chan *Socket),
Unregister: make(chan *Socket),
Sockets: &sync.Map{},
}
go registry.run(registerHandler, unregisterHandler)
return registry
}
func (registry *Registry) run(registerHandler, unregisterHandler func(s *Socket)) {
for {
select {
case s := <-registry.Register: //new connection
registry.Sockets.Store(s.ID, s)
registerHandler(s)
case s := <-registry.Unregister:
registry.Sockets.Delete(s.ID)
unregisterHandler(s)
break
}
}
}