Solved 5-session-cleaner

main
oabrivard 2 years ago
parent bcc767c7a1
commit 124e66798a

@ -20,17 +20,22 @@ package main
import ( import (
"errors" "errors"
"log" "log"
"sync"
"time"
) )
// SessionManager keeps track of all sessions from creation, updating // SessionManager keeps track of all sessions from creation, updating
// to destroying. // to destroying.
type SessionManager struct { type SessionManager struct {
sessions map[string]Session sessions map[string]Session
rwmu sync.RWMutex
} }
// Session stores the session's data // Session stores the session's data
type Session struct { type Session struct {
Data map[string]interface{} Data map[string]interface{}
timer *time.Timer
mu sync.Mutex
} }
// NewSessionManager creates a new sessionManager // NewSessionManager creates a new sessionManager
@ -42,6 +47,12 @@ func NewSessionManager() *SessionManager {
return m return m
} }
func (m *SessionManager) CleanSession(sessionID string) {
m.rwmu.Lock()
delete(m.sessions, sessionID)
m.rwmu.Unlock()
}
// CreateSession creates a new session and returns the sessionID // CreateSession creates a new session and returns the sessionID
func (m *SessionManager) CreateSession() (string, error) { func (m *SessionManager) CreateSession() (string, error) {
sessionID, err := MakeSessionID() sessionID, err := MakeSessionID()
@ -49,9 +60,14 @@ func (m *SessionManager) CreateSession() (string, error) {
return "", err return "", err
} }
m.rwmu.Lock()
m.sessions[sessionID] = Session{ m.sessions[sessionID] = Session{
Data: make(map[string]interface{}), Data: make(map[string]interface{}),
timer: time.AfterFunc(5*time.Second, func() {
m.CleanSession(sessionID)
}),
} }
m.rwmu.Unlock()
return sessionID, nil return sessionID, nil
} }
@ -63,7 +79,9 @@ var ErrSessionNotFound = errors.New("SessionID does not exists")
// GetSessionData returns data related to session if sessionID is // GetSessionData returns data related to session if sessionID is
// found, errors otherwise // found, errors otherwise
func (m *SessionManager) GetSessionData(sessionID string) (map[string]interface{}, error) { func (m *SessionManager) GetSessionData(sessionID string) (map[string]interface{}, error) {
m.rwmu.RLock()
session, ok := m.sessions[sessionID] session, ok := m.sessions[sessionID]
m.rwmu.RUnlock()
if !ok { if !ok {
return nil, ErrSessionNotFound return nil, ErrSessionNotFound
} }
@ -72,15 +90,22 @@ func (m *SessionManager) GetSessionData(sessionID string) (map[string]interface{
// UpdateSessionData overwrites the old session data with the new one // UpdateSessionData overwrites the old session data with the new one
func (m *SessionManager) UpdateSessionData(sessionID string, data map[string]interface{}) error { func (m *SessionManager) UpdateSessionData(sessionID string, data map[string]interface{}) error {
_, ok := m.sessions[sessionID] m.rwmu.RLock()
s, ok := m.sessions[sessionID]
m.rwmu.RUnlock()
if !ok { if !ok {
return ErrSessionNotFound return ErrSessionNotFound
} }
// Hint: you should renew expiry of the session here // Lock the session data to handle concurrent access to the same session
m.sessions[sessionID] = Session{ s.mu.Lock()
Data: data, s.Data = data
if !s.timer.Stop() {
<-s.timer.C
} }
s.timer.Reset(5 * time.Second)
s.mu.Unlock()
return nil return nil
} }

Loading…
Cancel
Save