forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_handler.go
338 lines (293 loc) · 7.99 KB
/
match_handler.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
// 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 (
"context"
"fmt"
"time"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"go.uber.org/atomic"
"go.uber.org/zap"
)
type MatchDataMessage struct {
UserID uuid.UUID
SessionID uuid.UUID
Username string
Node string
OpCode int64
Data []byte
ReceiveTime int64
}
func (m *MatchDataMessage) GetUserId() string {
return m.UserID.String()
}
func (m *MatchDataMessage) GetSessionId() string {
return m.SessionID.String()
}
func (m *MatchDataMessage) GetNodeId() string {
return m.Node
}
func (m *MatchDataMessage) GetHidden() bool {
return false
}
func (m *MatchDataMessage) GetPersistence() bool {
return false
}
func (m *MatchDataMessage) GetUsername() string {
return m.Username
}
func (m *MatchDataMessage) GetStatus() string {
return ""
}
func (m *MatchDataMessage) GetOpCode() int64 {
return m.OpCode
}
func (m *MatchDataMessage) GetData() []byte {
return m.Data
}
func (m *MatchDataMessage) GetReceiveTime() int64 {
return m.ReceiveTime
}
type MatchHandler struct {
logger *zap.Logger
matchRegistry MatchRegistry
tracker Tracker
router MessageRouter
core RuntimeMatchCore
// Identification not (directly) controlled by match init.
ID uuid.UUID
Node string
IDStr string
Stream PresenceStream
// Internal state.
tick int64
// Control elements.
inputCh chan *MatchDataMessage
ticker *time.Ticker
callCh chan func(*MatchHandler)
stopCh chan struct{}
stopped *atomic.Bool
// Configuration set by match init.
Label *atomic.String
Rate int
// Match state.
state interface{}
}
func NewMatchHandler(logger *zap.Logger, config Config, matchRegistry MatchRegistry, core RuntimeMatchCore, label *atomic.String, id uuid.UUID, node string, params map[string]interface{}) (*MatchHandler, error) {
state, rateInt, labelStr, err := core.MatchInit(params)
if err != nil {
core.Cancel()
return nil, err
}
if state == nil {
core.Cancel()
return nil, errors.New("Match initial state must not be nil")
}
err = matchRegistry.UpdateMatchLabel(id, labelStr)
if err != nil {
return nil, err
}
label.Store(labelStr)
// Construct the match.
mh := &MatchHandler{
logger: logger,
matchRegistry: matchRegistry,
core: core,
ID: id,
Node: node,
IDStr: fmt.Sprintf("%v.%v", id.String(), node),
Stream: PresenceStream{
Mode: StreamModeMatchAuthoritative,
Subject: id,
Label: node,
},
tick: 0,
inputCh: make(chan *MatchDataMessage, config.GetMatch().InputQueueSize),
// Ticker below.
callCh: make(chan func(mh *MatchHandler), config.GetMatch().CallQueueSize),
stopCh: make(chan struct{}),
stopped: atomic.NewBool(false),
Label: label,
Rate: rateInt,
state: state,
}
// Set up the ticker that governs the match loop.
mh.ticker = time.NewTicker(time.Second / time.Duration(mh.Rate))
// Continuously run queued actions until the match stops.
go func() {
for {
select {
case <-mh.stopCh:
// Match has been stopped.
return
case <-mh.ticker.C:
// Tick, queue a match loop invocation.
if !mh.QueueCall(loop) {
return
}
case call := <-mh.callCh:
// An invocation to one of the match functions.
call(mh)
}
}
}()
mh.logger.Info("Match started")
return mh, nil
}
// Used when an internal match process (or error) requires it to stop.
func (mh *MatchHandler) Stop() {
mh.Close()
mh.matchRegistry.RemoveMatch(mh.ID, mh.Stream)
}
// Used when the match is closed externally.
func (mh *MatchHandler) Close() {
if !mh.stopped.CAS(false, true) {
return
}
mh.core.Cancel()
close(mh.stopCh)
mh.ticker.Stop()
}
func (mh *MatchHandler) QueueCall(f func(*MatchHandler)) bool {
select {
case mh.callCh <- f:
return true
default:
// Match call queue is full, the handler isn't processing fast enough.
mh.logger.Warn("Match handler call processing too slow, closing match")
mh.Stop()
return false
}
}
func (mh *MatchHandler) QueueData(m *MatchDataMessage) {
select {
case mh.inputCh <- m:
return
default:
// Match input queue is full, the handler isn't processing fast enough or there's too much incoming data.
mh.logger.Warn("Match handler data processing too slow, dropping data message")
return
}
}
func loop(mh *MatchHandler) {
if mh.stopped.Load() {
return
}
state, err := mh.core.MatchLoop(mh.tick, mh.state, mh.inputCh)
if err != nil {
mh.Stop()
mh.logger.Warn("Stopping match after error from match_loop execution", zap.Int64("tick", mh.tick), zap.Error(err))
return
}
if state == nil {
mh.Stop()
mh.logger.Info("Match loop returned nil or no state, stopping match")
return
}
mh.state = state
mh.tick++
}
func JoinAttempt(ctx context.Context, resultCh chan *MatchJoinResult, userID, sessionID uuid.UUID, username, node string, metadata map[string]string) func(mh *MatchHandler) {
return func(mh *MatchHandler) {
select {
case <-ctx.Done():
// Do not process the match join attempt through the match handler if the client has gone away between
// when this call was inserted into the match call queue and when it's due for processing.
resultCh <- &MatchJoinResult{Allow: false}
return
default:
}
if mh.stopped.Load() {
resultCh <- &MatchJoinResult{Allow: false}
return
}
state, allow, reason, err := mh.core.MatchJoinAttempt(mh.tick, mh.state, userID, sessionID, username, node, metadata)
if err != nil {
mh.Stop()
mh.logger.Warn("Stopping match after error from match_join_attempt execution", zap.Int64("tick", mh.tick), zap.Error(err))
resultCh <- &MatchJoinResult{Allow: false}
return
}
if state == nil {
mh.Stop()
mh.logger.Info("Match join attempt returned nil or no state, stopping match")
resultCh <- &MatchJoinResult{Allow: false}
return
}
mh.state = state
resultCh <- &MatchJoinResult{Allow: allow, Reason: reason, Label: mh.Label.Load()}
}
}
func Join(joins []*MatchPresence) func(mh *MatchHandler) {
return func(mh *MatchHandler) {
if mh.stopped.Load() {
return
}
state, err := mh.core.MatchJoin(mh.tick, mh.state, joins)
if err != nil {
mh.Stop()
mh.logger.Warn("Stopping match after error from match_join execution", zap.Int64("tick", mh.tick), zap.Error(err))
return
}
if state == nil {
mh.Stop()
mh.logger.Info("Match join returned nil or no state, stopping match")
return
}
mh.state = state
}
}
func Leave(leaves []*MatchPresence) func(mh *MatchHandler) {
return func(mh *MatchHandler) {
if mh.stopped.Load() {
return
}
state, err := mh.core.MatchLeave(mh.tick, mh.state, leaves)
if err != nil {
mh.Stop()
mh.logger.Warn("Stopping match after error from match_leave execution", zap.Int("tick", int(mh.tick)), zap.Error(err))
return
}
if state == nil {
mh.Stop()
mh.logger.Info("Match leave returned nil or no state, stopping match")
return
}
mh.state = state
}
}
func Terminate(graceSeconds int) func(mh *MatchHandler) {
return func(mh *MatchHandler) {
if mh.stopped.Load() {
return
}
state, err := mh.core.MatchTerminate(mh.tick, mh.state, graceSeconds)
if err != nil {
mh.Stop()
mh.logger.Warn("Stopping match after error from match_terminate execution", zap.Int("tick", int(mh.tick)), zap.Error(err))
return
}
if state == nil {
mh.Stop()
mh.logger.Info("Match terminate returned nil or no state, stopping match")
return
}
mh.state = state
// If grace period is 0 end the match immediately after the callback returns.
if graceSeconds == 0 {
mh.Stop()
}
}
}