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()
|
log = logger.Logger()
|
||||||
|
|
||||||
userCache = make(map[string]*UserWithExpired)
|
userCache = make(map[string]*UserWithExpired)
|
||||||
ticker := time.NewTicker(time.Hour)
|
ticker := time.NewTicker(time.Minute)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -50,7 +50,7 @@ func init() {
|
|||||||
|
|
||||||
for _, data := range VerifyUser {
|
for _, data := range VerifyUser {
|
||||||
data.mu.Lock()
|
data.mu.Lock()
|
||||||
if currentTime.Sub(data.CreateTime) > time.Minute*1 {
|
if currentTime.Sub(data.CreateTime) > time.Minute*10 {
|
||||||
delete(VerifyUser, data.Code)
|
delete(VerifyUser, data.Code)
|
||||||
delete(VerifyEmail, data.User.Email)
|
delete(VerifyEmail, data.User.Email)
|
||||||
cacheClean++
|
cacheClean++
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fossyy/filekeeper/logger"
|
||||||
"github.com/fossyy/filekeeper/types"
|
"github.com/fossyy/filekeeper/types"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -11,9 +13,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ID string
|
ID string
|
||||||
Values map[string]interface{}
|
Values map[string]interface{}
|
||||||
mu sync.Mutex
|
CreateTime time.Time
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionInfo struct {
|
type SessionInfo struct {
|
||||||
@ -28,6 +31,7 @@ type SessionInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserStatus string
|
type UserStatus string
|
||||||
|
type SessionNotFoundError struct{}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Authorized UserStatus = "authorized"
|
Authorized UserStatus = "authorized"
|
||||||
@ -37,8 +41,34 @@ const (
|
|||||||
|
|
||||||
var GlobalSessionStore = make(map[string]*Session)
|
var GlobalSessionStore = make(map[string]*Session)
|
||||||
var UserSessionInfoList = make(map[string]map[string]*SessionInfo)
|
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 {
|
func (e *SessionNotFoundError) Error() string {
|
||||||
return "session not found"
|
return "session not found"
|
||||||
|
Reference in New Issue
Block a user