package main import ( "sync/atomic" "time" "git.samanthony.xyz/buth/back/auth" ) type Session struct { auth.SessId auth.Uname timer *time.Timer dead *atomic.Bool } func NewSession(id auth.SessId, uname auth.Uname) *Session { dead := new(atomic.Bool) timer := time.AfterFunc(auth.SessTimeout, func() { dead.Store(true) }) return &Session{ id, uname, timer, dead, } } // Close expires the session immediately. func (s *Session) Close() { s.timer.Stop() s.dead.Store(true) } // IsActive returns true if the session has not yet expired. func (s *Session) IsActive() bool { return !s.dead.Load() } // Extend resets the session's timer or returns false if it has // already expired. func (s *Session) Extend() bool { if s.timer.Stop() { s.timer.Reset(auth.SessTimeout) return true } // already expired return false }