forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_registry.go
93 lines (82 loc) · 2.7 KB
/
session_registry.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
// Copyright 2017 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/gorilla/websocket"
"github.com/satori/go.uuid"
"go.uber.org/zap"
)
// SessionRegistry maintains a list of sessions to their IDs. This is thread-safe.
type SessionRegistry struct {
sync.RWMutex
logger *zap.Logger
config Config
tracker Tracker
matchmaker Matchmaker
sessions map[uuid.UUID]*session
}
// NewSessionRegistry creates a new SessionRegistry
func NewSessionRegistry(logger *zap.Logger, config Config, tracker Tracker, matchmaker Matchmaker) *SessionRegistry {
return &SessionRegistry{
logger: logger,
config: config,
tracker: tracker,
matchmaker: matchmaker,
sessions: make(map[uuid.UUID]*session),
}
}
func (a *SessionRegistry) stop() {
a.Lock()
for _, session := range a.sessions {
if a.sessions[session.id] != nil {
delete(a.sessions, session.id)
go func() {
a.matchmaker.RemoveAll(session.id) // Drop all active matchmaking requests for this session.
a.tracker.UntrackAll(session.id) // Drop all tracked presences for this session.
}()
}
session.close()
}
a.Unlock()
}
// Get returns a session matching the sessionID
func (a *SessionRegistry) Get(sessionID uuid.UUID) *session {
var s *session
a.RLock()
s = a.sessions[sessionID]
a.RUnlock()
return s
}
func (a *SessionRegistry) add(userID uuid.UUID, handle string, lang string, expiry int64, conn *websocket.Conn, processRequest func(logger *zap.Logger, session *session, envelope *Envelope)) {
s := NewSession(a.logger, a.config, userID, handle, lang, expiry, conn, a.remove)
a.Lock()
a.sessions[s.id] = s
a.Unlock()
// Register the session for notifications.
a.tracker.Track(s.id, "notifications", s.userID, PresenceMeta{Handle: handle})
// Allow the server to begin processing incoming messages from this session.
s.Consume(processRequest)
}
func (a *SessionRegistry) remove(c *session) {
a.Lock()
if a.sessions[c.id] != nil {
delete(a.sessions, c.id)
go func() {
a.matchmaker.RemoveAll(c.id) // Drop all active matchmaking requests for this session.
a.tracker.UntrackAll(c.id) // Drop all tracked presences for this session.
}()
}
a.Unlock()
}