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.
nnet/session/session_mgr.go

47 lines
643 B
Go

package session
import (
"sync"
)
type Manager struct {
sync.RWMutex
sessions map[int64]*Session
}
func NewSessionMgr() *Manager {
return &Manager{
RWMutex: sync.RWMutex{},
sessions: make(map[int64]*Session),
}
}
func (m *Manager) StoreSession(s *Session) {
m.Lock()
defer m.Unlock()
m.sessions[s.ID()] = s
}
func (m *Manager) FindSession(sid int64) *Session {
m.RLock()
defer m.RUnlock()
return m.sessions[sid]
}
func (m *Manager) FindOrCreateSession(sid int64) *Session {
m.RLock()
s, ok := m.sessions[sid]
m.RUnlock()
if !ok {
s = NewSession()
m.Lock()
m.sessions[s.ID()] = s
m.Unlock()
}
return s
}