Implement session cleanup
This commit is contained in:
2
cache/user.go
vendored
2
cache/user.go
vendored
@ -26,7 +26,7 @@ func init() {
|
||||
log = logger.Logger()
|
||||
|
||||
userCache = make(map[string]*UserWithExpired)
|
||||
ticker := time.NewTicker(time.Hour)
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
|
@ -50,7 +50,7 @@ func init() {
|
||||
|
||||
for _, data := range VerifyUser {
|
||||
data.mu.Lock()
|
||||
if currentTime.Sub(data.CreateTime) > time.Minute*1 {
|
||||
if currentTime.Sub(data.CreateTime) > time.Minute*10 {
|
||||
delete(VerifyUser, data.Code)
|
||||
delete(VerifyEmail, data.User.Email)
|
||||
cacheClean++
|
||||
|
@ -1,6 +1,8 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/logger"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -13,6 +15,7 @@ import (
|
||||
type Session struct {
|
||||
ID string
|
||||
Values map[string]interface{}
|
||||
CreateTime time.Time
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
@ -28,6 +31,7 @@ type SessionInfo struct {
|
||||
}
|
||||
|
||||
type UserStatus string
|
||||
type SessionNotFoundError struct{}
|
||||
|
||||
const (
|
||||
Authorized UserStatus = "authorized"
|
||||
@ -37,8 +41,34 @@ const (
|
||||
|
||||
var GlobalSessionStore = make(map[string]*Session)
|
||||
var UserSessionInfoList = make(map[string]map[string]*SessionInfo)
|
||||
var log *logger.AggregatedLogger
|
||||
|
||||
type SessionNotFoundError struct{}
|
||||
func init() {
|
||||
log = logger.Logger()
|
||||
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
go func() {
|
||||
for {
|
||||
<-ticker.C
|
||||
currentTime := time.Now()
|
||||
cacheClean := 0
|
||||
cleanID := utils.GenerateRandomString(10)
|
||||
log.Info(fmt.Sprintf("Cache cleanup [Session] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
||||
|
||||
for _, data := range GlobalSessionStore {
|
||||
data.mu.Lock()
|
||||
if currentTime.Sub(data.CreateTime) > time.Hour*24*7 {
|
||||
RemoveSessionInfo(data.Values["user"].(types.User).Email, data.ID)
|
||||
delete(GlobalSessionStore, data.ID)
|
||||
cacheClean++
|
||||
}
|
||||
data.mu.Unlock()
|
||||
}
|
||||
|
||||
log.Info(fmt.Sprintf("Cache cleanup [Session] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime)))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (e *SessionNotFoundError) Error() string {
|
||||
return "session not found"
|
||||
|
Reference in New Issue
Block a user