begin handling cleanup of session tokens
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"astraltech.xyz/accountmanager/src/logging"
|
"astraltech.xyz/accountmanager/src/logging"
|
||||||
"astraltech.xyz/accountmanager/src/store"
|
"astraltech.xyz/accountmanager/src/store"
|
||||||
|
"astraltech.xyz/accountmanager/src/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
const SessionCookieName = "session_token"
|
const SessionCookieName = "session_token"
|
||||||
@@ -37,7 +38,11 @@ func (manager *SessionManager) SetStoreType(storeType StoreType) {
|
|||||||
switch storeType {
|
switch storeType {
|
||||||
case InMemory:
|
case InMemory:
|
||||||
{
|
{
|
||||||
// manager.store = NewMemoryStore()
|
manager.store = store.NewMemoryStore[*SessionData]()
|
||||||
|
worker.CreateWorker(time.Minute*5, func() {
|
||||||
|
inMemStore, _ := manager.store.(*store.MemoryStore[*SessionData])
|
||||||
|
cleanupInMemoryStore(inMemStore)
|
||||||
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case Redis:
|
case Redis:
|
||||||
@@ -98,6 +103,23 @@ func (manager *SessionManager) GetSession(r *http.Request) (*SessionData, error)
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanupInMemoryStore(m *store.MemoryStore[*SessionData]) {
|
||||||
|
logging.Debug("Cleaning up memory store sessions")
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
m.Lock.Lock()
|
||||||
|
defer m.Lock.Unlock()
|
||||||
|
|
||||||
|
deleted := 0
|
||||||
|
for id, session := range m.Sessions {
|
||||||
|
if now.After(session.ExpiresAt) {
|
||||||
|
delete(m.Sessions, id)
|
||||||
|
deleted = deleted + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logging.Infof("Cleaned up %d stale sessions", deleted)
|
||||||
|
}
|
||||||
|
|
||||||
func (manager *SessionManager) DeleteSession(sessionId string) error {
|
func (manager *SessionManager) DeleteSession(sessionId string) error {
|
||||||
return manager.store.Delete(sessionId)
|
return manager.store.Delete(sessionId)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MemoryStore[Value any] struct {
|
type MemoryStore[Value any] struct {
|
||||||
sessions map[string]Value
|
Sessions map[string]Value
|
||||||
lock sync.RWMutex
|
Lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMemoryStore[Value any]() *MemoryStore[Value] {
|
func NewMemoryStore[Value any]() *MemoryStore[Value] {
|
||||||
logging.Debug("Creating new in memory session store")
|
logging.Debug("Creating new in memory session store")
|
||||||
store := &MemoryStore[Value]{
|
store := &MemoryStore[Value]{
|
||||||
sessions: make(map[string]Value),
|
Sessions: make(map[string]Value),
|
||||||
}
|
}
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
@@ -22,24 +22,24 @@ func NewMemoryStore[Value any]() *MemoryStore[Value] {
|
|||||||
func (m *MemoryStore[Value]) Create(key string, session Value) (err error) {
|
func (m *MemoryStore[Value]) Create(key string, session Value) (err error) {
|
||||||
hashedkey := HashKey(key)
|
hashedkey := HashKey(key)
|
||||||
|
|
||||||
m.lock.Lock()
|
m.Lock.Lock()
|
||||||
defer m.lock.Unlock()
|
defer m.Lock.Unlock()
|
||||||
_, exist := m.sessions[hashedkey]
|
_, exist := m.Sessions[hashedkey]
|
||||||
if exist {
|
if exist {
|
||||||
return ErrKeyAlreadyExists
|
return ErrKeyAlreadyExists
|
||||||
}
|
}
|
||||||
|
|
||||||
m.sessions[hashedkey] = session
|
m.Sessions[hashedkey] = session
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MemoryStore[Value]) Get(key string) (Value, error) {
|
func (m *MemoryStore[Value]) Get(key string) (Value, error) {
|
||||||
var data Value
|
var data Value
|
||||||
|
|
||||||
m.lock.RLock()
|
m.Lock.RLock()
|
||||||
hashedkey := HashKey(key)
|
hashedkey := HashKey(key)
|
||||||
data, exists := m.sessions[hashedkey]
|
data, exists := m.Sessions[hashedkey]
|
||||||
m.lock.RUnlock()
|
m.Lock.RUnlock()
|
||||||
if exists == false {
|
if exists == false {
|
||||||
return data, ErrKeyNotFound
|
return data, ErrKeyNotFound
|
||||||
}
|
}
|
||||||
@@ -49,25 +49,25 @@ func (m *MemoryStore[Value]) Get(key string) (Value, error) {
|
|||||||
func (m *MemoryStore[Value]) Update(sessionID string, session Value) error {
|
func (m *MemoryStore[Value]) Update(sessionID string, session Value) error {
|
||||||
hashedkey := HashKey(sessionID)
|
hashedkey := HashKey(sessionID)
|
||||||
|
|
||||||
m.lock.Lock()
|
m.Lock.Lock()
|
||||||
defer m.lock.Unlock()
|
defer m.Lock.Unlock()
|
||||||
_, exist := m.sessions[hashedkey]
|
_, exist := m.Sessions[hashedkey]
|
||||||
if !exist {
|
if !exist {
|
||||||
return ErrKeyNotFound
|
return ErrKeyNotFound
|
||||||
}
|
}
|
||||||
m.sessions[hashedkey] = session
|
m.Sessions[hashedkey] = session
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MemoryStore[Value]) Delete(sessionID string) error {
|
func (m *MemoryStore[Value]) Delete(sessionID string) error {
|
||||||
hashedkey := HashKey(sessionID)
|
hashedkey := HashKey(sessionID)
|
||||||
|
|
||||||
m.lock.Lock()
|
m.Lock.Lock()
|
||||||
defer m.lock.Unlock()
|
defer m.Lock.Unlock()
|
||||||
_, exist := m.sessions[hashedkey]
|
_, exist := m.Sessions[hashedkey]
|
||||||
if !exist {
|
if !exist {
|
||||||
return ErrKeyNotFound
|
return ErrKeyNotFound
|
||||||
}
|
}
|
||||||
delete(m.sessions, hashedkey)
|
delete(m.Sessions, hashedkey)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user