forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_broadcast.go
562 lines (454 loc) · 11.5 KB
/
redis_broadcast.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package socketio
import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"github.com/gomodule/redigo/redis"
)
// redisBroadcast gives Join, Leave & BroadcastTO server API support to socket.io along with room management
// map of rooms where each room contains a map of connection id to connections in that room
type redisBroadcast struct {
pub *redis.PubSubConn
sub *redis.PubSubConn
nsp string
uid string
key string
reqChannel string
resChannel string
requests map[string]interface{}
rooms map[string]map[string]Conn
lock sync.RWMutex
}
// request types
const (
roomLenReqType = "0"
clearRoomReqType = "1"
allRoomReqType = "2"
)
// request structs
type roomLenRequest struct {
RequestType string
RequestID string
Room string
numSub int `json:"-"`
msgCount int `json:"-"`
connections int `json:"-"`
mutex sync.Mutex `json:"-"`
done chan bool `json:"-"`
}
type clearRoomRequest struct {
RequestType string
RequestID string
Room string
UUID string
}
type allRoomRequest struct {
RequestType string
RequestID string
rooms map[string]bool `json:"-"`
numSub int `json:"-"`
msgCount int `json:"-"`
mutex sync.Mutex `json:"-"`
done chan bool `json:"-"`
}
// response struct
type roomLenResponse struct {
RequestType string
RequestID string
Connections int
}
type allRoomResponse struct {
RequestType string
RequestID string
Rooms []string
}
func newRedisBroadcast(nsp string, opts *RedisAdapterOptions) (*redisBroadcast, error) {
addr := opts.getAddr()
var redisOpts []redis.DialOption
if len(opts.Password) > 0 {
redisOpts = append(redisOpts, redis.DialPassword(opts.Password))
}
pub, err := redis.Dial(opts.Network, addr, redisOpts...)
if err != nil {
return nil, err
}
sub, err := redis.Dial(opts.Network, addr, redisOpts...)
if err != nil {
return nil, err
}
subConn := &redis.PubSubConn{Conn: sub}
pubConn := &redis.PubSubConn{Conn: pub}
if err = subConn.PSubscribe(fmt.Sprintf("%s#%s#*", opts.Prefix, nsp)); err != nil {
return nil, err
}
uid := newV4UUID()
rbc := &redisBroadcast{
rooms: make(map[string]map[string]Conn),
requests: make(map[string]interface{}),
sub: subConn,
pub: pubConn,
key: fmt.Sprintf("%s#%s#%s", opts.Prefix, nsp, uid),
reqChannel: fmt.Sprintf("%s-request#%s", opts.Prefix, nsp),
resChannel: fmt.Sprintf("%s-response#%s", opts.Prefix, nsp),
nsp: nsp,
uid: uid,
}
if err = subConn.Subscribe(rbc.reqChannel, rbc.resChannel); err != nil {
return nil, err
}
go rbc.dispatch()
return rbc, nil
}
// AllRooms gives list of all rooms available for redisBroadcast.
func (bc *redisBroadcast) AllRooms() []string {
req := allRoomRequest{
RequestType: allRoomReqType,
RequestID: newV4UUID(),
}
reqJSON, _ := json.Marshal(&req)
req.rooms = make(map[string]bool)
numSub, _ := bc.getNumSub(bc.reqChannel)
req.numSub = numSub
req.done = make(chan bool, 1)
bc.requests[req.RequestID] = &req
_, err := bc.pub.Conn.Do("PUBLISH", bc.reqChannel, reqJSON)
if err != nil {
return []string{} // if error occurred,return empty
}
<-req.done
rooms := make([]string, 0, len(req.rooms))
for room := range req.rooms {
rooms = append(rooms, room)
}
delete(bc.requests, req.RequestID)
return rooms
}
// Join joins the given connection to the redisBroadcast room.
func (bc *redisBroadcast) Join(room string, connection Conn) {
bc.lock.Lock()
defer bc.lock.Unlock()
if _, ok := bc.rooms[room]; !ok {
bc.rooms[room] = make(map[string]Conn)
}
bc.rooms[room][connection.ID()] = connection
}
// Leave leaves the given connection from given room (if exist)
func (bc *redisBroadcast) Leave(room string, connection Conn) {
bc.lock.Lock()
defer bc.lock.Unlock()
if connections, ok := bc.rooms[room]; ok {
delete(connections, connection.ID())
if len(connections) == 0 {
delete(bc.rooms, room)
}
}
}
// LeaveAll leaves the given connection from all rooms.
func (bc *redisBroadcast) LeaveAll(connection Conn) {
bc.lock.Lock()
defer bc.lock.Unlock()
for room, connections := range bc.rooms {
delete(connections, connection.ID())
if len(connections) == 0 {
delete(bc.rooms, room)
}
}
}
// Clear clears the room.
func (bc *redisBroadcast) Clear(room string) {
bc.lock.Lock()
defer bc.lock.Unlock()
delete(bc.rooms, room)
go bc.publishClear(room)
}
// Send sends given event & args to all the connections in the specified room.
func (bc *redisBroadcast) Send(room, event string, args ...interface{}) {
bc.lock.RLock()
defer bc.lock.RUnlock()
connections, ok := bc.rooms[room]
if ok {
for _, connection := range connections {
connection.Emit(event, args...)
}
}
bc.publishMessage(room, event, args...)
}
// SendAll sends given event & args to all the connections to all the rooms.
func (bc *redisBroadcast) SendAll(event string, args ...interface{}) {
bc.lock.RLock()
defer bc.lock.RUnlock()
for _, connections := range bc.rooms {
for _, connection := range connections {
connection.Emit(event, args...)
}
}
bc.publishMessage("", event, args...)
}
// ForEach sends data returned by DataFunc, if room does not exits sends nothing.
func (bc *redisBroadcast) ForEach(room string, f EachFunc) {
bc.lock.RLock()
defer bc.lock.RUnlock()
occupants, ok := bc.rooms[room]
if !ok {
return
}
for _, connection := range occupants {
f(connection)
}
}
// Len gives number of connections in the room.
func (bc *redisBroadcast) Len(room string) int {
req := roomLenRequest{
RequestType: roomLenReqType,
RequestID: newV4UUID(),
Room: room,
}
reqJSON, err := json.Marshal(&req)
if err != nil {
return -1
}
numSub, err := bc.getNumSub(bc.reqChannel)
if err != nil {
return -1
}
req.numSub = numSub
req.done = make(chan bool, 1)
bc.requests[req.RequestID] = &req
_, err = bc.pub.Conn.Do("PUBLISH", bc.reqChannel, reqJSON)
if err != nil {
return -1
}
<-req.done
delete(bc.requests, req.RequestID)
return req.connections
}
// Rooms gives the list of all the rooms available for redisBroadcast in case of
// no connection is given, in case of a connection is given, it gives
// list of all the rooms the connection is joined to.
func (bc *redisBroadcast) Rooms(connection Conn) []string {
bc.lock.RLock()
defer bc.lock.RUnlock()
if connection == nil {
return bc.AllRooms()
}
return bc.getRoomsByConn(connection)
}
func (bc *redisBroadcast) onMessage(channel string, msg []byte) error {
channelParts := strings.Split(channel, "#")
nsp := channelParts[len(channelParts)-2]
if bc.nsp != nsp {
return nil
}
uid := channelParts[len(channelParts)-1]
if bc.uid == uid {
return nil
}
var bcMessage map[string][]interface{}
err := json.Unmarshal(msg, &bcMessage)
if err != nil {
return errors.New("invalid broadcast message")
}
args := bcMessage["args"]
opts := bcMessage["opts"]
room, ok := opts[0].(string)
if !ok {
return errors.New("invalid room")
}
event, ok := opts[1].(string)
if !ok {
return errors.New("invalid event")
}
if room != "" {
bc.send(room, event, args...)
} else {
bc.sendAll(event, args...)
}
return nil
}
// Get the number of subscribers of a channel.
func (bc *redisBroadcast) getNumSub(channel string) (int, error) {
rs, err := bc.pub.Conn.Do("PUBSUB", "NUMSUB", channel)
if err != nil {
return 0, err
}
numSub64, ok := rs.([]interface{})[1].(int64)
if !ok {
return 0, errors.New("redis reply cast to int error")
}
return int(numSub64), nil
}
// Handle request from redis channel.
func (bc *redisBroadcast) onRequest(msg []byte) {
var req map[string]string
if err := json.Unmarshal(msg, &req); err != nil {
return
}
var res interface{}
switch req["RequestType"] {
case roomLenReqType:
res = roomLenResponse{
RequestType: req["RequestType"],
RequestID: req["RequestID"],
Connections: len(bc.rooms[req["Room"]]),
}
bc.publish(bc.resChannel, &res)
case allRoomReqType:
res := allRoomResponse{
RequestType: req["RequestType"],
RequestID: req["RequestID"],
Rooms: bc.allRooms(),
}
bc.publish(bc.resChannel, &res)
case clearRoomReqType:
if bc.uid == req["UUID"] {
return
}
bc.clear(req["Room"])
default:
}
}
func (bc *redisBroadcast) publish(channel string, msg interface{}) {
resJSON, err := json.Marshal(msg)
if err != nil {
return
}
_, err = bc.pub.Conn.Do("PUBLISH", channel, resJSON)
if err != nil {
return
}
}
// Handle response from redis channel.
func (bc *redisBroadcast) onResponse(msg []byte) {
var res map[string]interface{}
err := json.Unmarshal(msg, &res)
if err != nil {
return
}
req, ok := bc.requests[res["RequestID"].(string)]
if !ok {
return
}
switch res["RequestType"] {
case roomLenReqType:
roomLenReq := req.(*roomLenRequest)
roomLenReq.mutex.Lock()
roomLenReq.msgCount++
roomLenReq.connections += int(res["Connections"].(float64))
roomLenReq.mutex.Unlock()
if roomLenReq.numSub == roomLenReq.msgCount {
roomLenReq.done <- true
}
case allRoomReqType:
allRoomReq := req.(*allRoomRequest)
rooms, ok := res["Rooms"].([]interface{})
if !ok {
allRoomReq.done <- true
return
}
allRoomReq.mutex.Lock()
allRoomReq.msgCount++
for _, room := range rooms {
allRoomReq.rooms[room.(string)] = true
}
allRoomReq.mutex.Unlock()
if allRoomReq.numSub == allRoomReq.msgCount {
allRoomReq.done <- true
}
default:
}
}
func (bc *redisBroadcast) publishClear(room string) {
req := clearRoomRequest{
RequestType: clearRoomReqType,
RequestID: newV4UUID(),
Room: room,
UUID: bc.uid,
}
bc.publish(bc.reqChannel, &req)
}
func (bc *redisBroadcast) clear(room string) {
bc.lock.Lock()
defer bc.lock.Unlock()
delete(bc.rooms, room)
}
func (bc *redisBroadcast) send(room string, event string, args ...interface{}) {
bc.lock.RLock()
defer bc.lock.RUnlock()
connections, ok := bc.rooms[room]
if !ok {
return
}
for _, connection := range connections {
connection.Emit(event, args...)
}
}
func (bc *redisBroadcast) publishMessage(room string, event string, args ...interface{}) {
opts := make([]interface{}, 2)
opts[0] = room
opts[1] = event
bcMessage := map[string][]interface{}{
"opts": opts,
"args": args,
}
bcMessageJSON, err := json.Marshal(bcMessage)
if err != nil {
return
}
_, err = bc.pub.Conn.Do("PUBLISH", bc.key, bcMessageJSON)
if err != nil {
return
}
}
func (bc *redisBroadcast) sendAll(event string, args ...interface{}) {
bc.lock.RLock()
defer bc.lock.RUnlock()
for _, connections := range bc.rooms {
for _, connection := range connections {
connection.Emit(event, args...)
}
}
}
func (bc *redisBroadcast) allRooms() []string {
bc.lock.RLock()
defer bc.lock.RUnlock()
rooms := make([]string, 0, len(bc.rooms))
for room := range bc.rooms {
rooms = append(rooms, room)
}
return rooms
}
func (bc *redisBroadcast) getRoomsByConn(connection Conn) []string {
var rooms []string
for room, connections := range bc.rooms {
if _, ok := connections[connection.ID()]; ok {
rooms = append(rooms, room)
}
}
return rooms
}
func (bc *redisBroadcast) dispatch() {
for {
switch m := bc.sub.Receive().(type) {
case redis.Message:
if m.Channel == bc.reqChannel {
bc.onRequest(m.Data)
break
} else if m.Channel == bc.resChannel {
bc.onResponse(m.Data)
break
}
err := bc.onMessage(m.Channel, m.Data)
if err != nil {
return
}
case redis.Subscription:
if m.Count == 0 {
return
}
case error:
return
}
}
}