package main import ( "sync" "git.samanthony.xyz/buth/back/auth" "git.samanthony.xyz/buth/back/qver" ) type SessionsDir struct { *qver.Version mu sync.Mutex // guards below sessions map[auth.SessId]*Session } func NewSessionsDir(rootVer *qver.Version) *SessionsDir { return &SessionsDir{ qver.New(qver.Parent(rootVer)), sync.Mutex{}, make(map[auth.SessId]*Session), } } func (dir *SessionsDir) Close() { dir.mu.Lock() // Don't unlock dir.Version.Close() for id, sess := range dir.sessions { sess.Close() delete(dir.sessions, id) } } // Get returns the session with the given ID if it exists, or false if it doesn't. func (dir *SessionsDir) Get(id auth.SessId) (*Session, bool) { dir.mu.Lock() defer dir.mu.Unlock() sess, ok := dir.sessions[id] return sess, ok } // Owner returns the owner of the given session if it exists, or false if it doesn't. func (dir *SessionsDir) Owner(id auth.SessId) (auth.Uname, bool) { if sess, ok := dir.Get(id); ok { return sess.Uname, ok } return "", false } func (dir *SessionsDir) Kill(id auth.SessId) { dir.mu.Lock() defer dir.mu.Unlock() if sess, ok := dir.sessions[id]; ok { sess.Close() delete(dir.sessions, id) dir.Version.Bump() } }