forked from heroiclabs/nakama
/
tracker.go
961 lines (868 loc) · 28.6 KB
/
tracker.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
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"bytes"
"context"
"github.com/golang/protobuf/proto"
"sync"
"fmt"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/heroiclabs/nakama/rtapi"
"go.uber.org/zap"
)
const (
StreamModeNotifications uint8 = iota
StreamModeStatus
StreamModeChannel
StreamModeGroup
StreamModeDM
StreamModeMatchRelayed
StreamModeMatchAuthoritative
)
type PresenceID struct {
Node string
SessionID uuid.UUID
}
type PresenceStream struct {
Mode uint8
Subject uuid.UUID
Subcontext uuid.UUID
Label string
}
type PresenceMeta struct {
Format SessionFormat
Hidden bool
Persistence bool
Username string
Status string
}
func (pm *PresenceMeta) GetHidden() bool {
return pm.Hidden
}
func (pm *PresenceMeta) GetPersistence() bool {
return pm.Persistence
}
func (pm *PresenceMeta) GetUsername() string {
return pm.Username
}
func (pm *PresenceMeta) GetStatus() string {
return pm.Status
}
type Presence struct {
ID PresenceID
Stream PresenceStream
UserID uuid.UUID
Meta PresenceMeta
}
func (p *Presence) GetUserId() string {
return p.UserID.String()
}
func (p *Presence) GetSessionId() string {
return p.ID.SessionID.String()
}
func (p *Presence) GetNodeId() string {
return p.ID.Node
}
func (p *Presence) GetHidden() bool {
return p.Meta.Hidden
}
func (p *Presence) GetPersistence() bool {
return p.Meta.Persistence
}
func (p *Presence) GetUsername() string {
return p.Meta.Username
}
func (p *Presence) GetStatus() string {
return p.Meta.Status
}
type PresenceEvent struct {
Joins []Presence
Leaves []Presence
}
type Tracker interface {
SetMatchJoinListener(func(id uuid.UUID, joins []*MatchPresence))
SetMatchLeaveListener(func(id uuid.UUID, leaves []*MatchPresence))
Stop()
// Track returns success true/false, and new presence true/false.
Track(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID, meta PresenceMeta, allowIfFirstForSession bool) (bool, bool)
Untrack(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID)
UntrackAll(sessionID uuid.UUID)
// Update returns success true/false - will only fail if the user has no presence and allowIfFirstForSession is false, otherwise is an upsert.
Update(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID, meta PresenceMeta, allowIfFirstForSession bool) bool
// Remove all presences on a stream, effectively closing it.
UntrackByStream(stream PresenceStream)
// Remove all presences on a stream from the local node.
UntrackLocalByStream(stream PresenceStream)
// List the nodes that have at least one presence for the given stream.
ListNodesForStream(stream PresenceStream) map[string]struct{}
// Check if a stream exists (has any presences) or not.
StreamExists(stream PresenceStream) bool
// Get current total number of presences.
Count() int
// Get the number of presences in the given stream.
CountByStream(stream PresenceStream) int
// Get a snapshot of current presence counts for streams with one of the given stream modes.
CountByStreamModeFilter(modes map[uint8]*uint8) map[*PresenceStream]int32
// Check if a single presence on the current node exists.
GetLocalBySessionIDStreamUserID(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID) *PresenceMeta
// Check if a single presence on any node exists.
GetBySessionIDStreamUserID(node string, sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID) *PresenceMeta
// List presences by stream, optionally include hidden ones and not hidden ones.
ListByStream(stream PresenceStream, includeHidden bool, includeNotHidden bool) []*Presence
// Fast lookup of local session IDs to use for message delivery.
ListLocalSessionIDByStream(stream PresenceStream) []uuid.UUID
// Fast lookup of node + session IDs to use for message delivery.
ListPresenceIDByStream(stream PresenceStream) []*PresenceID
}
type presenceCompact struct {
ID PresenceID
Stream PresenceStream
UserID uuid.UUID
}
type LocalTracker struct {
sync.RWMutex
logger *zap.Logger
matchJoinListener func(id uuid.UUID, leaves []*MatchPresence)
matchLeaveListener func(id uuid.UUID, leaves []*MatchPresence)
sessionRegistry SessionRegistry
jsonpbMarshaler *jsonpb.Marshaler
name string
eventsCh chan *PresenceEvent
presencesByStream map[uint8]map[PresenceStream]map[presenceCompact]PresenceMeta
presencesBySession map[uuid.UUID]map[presenceCompact]PresenceMeta
ctx context.Context
ctxCancelFn context.CancelFunc
}
func StartLocalTracker(logger *zap.Logger, config Config, sessionRegistry SessionRegistry, jsonpbMarshaler *jsonpb.Marshaler) Tracker {
ctx, ctxCancelFn := context.WithCancel(context.Background())
t := &LocalTracker{
logger: logger,
sessionRegistry: sessionRegistry,
jsonpbMarshaler: jsonpbMarshaler,
name: config.GetName(),
eventsCh: make(chan *PresenceEvent, config.GetTracker().EventQueueSize),
presencesByStream: make(map[uint8]map[PresenceStream]map[presenceCompact]PresenceMeta),
presencesBySession: make(map[uuid.UUID]map[presenceCompact]PresenceMeta),
ctx: ctx,
ctxCancelFn: ctxCancelFn,
}
go func() {
// Asynchronously process and dispatch presence events.
for {
select {
case <-t.ctx.Done():
return
case e := <-t.eventsCh:
t.processEvent(e)
}
}
}()
return t
}
func (t *LocalTracker) SetMatchJoinListener(f func(id uuid.UUID, joins []*MatchPresence)) {
t.matchJoinListener = f
}
func (t *LocalTracker) SetMatchLeaveListener(f func(id uuid.UUID, leaves []*MatchPresence)) {
t.matchLeaveListener = f
}
func (t *LocalTracker) Stop() {
// No need to explicitly clean up the events channel, just let the application exit.
t.ctxCancelFn()
}
func (t *LocalTracker) Track(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID, meta PresenceMeta, allowIfFirstForSession bool) (bool, bool) {
pc := presenceCompact{ID: PresenceID{Node: t.name, SessionID: sessionID}, Stream: stream, UserID: userID}
t.Lock()
// See if this session has any presences tracked at all.
if bySession, anyTracked := t.presencesBySession[sessionID]; anyTracked {
// Then see if the exact presence we need is tracked.
if _, alreadyTracked := bySession[pc]; !alreadyTracked {
// If the current session had others tracked, but not this presence.
bySession[pc] = meta
} else {
t.Unlock()
return true, false
}
} else {
if !allowIfFirstForSession {
// If it's the first presence for this session, only allow it if explicitly permitted to.
t.Unlock()
return false, false
}
// If nothing at all was tracked for the current session, begin tracking.
bySession = make(map[presenceCompact]PresenceMeta)
bySession[pc] = meta
t.presencesBySession[sessionID] = bySession
}
// Update tracking for stream.
byStreamMode, ok := t.presencesByStream[stream.Mode]
if !ok {
byStreamMode = make(map[PresenceStream]map[presenceCompact]PresenceMeta)
t.presencesByStream[stream.Mode] = byStreamMode
}
if byStream, ok := byStreamMode[stream]; !ok {
byStream = make(map[presenceCompact]PresenceMeta)
byStream[pc] = meta
byStreamMode[stream] = byStream
} else {
byStream[pc] = meta
}
t.Unlock()
if !meta.Hidden {
t.queueEvent(
[]Presence{
Presence{ID: pc.ID, Stream: stream, UserID: userID, Meta: meta},
},
nil,
)
}
return true, true
}
func (t *LocalTracker) Untrack(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID) {
pc := presenceCompact{ID: PresenceID{Node: t.name, SessionID: sessionID}, Stream: stream, UserID: userID}
t.Lock()
bySession, anyTracked := t.presencesBySession[sessionID]
if !anyTracked {
// Nothing tracked for the session.
t.Unlock()
return
}
meta, found := bySession[pc]
if !found {
// The session had other presences, but not for this stream.
t.Unlock()
return
}
// Update the tracking for session.
if len(bySession) == 1 {
// This was the only presence for the session, discard the whole list.
delete(t.presencesBySession, sessionID)
} else {
// There were other presences for the session, drop just this one.
delete(bySession, pc)
}
// Update the tracking for stream.
if byStreamMode := t.presencesByStream[stream.Mode]; len(byStreamMode) == 1 {
// This is the only stream for this stream mode.
if byStream := byStreamMode[stream]; len(byStream) == 1 {
// This was the only presence in the only stream for this stream mode, discard the whole list.
delete(t.presencesByStream, stream.Mode)
} else {
// There were other presences for the stream, drop just this one.
delete(byStream, pc)
}
} else {
// There are other streams for this stream mode.
if byStream := byStreamMode[stream]; len(byStream) == 1 {
// This was the only presence for the stream, discard the whole list.
delete(byStreamMode, stream)
} else {
// There were other presences for the stream, drop just this one.
delete(byStream, pc)
}
}
t.Unlock()
if !meta.Hidden {
t.queueEvent(
nil,
[]Presence{
Presence{ID: pc.ID, Stream: stream, UserID: userID, Meta: meta},
},
)
}
}
func (t *LocalTracker) UntrackAll(sessionID uuid.UUID) {
t.Lock()
bySession, anyTracked := t.presencesBySession[sessionID]
if !anyTracked {
// Nothing tracked for the session.
t.Unlock()
return
}
leaves := make([]Presence, 0, len(bySession))
for pc, meta := range bySession {
// Update the tracking for stream.
if byStreamMode := t.presencesByStream[pc.Stream.Mode]; len(byStreamMode) == 1 {
// This is the only stream for this stream mode.
if byStream := byStreamMode[pc.Stream]; len(byStream) == 1 {
// This was the only presence in the only stream for this stream mode, discard the whole list.
delete(t.presencesByStream, pc.Stream.Mode)
} else {
// There were other presences for the stream, drop just this one.
delete(byStream, pc)
}
} else {
// There are other streams for this stream mode.
if byStream := byStreamMode[pc.Stream]; len(byStream) == 1 {
// This was the only presence for the stream, discard the whole list.
delete(byStreamMode, pc.Stream)
} else {
// There were other presences for the stream, drop just this one.
delete(byStream, pc)
}
}
// Check if there should be an event for this presence.
if !meta.Hidden {
leaves = append(leaves, Presence{ID: pc.ID, Stream: pc.Stream, UserID: pc.UserID, Meta: meta})
}
}
// Discard the tracking for session.
delete(t.presencesBySession, sessionID)
t.Unlock()
if len(leaves) != 0 {
t.queueEvent(
nil,
leaves,
)
}
}
func (t *LocalTracker) Update(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID, meta PresenceMeta, allowIfFirstForSession bool) bool {
pc := presenceCompact{ID: PresenceID{Node: t.name, SessionID: sessionID}, Stream: stream, UserID: userID}
t.Lock()
bySession, anyTracked := t.presencesBySession[sessionID]
if !anyTracked {
if !allowIfFirstForSession {
// Nothing tracked for the session and not allowed to track as first presence.
t.Unlock()
return false
}
bySession = make(map[presenceCompact]PresenceMeta)
t.presencesBySession[sessionID] = bySession
}
// Update tracking for session, but capture any previous meta in case a leave event is required.
previousMeta, alreadyTracked := bySession[pc]
bySession[pc] = meta
// Update tracking for stream.
byStreamMode, ok := t.presencesByStream[stream.Mode]
if !ok {
byStreamMode = make(map[PresenceStream]map[presenceCompact]PresenceMeta)
t.presencesByStream[stream.Mode] = byStreamMode
}
if byStream, ok := byStreamMode[stream]; !ok {
byStream = make(map[presenceCompact]PresenceMeta)
byStream[pc] = meta
byStreamMode[stream] = byStream
} else {
byStream[pc] = meta
}
t.Unlock()
if !meta.Hidden || (alreadyTracked && !previousMeta.Hidden) {
var joins []Presence
if !meta.Hidden {
joins = []Presence{
Presence{ID: pc.ID, Stream: stream, UserID: userID, Meta: meta},
}
}
var leaves []Presence
if alreadyTracked && !previousMeta.Hidden {
leaves = []Presence{
Presence{ID: pc.ID, Stream: stream, UserID: userID, Meta: previousMeta},
}
}
// Guaranteed joins and/or leaves are not empty or we wouldn't be inside this block.
t.queueEvent(
joins,
leaves,
)
}
return true
}
func (t *LocalTracker) UntrackLocalByStream(stream PresenceStream) {
// NOTE: Generates no presence notifications as everyone on the stream is going away all at once.
t.Lock()
byStream, anyTracked := t.presencesByStream[stream.Mode][stream]
if !anyTracked {
// Nothing tracked for the stream.
t.Unlock()
return
}
// Drop the presences from tracking for each session.
for pc, _ := range byStream {
if bySession := t.presencesBySession[pc.ID.SessionID]; len(bySession) == 1 {
// This is the only presence for that session, discard the whole list.
delete(t.presencesBySession, pc.ID.SessionID)
} else {
// There were other presences for the session, drop just this one.
delete(bySession, pc)
}
}
// Discard the tracking for stream.
if byStreamMode := t.presencesByStream[stream.Mode]; len(byStreamMode) == 1 {
// This is the only stream for this stream mode.
delete(t.presencesByStream, stream.Mode)
} else {
// There are other streams for this stream mode.
delete(byStreamMode, stream)
}
t.Unlock()
}
func (t *LocalTracker) UntrackByStream(stream PresenceStream) {
// NOTE: Generates no presence notifications as everyone on the stream is going away all at once.
t.Lock()
byStream, anyTracked := t.presencesByStream[stream.Mode][stream]
if !anyTracked {
// Nothing tracked for the stream.
t.Unlock()
return
}
// Drop the presences from tracking for each session.
for pc, _ := range byStream {
if bySession := t.presencesBySession[pc.ID.SessionID]; len(bySession) == 1 {
// This is the only presence for that session, discard the whole list.
delete(t.presencesBySession, pc.ID.SessionID)
} else {
// There were other presences for the session, drop just this one.
delete(bySession, pc)
}
}
// Discard the tracking for stream.
if byStreamMode := t.presencesByStream[stream.Mode]; len(byStreamMode) == 1 {
// This is the only stream for this stream mode.
delete(t.presencesByStream, stream.Mode)
} else {
// There are other streams for this stream mode.
delete(byStreamMode, stream)
}
t.Unlock()
}
func (t *LocalTracker) ListNodesForStream(stream PresenceStream) map[string]struct{} {
t.RLock()
_, anyTracked := t.presencesByStream[stream.Mode][stream]
t.RUnlock()
if anyTracked {
// For the local tracker having any presences for this stream is enough.
return map[string]struct{}{t.name: struct{}{}}
}
return map[string]struct{}{}
}
func (t *LocalTracker) StreamExists(stream PresenceStream) bool {
var exists bool
t.RLock()
exists = t.presencesByStream[stream.Mode][stream] != nil
t.RUnlock()
return exists
}
func (t *LocalTracker) Count() int {
var count int
t.RLock()
// For each session add together their presence count.
for _, bySession := range t.presencesBySession {
count += len(bySession)
}
t.RUnlock()
return count
}
func (t *LocalTracker) CountByStream(stream PresenceStream) int {
var count int
t.RLock()
// If the stream exists use its presence count, otherwise 0.
if byStream, anyTracked := t.presencesByStream[stream.Mode][stream]; anyTracked {
count = len(byStream)
}
t.RUnlock()
return count
}
func (t *LocalTracker) CountByStreamModeFilter(modes map[uint8]*uint8) map[*PresenceStream]int32 {
counts := make(map[*PresenceStream]int32)
t.RLock()
for mode, byStreamMode := range t.presencesByStream {
if modes[mode] == nil {
continue
}
for s, ps := range byStreamMode {
cs := s
counts[&cs] = int32(len(ps))
}
}
t.RUnlock()
return counts
}
func (t *LocalTracker) GetLocalBySessionIDStreamUserID(sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID) *PresenceMeta {
pc := presenceCompact{ID: PresenceID{Node: t.name, SessionID: sessionID}, Stream: stream, UserID: userID}
t.RLock()
bySession, anyTracked := t.presencesBySession[sessionID]
if !anyTracked {
// Nothing tracked for the session.
t.RUnlock()
return nil
}
meta, found := bySession[pc]
t.RUnlock()
if !found {
return nil
}
return &meta
}
func (t *LocalTracker) GetBySessionIDStreamUserID(node string, sessionID uuid.UUID, stream PresenceStream, userID uuid.UUID) *PresenceMeta {
pc := presenceCompact{ID: PresenceID{Node: node, SessionID: sessionID}, Stream: stream, UserID: userID}
t.RLock()
bySession, anyTracked := t.presencesBySession[sessionID]
if !anyTracked {
// Nothing tracked for the session.
t.RUnlock()
return nil
}
meta, found := bySession[pc]
t.RUnlock()
if !found {
return nil
}
return &meta
}
func (t *LocalTracker) ListByStream(stream PresenceStream, includeHidden bool, includeNotHidden bool) []*Presence {
t.RLock()
byStream, anyTracked := t.presencesByStream[stream.Mode][stream]
if !anyTracked {
t.RUnlock()
return []*Presence{}
}
ps := make([]*Presence, 0, len(byStream))
for pc, meta := range byStream {
if (meta.Hidden && includeHidden) || (!meta.Hidden && includeNotHidden) {
ps = append(ps, &Presence{ID: pc.ID, Stream: stream, UserID: pc.UserID, Meta: meta})
}
}
t.RUnlock()
return ps
}
func (t *LocalTracker) ListLocalSessionIDByStream(stream PresenceStream) []uuid.UUID {
t.RLock()
byStream, anyTracked := t.presencesByStream[stream.Mode][stream]
if !anyTracked {
t.RUnlock()
return []uuid.UUID{}
}
ps := make([]uuid.UUID, 0, len(byStream))
for pc, _ := range byStream {
ps = append(ps, pc.ID.SessionID)
}
t.RUnlock()
return ps
}
func (t *LocalTracker) ListPresenceIDByStream(stream PresenceStream) []*PresenceID {
t.RLock()
byStream, anyTracked := t.presencesByStream[stream.Mode][stream]
if !anyTracked {
t.RUnlock()
return []*PresenceID{}
}
ps := make([]*PresenceID, 0, len(byStream))
for pc, _ := range byStream {
pid := pc.ID
ps = append(ps, &pid)
}
t.RUnlock()
return ps
}
func (t *LocalTracker) queueEvent(joins, leaves []Presence) {
select {
case t.eventsCh <- &PresenceEvent{Joins: joins, Leaves: leaves}:
// Event queued for asynchronous dispatch.
default:
// Event queue is full, log an error and completely drain the queue.
t.logger.Error("Presence event dispatch queue is full, presence events may be lost")
for {
select {
case <-t.eventsCh:
// Discard the event.
default:
// Queue is now empty.
return
}
}
}
}
func (t *LocalTracker) processEvent(e *PresenceEvent) {
t.logger.Debug("Processing presence event", zap.Int("joins", len(e.Joins)), zap.Int("leaves", len(e.Leaves)))
// Group joins/leaves by stream to allow batching.
// Convert to wire representation at the same time.
streamJoins := make(map[PresenceStream][]*rtapi.UserPresence, 0)
streamLeaves := make(map[PresenceStream][]*rtapi.UserPresence, 0)
// Track grouped authoritative match joins and leaves separately from client-bound events.
matchJoins := make(map[uuid.UUID][]*MatchPresence, 0)
matchLeaves := make(map[uuid.UUID][]*MatchPresence, 0)
for _, p := range e.Joins {
pWire := &rtapi.UserPresence{
UserId: p.UserID.String(),
SessionId: p.ID.SessionID.String(),
Username: p.Meta.Username,
Persistence: p.Meta.Persistence,
}
if p.Stream.Mode == StreamModeStatus {
// Status field is only populated for status stream presences.
pWire.Status = &wrappers.StringValue{Value: p.Meta.Status}
}
if j, ok := streamJoins[p.Stream]; ok {
streamJoins[p.Stream] = append(j, pWire)
} else {
streamJoins[p.Stream] = []*rtapi.UserPresence{pWire}
}
// We only care about authoritative match joins where the match host is the current node.
if p.Stream.Mode == StreamModeMatchAuthoritative && p.Stream.Label == t.name {
mp := &MatchPresence{
Node: p.ID.Node,
UserID: p.UserID,
SessionID: p.ID.SessionID,
Username: p.Meta.Username,
}
if j, ok := matchJoins[p.Stream.Subject]; ok {
matchJoins[p.Stream.Subject] = append(j, mp)
} else {
matchJoins[p.Stream.Subject] = []*MatchPresence{mp}
}
}
}
for _, p := range e.Leaves {
pWire := &rtapi.UserPresence{
UserId: p.UserID.String(),
SessionId: p.ID.SessionID.String(),
Username: p.Meta.Username,
Persistence: p.Meta.Persistence,
}
if p.Stream.Mode == StreamModeStatus {
// Status field is only populated for status stream presences.
pWire.Status = &wrappers.StringValue{Value: p.Meta.Status}
}
if l, ok := streamLeaves[p.Stream]; ok {
streamLeaves[p.Stream] = append(l, pWire)
} else {
streamLeaves[p.Stream] = []*rtapi.UserPresence{pWire}
}
// We only care about authoritative match leaves where the match host is the current node.
if p.Stream.Mode == StreamModeMatchAuthoritative && p.Stream.Label == t.name {
mp := &MatchPresence{
Node: p.ID.Node,
UserID: p.UserID,
SessionID: p.ID.SessionID,
Username: p.Meta.Username,
}
if l, ok := matchLeaves[p.Stream.Subject]; ok {
matchLeaves[p.Stream.Subject] = append(l, mp)
} else {
matchLeaves[p.Stream.Subject] = []*MatchPresence{mp}
}
}
}
// Notify locally hosted authoritative matches of join and leave events.
for matchID, joins := range matchJoins {
t.matchJoinListener(matchID, joins)
}
for matchID, leaves := range matchLeaves {
t.matchLeaveListener(matchID, leaves)
}
// Send joins, together with any leaves for the same stream.
for stream, joins := range streamJoins {
leaves, ok := streamLeaves[stream]
if ok {
delete(streamLeaves, stream)
}
// Construct the wire representation of the stream.
streamWire := &rtapi.Stream{
Mode: int32(stream.Mode),
Label: stream.Label,
}
if stream.Subject != uuid.Nil {
streamWire.Subject = stream.Subject.String()
}
if stream.Subcontext != uuid.Nil {
streamWire.Subcontext = stream.Subcontext.String()
}
// Find the list of event recipients first so we can skip event encoding work if it's not necessary.
sessionIDs := t.ListLocalSessionIDByStream(stream)
if len(sessionIDs) == 0 {
continue
}
// Construct the wire representation of the event based on the stream mode.
var envelope *rtapi.Envelope
switch stream.Mode {
case StreamModeStatus:
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_StatusPresenceEvent{StatusPresenceEvent: &rtapi.StatusPresenceEvent{
Joins: joins,
Leaves: leaves,
}}}
case StreamModeChannel:
fallthrough
case StreamModeGroup:
fallthrough
case StreamModeDM:
channelId, err := StreamToChannelId(stream)
if err != nil {
// Should not happen thanks to previous validation, but guard just in case.
t.logger.Error("Error converting stream to channel identifier in presence event", zap.Error(err), zap.Any("stream", stream))
continue
}
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_ChannelPresenceEvent{ChannelPresenceEvent: &rtapi.ChannelPresenceEvent{
ChannelId: channelId,
Joins: joins,
Leaves: leaves,
}}}
case StreamModeMatchRelayed:
fallthrough
case StreamModeMatchAuthoritative:
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_MatchPresenceEvent{MatchPresenceEvent: &rtapi.MatchPresenceEvent{
MatchId: fmt.Sprintf("%v.%v", stream.Subject.String(), stream.Label),
Joins: joins,
Leaves: leaves,
}}}
default:
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_StreamPresenceEvent{StreamPresenceEvent: &rtapi.StreamPresenceEvent{
Stream: streamWire,
Joins: joins,
Leaves: leaves,
}}}
}
// Prepare payload variables but do not initialize until we hit a session that needs them to avoid unnecessary work.
var payloadProtobuf []byte
var payloadJson []byte
// Deliver event.
for _, sessionID := range sessionIDs {
session := t.sessionRegistry.Get(sessionID)
if session == nil {
t.logger.Debug("Could not deliver presence event, no session", zap.String("sid", sessionID.String()))
continue
}
var err error
switch session.Format() {
case SessionFormatProtobuf:
if payloadProtobuf == nil {
// Marshal the payload now that we know this format is needed.
payloadProtobuf, err = proto.Marshal(envelope)
if err != nil {
t.logger.Error("Could not marshal presence event", zap.Error(err))
return
}
}
err = session.SendBytes(true, stream.Mode, payloadProtobuf)
case SessionFormatJson:
fallthrough
default:
if payloadJson == nil {
// Marshal the payload now that we know this format is needed.
var buf bytes.Buffer
if err = t.jsonpbMarshaler.Marshal(&buf, envelope); err == nil {
payloadJson = buf.Bytes()
} else {
t.logger.Error("Could not marshal presence event", zap.Error(err))
return
}
}
err = session.SendBytes(true, stream.Mode, payloadJson)
}
if err != nil {
t.logger.Error("Failed to deliver presence event", zap.String("sid", sessionID.String()), zap.Error(err))
}
}
}
// If there are leaves without corresponding joins.
for stream, leaves := range streamLeaves {
// Construct the wire representation of the stream.
streamWire := &rtapi.Stream{
Mode: int32(stream.Mode),
Label: stream.Label,
}
if stream.Subject != uuid.Nil {
streamWire.Subject = stream.Subject.String()
}
if stream.Subcontext != uuid.Nil {
streamWire.Subcontext = stream.Subcontext.String()
}
// Find the list of event recipients first so we can skip event encoding work if it's not necessary.
sessionIDs := t.ListLocalSessionIDByStream(stream)
if len(sessionIDs) == 0 {
continue
}
// Construct the wire representation of the event based on the stream mode.
var envelope *rtapi.Envelope
switch stream.Mode {
case StreamModeStatus:
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_StatusPresenceEvent{StatusPresenceEvent: &rtapi.StatusPresenceEvent{
// No joins.
Leaves: leaves,
}}}
case StreamModeChannel:
fallthrough
case StreamModeGroup:
fallthrough
case StreamModeDM:
channelId, err := StreamToChannelId(stream)
if err != nil {
// Should not happen thanks to previous validation, but guard just in case.
t.logger.Error("Error converting stream to channel identifier in presence event", zap.Error(err), zap.Any("stream", stream))
continue
}
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_ChannelPresenceEvent{ChannelPresenceEvent: &rtapi.ChannelPresenceEvent{
ChannelId: channelId,
// No joins.
Leaves: leaves,
}}}
case StreamModeMatchRelayed:
fallthrough
case StreamModeMatchAuthoritative:
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_MatchPresenceEvent{MatchPresenceEvent: &rtapi.MatchPresenceEvent{
MatchId: fmt.Sprintf("%v.%v", stream.Subject.String(), stream.Label),
// No joins.
Leaves: leaves,
}}}
default:
envelope = &rtapi.Envelope{Message: &rtapi.Envelope_StreamPresenceEvent{StreamPresenceEvent: &rtapi.StreamPresenceEvent{
Stream: streamWire,
// No joins.
Leaves: leaves,
}}}
}
// Prepare payload variables but do not initialize until we hit a session that needs them to avoid unnecessary work.
var payloadProtobuf []byte
var payloadJson []byte
// Deliver event.
for _, sessionID := range sessionIDs {
session := t.sessionRegistry.Get(sessionID)
if session == nil {
t.logger.Debug("Could not deliver presence event, no session", zap.String("sid", sessionID.String()))
continue
}
var err error
switch session.Format() {
case SessionFormatProtobuf:
if payloadProtobuf == nil {
// Marshal the payload now that we know this format is needed.
payloadProtobuf, err = proto.Marshal(envelope)
if err != nil {
t.logger.Error("Could not marshal presence event", zap.Error(err))
return
}
}
err = session.SendBytes(true, stream.Mode, payloadProtobuf)
case SessionFormatJson:
fallthrough
default:
if payloadJson == nil {
// Marshal the payload now that we know this format is needed.
var buf bytes.Buffer
if err = t.jsonpbMarshaler.Marshal(&buf, envelope); err == nil {
payloadJson = buf.Bytes()
} else {
t.logger.Error("Could not marshal presence event", zap.Error(err))
return
}
}
err = session.SendBytes(true, stream.Mode, payloadJson)
}
if err != nil {
t.logger.Error("Failed to deliver presence event", zap.String("sid", sessionID.String()), zap.Error(err))
}
}
}
}