You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
701 B
Go
48 lines
701 B
Go
2 years ago
|
package session
|
||
|
|
||
|
import (
|
||
|
"git.noahlan.cn/northlan/nnet/nface"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type Manager struct {
|
||
|
sync.RWMutex
|
||
|
sessions map[int64]nface.ISession
|
||
|
}
|
||
|
|
||
|
func NewManager() *Manager {
|
||
|
return &Manager{
|
||
|
RWMutex: sync.RWMutex{},
|
||
|
sessions: make(map[int64]nface.ISession),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *Manager) storeSession(s nface.ISession) {
|
||
|
m.Lock()
|
||
|
defer m.Unlock()
|
||
|
|
||
|
m.sessions[s.ID()] = s
|
||
|
}
|
||
|
|
||
|
func (m *Manager) findSession(sid int64) nface.ISession {
|
||
|
m.RLock()
|
||
|
defer m.RUnlock()
|
||
|
|
||
|
return m.sessions[sid]
|
||
|
}
|
||
|
|
||
|
func (m *Manager) findOrCreateSession(sid int64) nface.ISession {
|
||
|
m.RLock()
|
||
|
s, ok := m.sessions[sid]
|
||
|
m.RUnlock()
|
||
|
|
||
|
if !ok {
|
||
|
s = New()
|
||
|
|
||
|
m.Lock()
|
||
|
m.sessions[s.ID()] = s
|
||
|
m.Unlock()
|
||
|
}
|
||
|
return s
|
||
|
}
|