-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
matchmaker.go
251 lines (219 loc) · 6.79 KB
/
matchmaker.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
// 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 (
"sync"
"github.com/blevesearch/bleve/analysis/analyzer/keyword"
"github.com/blevesearch/bleve"
"github.com/gofrs/uuid"
"github.com/heroiclabs/nakama/runtime"
"github.com/pkg/errors"
"go.uber.org/zap"
)
var ErrMatchmakerTicketNotFound = errors.New("ticket not found")
type MatchmakerPresence struct {
UserId string `json:"user_id"`
SessionId string `json:"session_id"`
Username string `json:"username"`
Node string `json:"node"`
}
func (p *MatchmakerPresence) GetUserId() string {
return p.UserId
}
func (p *MatchmakerPresence) GetSessionId() string {
return p.SessionId
}
func (p *MatchmakerPresence) GetNodeId() string {
return p.Node
}
func (p *MatchmakerPresence) GetHidden() bool {
return false
}
func (p *MatchmakerPresence) GetPersistence() bool {
return false
}
func (p *MatchmakerPresence) GetUsername() string {
return p.Username
}
func (p *MatchmakerPresence) GetStatus() string {
return ""
}
type MatchmakerEntry struct {
Ticket string `json:"ticket"`
Presence *MatchmakerPresence `json:"presence"`
Properties map[string]interface{} `json:"properties"`
// Cached for when we need them returned to clients, but not indexed.
StringProperties map[string]string `json:"-"`
NumericProperties map[string]float64 `json:"-"`
SessionID uuid.UUID `json:"-"`
}
func (m *MatchmakerEntry) GetPresence() runtime.Presence {
return m.Presence
}
func (m *MatchmakerEntry) GetTicket() string {
return m.Ticket
}
func (m *MatchmakerEntry) GetProperties() map[string]interface{} {
return m.Properties
}
type Matchmaker interface {
Add(session Session, query string, minCount int, maxCount int, stringProperties map[string]string, numericProperties map[string]float64) (string, []*MatchmakerEntry, error)
Remove(sessionID uuid.UUID, ticket string) error
RemoveAll(sessionID uuid.UUID) error
}
type LocalMatchmaker struct {
sync.Mutex
node string
entries map[string]*MatchmakerEntry
index bleve.Index
}
func NewLocalMatchmaker(startupLogger *zap.Logger, node string) Matchmaker {
mapping := bleve.NewIndexMapping()
mapping.DefaultAnalyzer = keyword.Name
index, err := bleve.NewMemOnly(mapping)
if err != nil {
startupLogger.Fatal("Failed to create matchmaker index", zap.Error(err))
}
return &LocalMatchmaker{
node: node,
entries: make(map[string]*MatchmakerEntry),
index: index,
}
}
func (m *LocalMatchmaker) Add(session Session, query string, minCount int, maxCount int, stringProperties map[string]string, numericProperties map[string]float64) (string, []*MatchmakerEntry, error) {
// Merge incoming properties.
properties := make(map[string]interface{}, len(stringProperties)+len(numericProperties))
for k, v := range stringProperties {
properties[k] = v
}
for k, v := range numericProperties {
properties[k] = v
}
indexQuery := bleve.NewQueryStringQuery(query)
search := bleve.NewSearchRequestOptions(indexQuery, maxCount-1, 0, false)
ticket := uuid.Must(uuid.NewV4()).String()
entry := &MatchmakerEntry{
Ticket: ticket,
Presence: &MatchmakerPresence{
UserId: session.UserID().String(),
SessionId: session.ID().String(),
Username: session.Username(),
Node: m.node,
},
Properties: properties,
StringProperties: stringProperties,
NumericProperties: numericProperties,
SessionID: session.ID(),
}
m.Lock()
result, err := m.index.SearchInContext(session.Context(), search)
if err != nil {
m.Unlock()
return ticket, nil, err
}
// Check if we have enough results to return them, or if we just add a new entry to the matchmaker.
resultCount := result.Hits.Len()
if resultCount < minCount-1 {
if err := m.index.Index(ticket, entry); err != nil {
m.Unlock()
return ticket, nil, err
}
m.entries[ticket] = entry
m.Unlock()
return ticket, nil, nil
}
// We have enough entries to satisfy the request.
entries := make([]*MatchmakerEntry, 0, resultCount+1)
tickets := make([]string, 0, resultCount)
batch := m.index.NewBatch()
for _, hit := range result.Hits {
entry, ok := m.entries[hit.ID]
if !ok {
// Index and entries map are out of sync, should not happen but check to be sure.
m.Unlock()
return ticket, nil, ErrMatchmakerTicketNotFound
}
entries = append(entries, entry)
tickets = append(tickets, hit.ID)
batch.Delete(hit.ID)
}
// Only remove the entries after we've processed each one to make sure
// there were no sync issues between the index and the entries map.
if err := m.index.Batch(batch); err != nil {
m.Unlock()
return ticket, nil, err
}
for _, ticket := range tickets {
delete(m.entries, ticket)
}
m.Unlock()
// Add the current user.
entries = append(entries, entry)
return ticket, entries, nil
}
func (m *LocalMatchmaker) Remove(sessionID uuid.UUID, ticket string) error {
m.Lock()
if entry, ok := m.entries[ticket]; !ok || entry.Presence.SessionId != sessionID.String() {
// Ticket does not exist or does not belong to this session.
m.Unlock()
return ErrMatchmakerTicketNotFound
}
if err := m.index.Delete(ticket); err != nil {
m.Unlock()
return err
}
delete(m.entries, ticket)
m.Unlock()
return nil
}
func (m *LocalMatchmaker) RemoveAll(sessionID uuid.UUID) error {
query := bleve.NewMatchQuery(sessionID.String())
query.SetField("presence.session_id")
queuedRemoves := 0
batch := m.index.NewBatch()
tickets := make([]string, 0, 10)
m.Lock()
// Look up and accumulate all required removes to be executed as a batch later.
for {
// Load a set of matchmaker entries for the given session.
search := bleve.NewSearchRequestOptions(query, 10, queuedRemoves, false)
result, err := m.index.Search(search)
if err != nil {
m.Unlock()
return err
}
// Queue each hit up to be removed.
for _, hit := range result.Hits {
batch.Delete(hit.ID)
tickets = append(tickets, hit.ID)
queuedRemoves++
}
// Check if we've accumulated all available hits.
if result.Hits.Len() == 0 || uint64(queuedRemoves) >= result.Total {
break
}
}
// Execute the batch and delete from the entries map, if any removes are present.
if queuedRemoves > 0 {
if err := m.index.Batch(batch); err != nil {
m.Unlock()
return err
}
for _, ticket := range tickets {
delete(m.entries, ticket)
}
}
m.Unlock()
return nil
}