-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
match_presence.go
185 lines (168 loc) · 4.59 KB
/
match_presence.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
// 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 (
"github.com/gofrs/uuid"
"go.uber.org/atomic"
"sync"
)
// Represents routing and identify information for a single match participant.
type MatchPresence struct {
Node string
UserID uuid.UUID
SessionID uuid.UUID
Username string
}
func (p *MatchPresence) GetUserId() string {
return p.UserID.String()
}
func (p *MatchPresence) GetSessionId() string {
return p.SessionID.String()
}
func (p *MatchPresence) GetNodeId() string {
return p.Node
}
func (p *MatchPresence) GetHidden() bool {
return false
}
func (p *MatchPresence) GetPersistence() bool {
return false
}
func (p *MatchPresence) GetUsername() string {
return p.Username
}
func (p *MatchPresence) GetStatus() string {
return ""
}
// Used to monitor when match presences begin and complete their match join process.
type MatchJoinMarker struct {
presence *MatchPresence
expiryTick int64
}
type MatchJoinMarkerList struct {
sync.RWMutex
expiryDelayMs int64
tickRate int64
joinMarkers map[uuid.UUID]*MatchJoinMarker
}
func NewMatchJoinMarkerList(config Config, tickRate int64) *MatchJoinMarkerList {
return &MatchJoinMarkerList{
expiryDelayMs: int64(config.GetMatch().JoinMarkerDeadlineMs),
tickRate: tickRate,
joinMarkers: make(map[uuid.UUID]*MatchJoinMarker),
}
}
func (m *MatchJoinMarkerList) Add(presence *MatchPresence, currentTick int64) {
m.Lock()
m.joinMarkers[presence.SessionID] = &MatchJoinMarker{
presence: presence,
expiryTick: currentTick + (m.tickRate * (m.expiryDelayMs / 1000)),
}
m.Unlock()
}
func (m *MatchJoinMarkerList) Mark(sessionID uuid.UUID) {
m.Lock()
delete(m.joinMarkers, sessionID)
m.Unlock()
}
func (m *MatchJoinMarkerList) ClearExpired(tick int64) []*MatchPresence {
presences := make([]*MatchPresence, 0)
m.Lock()
for sessionID, joinMarker := range m.joinMarkers {
if joinMarker.expiryTick <= tick {
presences = append(presences, joinMarker.presence)
delete(m.joinMarkers, sessionID)
}
}
m.Unlock()
return presences
}
// Maintains the match presences for routing and validation purposes.
type MatchPresenceList struct {
sync.RWMutex
size *atomic.Int32
presences []*PresenceID
presenceMap map[uuid.UUID]struct{}
}
func NewMatchPresenceList() *MatchPresenceList {
return &MatchPresenceList{
size: atomic.NewInt32(0),
presences: make([]*PresenceID, 0, 10),
presenceMap: make(map[uuid.UUID]struct{}, 10),
}
}
func (m *MatchPresenceList) Join(joins []*MatchPresence) []*MatchPresence {
processed := make([]*MatchPresence, 0, len(joins))
m.Lock()
for _, join := range joins {
if _, ok := m.presenceMap[join.SessionID]; !ok {
m.presences = append(m.presences, &PresenceID{
Node: join.Node,
SessionID: join.SessionID,
})
m.presenceMap[join.SessionID] = struct{}{}
processed = append(processed, join)
}
}
m.Unlock()
if l := len(processed); l != 0 {
m.size.Add(int32(l))
}
return processed
}
func (m *MatchPresenceList) Leave(leaves []*MatchPresence) []*MatchPresence {
processed := make([]*MatchPresence, 0, len(leaves))
m.Lock()
for _, leave := range leaves {
if _, ok := m.presenceMap[leave.SessionID]; ok {
for i, presenceID := range m.presences {
if presenceID.SessionID == leave.SessionID && presenceID.Node == leave.Node {
m.presences = append(m.presences[:i], m.presences[i+1:]...)
break
}
}
delete(m.presenceMap, leave.SessionID)
processed = append(processed, leave)
}
}
m.Unlock()
if l := len(processed); l != 0 {
m.size.Sub(int32(l))
}
return processed
}
func (m *MatchPresenceList) Contains(presence *PresenceID) bool {
var found bool
m.RLock()
for _, p := range m.presences {
if p.SessionID == presence.SessionID && p.Node == p.Node {
found = true
break
}
}
m.RUnlock()
return found
}
func (m *MatchPresenceList) List() []*PresenceID {
m.RLock()
list := make([]*PresenceID, 0, len(m.presences))
for _, presence := range m.presences {
list = append(list, presence)
}
m.RUnlock()
return list
}
func (m *MatchPresenceList) Size() int {
return int(m.size.Load())
}