-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
hub.go
1014 lines (898 loc) · 27.3 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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package centrifuge
import (
"context"
"io"
"sync"
"time"
"github.com/centrifugal/centrifuge/internal/convert"
"github.com/centrifugal/protocol"
"github.com/segmentio/encoding/json"
fdelta "github.com/shadowspore/fossil-delta"
)
const numHubShards = 64
// Hub tracks Client connections on the current Node.
type Hub struct {
connShards [numHubShards]*connShard
subShards [numHubShards]*subShard
sessionsMu sync.RWMutex
sessions map[string]*Client
}
// newHub initializes Hub.
func newHub(logger *logger, metrics *metrics, maxTimeLagMilli int64) *Hub {
h := &Hub{
sessions: map[string]*Client{},
}
for i := 0; i < numHubShards; i++ {
h.connShards[i] = newConnShard()
h.subShards[i] = newSubShard(logger, metrics, maxTimeLagMilli)
}
return h
}
func (h *Hub) clientBySession(session string) (*Client, bool) {
h.sessionsMu.RLock()
defer h.sessionsMu.RUnlock()
c, ok := h.sessions[session]
return c, ok
}
// shutdown unsubscribes users from all channels and disconnects them.
func (h *Hub) shutdown(ctx context.Context) error {
// Limit concurrency here to prevent resource usage burst on shutdown.
sem := make(chan struct{}, hubShutdownSemaphoreSize)
var errMu sync.Mutex
var shutdownErr error
var wg sync.WaitGroup
wg.Add(numHubShards)
for i := 0; i < numHubShards; i++ {
go func(i int) {
defer wg.Done()
err := h.connShards[i].shutdown(ctx, sem)
if err != nil {
errMu.Lock()
if shutdownErr == nil {
shutdownErr = err
}
errMu.Unlock()
}
}(i)
}
wg.Wait()
return shutdownErr
}
// Add connection into clientHub connections registry.
func (h *Hub) add(c *Client) error {
h.sessionsMu.Lock()
if c.sessionID() != "" {
h.sessions[c.sessionID()] = c
}
h.sessionsMu.Unlock()
return h.connShards[index(c.UserID(), numHubShards)].add(c)
}
// Remove connection from clientHub connections registry.
func (h *Hub) remove(c *Client) error {
h.sessionsMu.Lock()
if c.sessionID() != "" {
delete(h.sessions, c.sessionID())
}
h.sessionsMu.Unlock()
return h.connShards[index(c.UserID(), numHubShards)].remove(c)
}
// Connections returns all user connections to the current Node.
func (h *Hub) Connections() map[string]*Client {
connections := make(map[string]*Client)
for _, shard := range h.connShards {
shard.mu.RLock()
for clientID, c := range shard.clients {
connections[clientID] = c
}
shard.mu.RUnlock()
}
return connections
}
// UserConnections returns all user connections to the current Node.
func (h *Hub) UserConnections(userID string) map[string]*Client {
return h.connShards[index(userID, numHubShards)].userConnections(userID)
}
func (h *Hub) refresh(userID string, clientID, sessionID string, opts ...RefreshOption) error {
return h.connShards[index(userID, numHubShards)].refresh(userID, clientID, sessionID, opts...)
}
func (h *Hub) subscribe(userID string, ch string, clientID string, sessionID string, opts ...SubscribeOption) error {
return h.connShards[index(userID, numHubShards)].subscribe(userID, ch, clientID, sessionID, opts...)
}
func (h *Hub) unsubscribe(userID string, ch string, unsubscribe Unsubscribe, clientID string, sessionID string) error {
return h.connShards[index(userID, numHubShards)].unsubscribe(userID, ch, unsubscribe, clientID, sessionID)
}
func (h *Hub) disconnect(userID string, disconnect Disconnect, clientID, sessionID string, whitelist []string) error {
return h.connShards[index(userID, numHubShards)].disconnect(userID, disconnect, clientID, sessionID, whitelist)
}
func (h *Hub) addSub(ch string, sub subInfo) (bool, error) {
return h.subShards[index(ch, numHubShards)].addSub(ch, sub)
}
// removeSub removes connection from clientHub subscriptions registry.
func (h *Hub) removeSub(ch string, c *Client) (bool, error) {
return h.subShards[index(ch, numHubShards)].removeSub(ch, c)
}
// BroadcastPublication sends message to all clients subscribed on a channel on the current Node.
// Usually this is NOT what you need since in most cases you should use Node.Publish method which
// uses a Broker to deliver publications to all Nodes in a cluster and maintains publication history
// in a channel with incremental offset. By calling BroadcastPublication messages will only be sent
// to the current node subscribers without any defined offset semantics, without delta support.
func (h *Hub) BroadcastPublication(ch string, pub *Publication, sp StreamPosition) error {
return h.broadcastPublication(ch, sp, pub, nil, nil)
}
func (h *Hub) broadcastPublication(ch string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
return h.subShards[index(ch, numHubShards)].broadcastPublication(ch, sp, pub, prevPub, localPrevPub)
}
// broadcastJoin sends message to all clients subscribed on channel.
func (h *Hub) broadcastJoin(ch string, info *ClientInfo) error {
return h.subShards[index(ch, numHubShards)].broadcastJoin(ch, &protocol.Join{Info: infoToProto(info)})
}
func (h *Hub) broadcastLeave(ch string, info *ClientInfo) error {
return h.subShards[index(ch, numHubShards)].broadcastLeave(ch, &protocol.Leave{Info: infoToProto(info)})
}
// NumSubscribers returns number of current subscribers for a given channel.
func (h *Hub) NumSubscribers(ch string) int {
return h.subShards[index(ch, numHubShards)].NumSubscribers(ch)
}
// Channels returns a slice of all active channels.
func (h *Hub) Channels() []string {
channels := make([]string, 0, h.NumChannels())
for i := 0; i < numHubShards; i++ {
channels = append(channels, h.subShards[i].Channels()...)
}
return channels
}
// NumClients returns total number of client connections.
func (h *Hub) NumClients() int {
var total int
for i := 0; i < numHubShards; i++ {
total += h.connShards[i].NumClients()
}
return total
}
// NumUsers returns a number of unique users connected.
func (h *Hub) NumUsers() int {
var total int
for i := 0; i < numHubShards; i++ {
// users do not overlap among shards.
total += h.connShards[i].NumUsers()
}
return total
}
// NumSubscriptions returns a total number of subscriptions.
func (h *Hub) NumSubscriptions() int {
var total int
for i := 0; i < numHubShards; i++ {
// users do not overlap among shards.
total += h.subShards[i].NumSubscriptions()
}
return total
}
// NumChannels returns a total number of different channels.
func (h *Hub) NumChannels() int {
var total int
for i := 0; i < numHubShards; i++ {
// channels do not overlap among shards.
total += h.subShards[i].NumChannels()
}
return total
}
type connShard struct {
mu sync.RWMutex
// match client ID with actual client connection.
clients map[string]*Client
// registry to hold active client connections grouped by user.
users map[string]map[string]struct{}
}
func newConnShard() *connShard {
return &connShard{
clients: make(map[string]*Client),
users: make(map[string]map[string]struct{}),
}
}
const (
// hubShutdownSemaphoreSize limits graceful disconnects concurrency
// on node shutdown.
hubShutdownSemaphoreSize = 128
)
// shutdown unsubscribes users from all channels and disconnects them.
func (h *connShard) shutdown(ctx context.Context, sem chan struct{}) error {
advice := DisconnectShutdown
h.mu.RLock()
// At this moment node won't accept new client connections, so we can
// safely copy existing clients and release lock.
clients := make([]*Client, 0, len(h.clients))
for _, client := range h.clients {
clients = append(clients, client)
}
h.mu.RUnlock()
closeFinishedCh := make(chan struct{}, len(clients))
finished := 0
if len(clients) == 0 {
return nil
}
for _, client := range clients {
select {
case sem <- struct{}{}:
case <-ctx.Done():
return ctx.Err()
}
go func(cc *Client) {
defer func() { <-sem }()
defer func() { closeFinishedCh <- struct{}{} }()
_ = cc.close(advice)
}(client)
}
for {
select {
case <-closeFinishedCh:
finished++
if finished == len(clients) {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func stringInSlice(str string, slice []string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
func (h *connShard) subscribe(user string, ch string, clientID string, sessionID string, opts ...SubscribeOption) error {
userConnections := h.userConnections(user)
var firstErr error
var errMu sync.Mutex
var wg sync.WaitGroup
for _, c := range userConnections {
if clientID != "" && c.ID() != clientID {
continue
}
if sessionID != "" && c.sessionID() != sessionID {
continue
}
wg.Add(1)
go func(c *Client) {
defer wg.Done()
err := c.Subscribe(ch, opts...)
errMu.Lock()
defer errMu.Unlock()
if err != nil && err != io.EOF && firstErr == nil {
firstErr = err
}
}(c)
}
wg.Wait()
return firstErr
}
func (h *connShard) refresh(user string, clientID string, sessionID string, opts ...RefreshOption) error {
userConnections := h.userConnections(user)
var firstErr error
var errMu sync.Mutex
var wg sync.WaitGroup
for _, c := range userConnections {
if clientID != "" && c.ID() != clientID {
continue
}
if sessionID != "" && c.sessionID() != sessionID {
continue
}
wg.Add(1)
go func(c *Client) {
defer wg.Done()
err := c.Refresh(opts...)
errMu.Lock()
defer errMu.Unlock()
if err != nil && err != io.EOF && firstErr == nil {
firstErr = err
}
}(c)
}
wg.Wait()
return firstErr
}
func (h *connShard) unsubscribe(user string, ch string, unsubscribe Unsubscribe, clientID string, sessionID string) error {
userConnections := h.userConnections(user)
var wg sync.WaitGroup
for _, c := range userConnections {
if clientID != "" && c.ID() != clientID {
continue
}
if sessionID != "" && c.sessionID() != sessionID {
continue
}
wg.Add(1)
go func(c *Client) {
defer wg.Done()
c.Unsubscribe(ch, unsubscribe)
}(c)
}
wg.Wait()
return nil
}
func (h *connShard) disconnect(user string, disconnect Disconnect, clientID string, sessionID string, whitelist []string) error {
userConnections := h.userConnections(user)
var firstErr error
var errMu sync.Mutex
var wg sync.WaitGroup
for _, c := range userConnections {
if stringInSlice(c.ID(), whitelist) {
continue
}
if clientID != "" && c.ID() != clientID {
continue
}
if sessionID != "" && c.sessionID() != sessionID {
continue
}
wg.Add(1)
go func(cc *Client) {
defer wg.Done()
err := cc.close(disconnect)
errMu.Lock()
defer errMu.Unlock()
if err != nil && err != io.EOF && firstErr == nil {
firstErr = err
}
}(c)
}
wg.Wait()
return firstErr
}
// userConnections returns all connections of user with specified User.
func (h *connShard) userConnections(userID string) map[string]*Client {
h.mu.RLock()
defer h.mu.RUnlock()
userConnections, ok := h.users[userID]
if !ok {
return map[string]*Client{}
}
connections := make(map[string]*Client, len(userConnections))
for uid := range userConnections {
c, ok := h.clients[uid]
if !ok {
continue
}
connections[uid] = c
}
return connections
}
// Add connection into clientHub connections registry.
func (h *connShard) add(c *Client) error {
h.mu.Lock()
defer h.mu.Unlock()
uid := c.ID()
user := c.UserID()
h.clients[uid] = c
if _, ok := h.users[user]; !ok {
h.users[user] = make(map[string]struct{})
}
h.users[user][uid] = struct{}{}
return nil
}
// Remove connection from clientHub connections registry.
func (h *connShard) remove(c *Client) error {
h.mu.Lock()
defer h.mu.Unlock()
uid := c.ID()
user := c.UserID()
delete(h.clients, uid)
// try to find connection to delete, return early if not found.
if _, ok := h.users[user]; !ok {
return nil
}
if _, ok := h.users[user][uid]; !ok {
return nil
}
// actually remove connection from hub.
delete(h.users[user], uid)
// clean up users map if it's needed.
if len(h.users[user]) == 0 {
delete(h.users, user)
}
return nil
}
// NumClients returns total number of client connections.
func (h *connShard) NumClients() int {
h.mu.RLock()
defer h.mu.RUnlock()
total := 0
for _, clientConnections := range h.users {
total += len(clientConnections)
}
return total
}
// NumUsers returns a number of unique users connected.
func (h *connShard) NumUsers() int {
h.mu.RLock()
defer h.mu.RUnlock()
return len(h.users)
}
type DeltaType string
const (
deltaTypeNone DeltaType = ""
// DeltaTypeFossil is Fossil delta encoding. See https://fossil-scm.org/home/doc/tip/www/delta_encoder_algorithm.wiki.
DeltaTypeFossil DeltaType = "fossil"
)
var stringToDeltaType = map[string]DeltaType{
"fossil": DeltaTypeFossil,
}
type subInfo struct {
client *Client
deltaType DeltaType
}
type subShard struct {
mu sync.RWMutex
// registry to hold active subscriptions of clients to channels with some additional info.
subs map[string]map[string]subInfo
maxTimeLagMilli int64
logger *logger
metrics *metrics
}
func newSubShard(logger *logger, metrics *metrics, maxTimeLagMilli int64) *subShard {
return &subShard{
subs: make(map[string]map[string]subInfo),
logger: logger,
metrics: metrics,
maxTimeLagMilli: maxTimeLagMilli,
}
}
// addSub adds connection into clientHub subscriptions registry.
func (h *subShard) addSub(ch string, sub subInfo) (bool, error) {
h.mu.Lock()
defer h.mu.Unlock()
uid := sub.client.ID()
_, ok := h.subs[ch]
if !ok {
h.subs[ch] = make(map[string]subInfo)
}
h.subs[ch][uid] = sub
if !ok {
return true, nil
}
return false, nil
}
// removeSub removes connection from clientHub subscriptions registry.
func (h *subShard) removeSub(ch string, c *Client) (bool, error) {
h.mu.Lock()
defer h.mu.Unlock()
uid := c.ID()
// try to find subscription to delete, return early if not found.
if _, ok := h.subs[ch]; !ok {
return true, nil
}
if _, ok := h.subs[ch][uid]; !ok {
return true, nil
}
// actually remove subscription from hub.
delete(h.subs[ch], uid)
// clean up subs map if it's needed.
if len(h.subs[ch]) == 0 {
delete(h.subs, ch)
return true, nil
}
return false, nil
}
type encodeError struct {
client string
user string
error error
}
type preparedKey struct {
ProtocolType protocol.Type
Unidirectional bool
DeltaType DeltaType
}
type preparedData struct {
fullData []byte
brokerDeltaData []byte
localDeltaData []byte
deltaSub bool
}
func getDeltaPub(prevPub *Publication, fullPub *protocol.Publication, key preparedKey) *protocol.Publication {
deltaPub := fullPub
if prevPub != nil && key.DeltaType == DeltaTypeFossil {
patch := fdelta.Create(prevPub.Data, fullPub.Data)
delta := true
deltaData := patch
if len(patch) >= len(fullPub.Data) {
delta = false
deltaData = fullPub.Data
}
if key.ProtocolType == protocol.TypeJSON {
deltaData = json.Escape(convert.BytesToString(deltaData))
}
deltaPub = &protocol.Publication{
Offset: fullPub.Offset,
Data: deltaData,
Info: fullPub.Info,
Tags: fullPub.Tags,
Delta: delta,
}
} else if prevPub == nil && key.ProtocolType == protocol.TypeJSON && key.DeltaType == DeltaTypeFossil {
// In JSON and Fossil case we need to send full state in JSON string format.
deltaPub = &protocol.Publication{
Offset: fullPub.Offset,
Data: json.Escape(convert.BytesToString(fullPub.Data)),
Info: fullPub.Info,
Tags: fullPub.Tags,
}
}
return deltaPub
}
func getDeltaData(sub subInfo, key preparedKey, channel string, deltaPub *protocol.Publication, jsonEncodeErr *encodeError) ([]byte, error) {
var deltaData []byte
if key.ProtocolType == protocol.TypeJSON {
if sub.client.transport.Unidirectional() {
push := &protocol.Push{Channel: channel, Pub: deltaPub}
var err error
deltaData, err = protocol.DefaultJsonPushEncoder.Encode(push)
if err != nil {
*jsonEncodeErr = encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
}
} else {
push := &protocol.Push{Channel: channel, Pub: deltaPub}
var err error
deltaData, err = protocol.DefaultJsonReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
*jsonEncodeErr = encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
}
}
} else if key.ProtocolType == protocol.TypeProtobuf {
if sub.client.transport.Unidirectional() {
push := &protocol.Push{Channel: channel, Pub: deltaPub}
var err error
deltaData, err = protocol.DefaultProtobufPushEncoder.Encode(push)
if err != nil {
return nil, err
}
} else {
push := &protocol.Push{Channel: channel, Pub: deltaPub}
var err error
deltaData, err = protocol.DefaultProtobufReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
return nil, err
}
}
}
return deltaData, nil
}
// broadcastPublication sends message to all clients subscribed on a channel.
func (h *subShard) broadcastPublication(channel string, sp StreamPosition, pub, prevPub, localPrevPub *Publication) error {
pubTime := pub.Time
// Check lag in PUB/SUB processing. We use it to notify subscribers with positioning enabled
// about insufficient state in the stream.
var maxLagExceeded bool
now := time.Now()
if pubTime > 0 {
timeLagMilli := now.UnixMilli() - pubTime
if h.maxTimeLagMilli > 0 && timeLagMilli > h.maxTimeLagMilli {
maxLagExceeded = true
}
h.metrics.observePubSubDeliveryLag(timeLagMilli)
}
fullPub := pubToProto(pub)
preparedDataByKey := make(map[preparedKey]preparedData)
h.mu.RLock()
defer h.mu.RUnlock()
channelSubscribers, ok := h.subs[channel]
if !ok {
return nil
}
if pub.Channel != channel {
fullPub.Channel = pub.Channel
}
var (
jsonEncodeErr *encodeError
)
for _, sub := range channelSubscribers {
key := preparedKey{
ProtocolType: sub.client.Transport().Protocol().toProto(),
Unidirectional: sub.client.transport.Unidirectional(),
DeltaType: sub.deltaType,
}
prepValue, prepDataFound := preparedDataByKey[key]
if !prepDataFound {
var brokerDeltaPub *protocol.Publication
if fullPub.Offset > 0 {
brokerDeltaPub = getDeltaPub(prevPub, fullPub, key)
}
localDeltaPub := getDeltaPub(localPrevPub, fullPub, key)
var brokerDeltaData []byte
var localDeltaData []byte
if key.DeltaType != deltaTypeNone {
var err error
brokerDeltaData, err = getDeltaData(sub, key, channel, brokerDeltaPub, jsonEncodeErr)
if err != nil {
return err
}
localDeltaData, err = getDeltaData(sub, key, channel, localDeltaPub, jsonEncodeErr)
if err != nil {
return err
}
}
var fullData []byte
if key.ProtocolType == protocol.TypeJSON {
if sub.client.transport.Unidirectional() {
pubToUse := fullPub
if key.ProtocolType == protocol.TypeJSON && key.DeltaType == DeltaTypeFossil {
pubToUse = &protocol.Publication{
Offset: fullPub.Offset,
Data: json.Escape(convert.BytesToString(fullPub.Data)),
Info: fullPub.Info,
Tags: fullPub.Tags,
Channel: fullPub.Channel,
}
}
push := &protocol.Push{Channel: channel, Pub: pubToUse}
var err error
fullData, err = protocol.DefaultJsonPushEncoder.Encode(push)
if err != nil {
jsonEncodeErr = &encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
}
} else {
pubToUse := fullPub
if key.ProtocolType == protocol.TypeJSON && key.DeltaType == DeltaTypeFossil {
pubToUse = &protocol.Publication{
Offset: fullPub.Offset,
Data: json.Escape(convert.BytesToString(fullPub.Data)),
Info: fullPub.Info,
Tags: fullPub.Tags,
Channel: fullPub.Channel,
}
}
push := &protocol.Push{Channel: channel, Pub: pubToUse}
var err error
fullData, err = protocol.DefaultJsonReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
jsonEncodeErr = &encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
}
}
} else if key.ProtocolType == protocol.TypeProtobuf {
if sub.client.transport.Unidirectional() {
push := &protocol.Push{Channel: channel, Pub: fullPub}
var err error
fullData, err = protocol.DefaultProtobufPushEncoder.Encode(push)
if err != nil {
return err
}
} else {
push := &protocol.Push{Channel: channel, Pub: fullPub}
var err error
fullData, err = protocol.DefaultProtobufReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
return err
}
}
}
prepValue = preparedData{
fullData: fullData,
brokerDeltaData: brokerDeltaData,
localDeltaData: localDeltaData,
deltaSub: key.DeltaType != deltaTypeNone,
}
preparedDataByKey[key] = prepValue
}
if sub.client.transport.Protocol() == ProtocolTypeJSON && jsonEncodeErr != nil {
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
_ = sub.client.writePublication(channel, fullPub, prepValue, sp, maxLagExceeded)
}
if jsonEncodeErr != nil && h.logger.enabled(LogLevelWarn) {
// Log that we had clients with inappropriate protocol, and point to the first such client.
h.logger.log(NewLogEntry(LogLevelWarn, "inappropriate protocol publication", map[string]any{
"channel": channel,
"user": jsonEncodeErr.user,
"client": jsonEncodeErr.client,
"error": jsonEncodeErr.error,
}))
}
h.metrics.observeBroadcastDuration(now)
return nil
}
// broadcastJoin sends message to all clients subscribed on channel.
func (h *subShard) broadcastJoin(channel string, join *protocol.Join) error {
h.mu.RLock()
defer h.mu.RUnlock()
channelSubscribers, ok := h.subs[channel]
if !ok {
return nil
}
var (
jsonReply []byte
protobufReply []byte
jsonPush []byte
protobufPush []byte
jsonEncodeErr *encodeError
)
for _, sub := range channelSubscribers {
protoType := sub.client.Transport().Protocol().toProto()
if protoType == protocol.TypeJSON {
if jsonEncodeErr != nil {
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
if sub.client.transport.Unidirectional() {
if jsonPush == nil {
push := &protocol.Push{Channel: channel, Join: join}
var err error
jsonPush, err = protocol.DefaultJsonPushEncoder.Encode(push)
if err != nil {
jsonEncodeErr = &encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
}
_ = sub.client.writeJoin(channel, join, jsonPush)
} else {
if jsonReply == nil {
push := &protocol.Push{Channel: channel, Join: join}
var err error
jsonReply, err = protocol.DefaultJsonReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
jsonEncodeErr = &encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
}
_ = sub.client.writeJoin(channel, join, jsonReply)
}
} else if protoType == protocol.TypeProtobuf {
if sub.client.transport.Unidirectional() {
if protobufPush == nil {
push := &protocol.Push{Channel: channel, Join: join}
var err error
protobufPush, err = protocol.DefaultProtobufPushEncoder.Encode(push)
if err != nil {
return err
}
}
_ = sub.client.writeJoin(channel, join, protobufPush)
} else {
if protobufReply == nil {
push := &protocol.Push{Channel: channel, Join: join}
var err error
protobufReply, err = protocol.DefaultProtobufReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
return err
}
}
_ = sub.client.writeJoin(channel, join, protobufReply)
}
}
}
if jsonEncodeErr != nil && h.logger.enabled(LogLevelWarn) {
// Log that we had clients with inappropriate protocol, and point to the first such client.
h.logger.log(NewLogEntry(LogLevelWarn, "inappropriate protocol join", map[string]any{
"channel": channel,
"user": jsonEncodeErr.user,
"client": jsonEncodeErr.client,
"error": jsonEncodeErr.error,
}))
}
return nil
}
// broadcastLeave sends message to all clients subscribed on channel.
func (h *subShard) broadcastLeave(channel string, leave *protocol.Leave) error {
h.mu.RLock()
defer h.mu.RUnlock()
channelSubscribers, ok := h.subs[channel]
if !ok {
return nil
}
var (
jsonReply []byte
protobufReply []byte
jsonPush []byte
protobufPush []byte
jsonEncodeErr *encodeError
)
for _, sub := range channelSubscribers {
protoType := sub.client.Transport().Protocol().toProto()
if protoType == protocol.TypeJSON {
if jsonEncodeErr != nil {
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
if sub.client.transport.Unidirectional() {
if jsonPush == nil {
push := &protocol.Push{Channel: channel, Leave: leave}
var err error
jsonPush, err = protocol.DefaultJsonPushEncoder.Encode(push)
if err != nil {
jsonEncodeErr = &encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
}
_ = sub.client.writeLeave(channel, leave, jsonPush)
} else {
if jsonReply == nil {
push := &protocol.Push{Channel: channel, Leave: leave}
var err error
jsonReply, err = protocol.DefaultJsonReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
jsonEncodeErr = &encodeError{client: sub.client.ID(), user: sub.client.UserID(), error: err}
go func(c *Client) { c.Disconnect(DisconnectInappropriateProtocol) }(sub.client)
continue
}
}
_ = sub.client.writeLeave(channel, leave, jsonReply)
}
} else if protoType == protocol.TypeProtobuf {
if sub.client.transport.Unidirectional() {
if protobufPush == nil {
push := &protocol.Push{Channel: channel, Leave: leave}
var err error
protobufPush, err = protocol.DefaultProtobufPushEncoder.Encode(push)
if err != nil {
return err
}
}
_ = sub.client.writeLeave(channel, leave, protobufPush)
} else {
if protobufReply == nil {
push := &protocol.Push{Channel: channel, Leave: leave}
var err error
protobufReply, err = protocol.DefaultProtobufReplyEncoder.Encode(&protocol.Reply{Push: push})
if err != nil {
return err
}
}
_ = sub.client.writeLeave(channel, leave, protobufReply)
}
}
}
if jsonEncodeErr != nil && h.logger.enabled(LogLevelWarn) {
// Log that we had clients with inappropriate protocol, and point to the first such client.
h.logger.log(NewLogEntry(LogLevelWarn, "inappropriate protocol leave", map[string]any{
"channel": channel,
"user": jsonEncodeErr.user,
"client": jsonEncodeErr.client,
"error": jsonEncodeErr.error,
}))
}
return nil
}
// NumChannels returns a total number of different channels.
func (h *subShard) NumChannels() int {
h.mu.RLock()
defer h.mu.RUnlock()
return len(h.subs)
}
// NumSubscriptions returns total number of subscriptions.
func (h *subShard) NumSubscriptions() int {
h.mu.RLock()
defer h.mu.RUnlock()
total := 0
for _, subscriptions := range h.subs {
total += len(subscriptions)
}
return total
}
// Channels returns a slice of all active channels.
func (h *subShard) Channels() []string {
h.mu.RLock()
defer h.mu.RUnlock()
channels := make([]string, len(h.subs))
i := 0
for ch := range h.subs {
channels[i] = ch
i++