forked from anycable/anycable-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
173 lines (133 loc) · 4.08 KB
/
hub.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
)
type SubscriptionInfo struct {
conn *Conn
stream string
identifier string
}
type StreamMessage struct {
Stream string `json:"stream"`
Data string `json:"data"`
}
type Hub struct {
// Registered connections.
connections map[*Conn]bool
// Messages for all connections.
broadcast chan []byte
// Messages for specified stream.
stream_broadcast chan *StreamMessage
// Register requests from the connections.
register chan *Conn
// Unregister requests from connections.
unregister chan *Conn
// Subscribe requests to strreams.
subscribe chan *SubscriptionInfo
// Unsubscribe requests from streams.
unsubscribe chan *SubscriptionInfo
// Maps streams to connections
streams map[string]map[*Conn]string
// Maps connections to identifiers to streams
connection_streams map[*Conn]map[string][]string
}
var hub = Hub{
broadcast: make(chan []byte),
stream_broadcast: make(chan *StreamMessage),
register: make(chan *Conn),
unregister: make(chan *Conn),
subscribe: make(chan *SubscriptionInfo),
unsubscribe: make(chan *SubscriptionInfo),
connections: make(map[*Conn]bool),
streams: make(map[string]map[*Conn]string),
connection_streams: make(map[*Conn]map[string][]string),
}
func (h *Hub) run() {
for {
select {
case conn := <-h.register:
log.Debugf("Register connection %v", conn)
h.connections[conn] = true
case conn := <-h.unregister:
log.Debugf("Unregister connection %v", conn)
h.UnsubscribeConnection(conn)
if _, ok := h.connections[conn]; ok {
delete(h.connections, conn)
close(conn.send)
}
case message := <-h.broadcast:
log.Debugf("Broadcast message %s", message)
for conn := range h.connections {
select {
case conn.send <- message:
default:
close(conn.send)
delete(hub.connections, conn)
}
}
case stream_message := <-h.stream_broadcast:
log.Debugf("Broadcast to stream %s: %s", stream_message.Stream, stream_message.Data)
if _, ok := h.streams[stream_message.Stream]; !ok {
log.Debugf("No connections for stream %s", stream_message.Stream)
break
}
buf := make(map[string][]byte)
for conn, id := range h.streams[stream_message.Stream] {
var bdata []byte
if msg, ok := buf[id]; ok {
bdata = msg
} else {
bdata = BuildMessage(stream_message.Data, id)
buf[id] = bdata
}
select {
case conn.send <- bdata:
default:
close(conn.send)
delete(hub.connections, conn)
}
}
case subinfo := <-h.subscribe:
log.Debugf("Subscribe to stream %s for %s", subinfo.stream, subinfo.conn.identifiers)
if _, ok := h.streams[subinfo.stream]; !ok {
h.streams[subinfo.stream] = make(map[*Conn]string)
}
h.streams[subinfo.stream][subinfo.conn] = subinfo.identifier
if _, ok := h.connection_streams[subinfo.conn]; !ok {
h.connection_streams[subinfo.conn] = make(map[string][]string)
}
h.connection_streams[subinfo.conn][subinfo.identifier] = append(
h.connection_streams[subinfo.conn][subinfo.identifier],
subinfo.stream)
case subinfo := <-h.unsubscribe:
h.UnsubscribeConnectionFromChannel(subinfo.conn, subinfo.identifier)
}
}
}
func (h *Hub) Size() int {
return len(h.connections)
}
func (h *Hub) UnsubscribeConnection(conn *Conn) {
log.Debugf("Unsubscribe from all streams: %s", conn.identifiers)
for channel, _ := range h.connection_streams[conn] {
h.UnsubscribeConnectionFromChannel(conn, channel)
}
delete(h.connection_streams, conn)
}
func (h *Hub) UnsubscribeConnectionFromChannel(conn *Conn, channel string) {
log.Debugf("Unsubscribe from channel %s: %s", channel, conn.identifiers)
if _, ok := h.connection_streams[conn]; !ok {
return
}
for _, stream := range h.connection_streams[conn][channel] {
delete(h.streams[stream], conn)
if len(h.streams[stream]) == 0 {
delete(h.streams, stream)
}
}
}
func BuildMessage(data string, identifier string) []byte {
var msg map[string]interface{}
json.Unmarshal([]byte(data), &msg)
return (&Reply{Identifier: identifier, Message: msg}).toJSON()
}