Merge pull request #14 from fossyy/staging
Fix mutex lock issue in user cache preventing login
This commit is contained in:
33
cache/user.go
vendored
33
cache/user.go
vendored
@ -16,21 +16,17 @@ type UserWithExpired struct {
|
|||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
AccessAt time.Time
|
AccessAt time.Time
|
||||||
}
|
|
||||||
|
|
||||||
type UserCache struct {
|
|
||||||
users map[string]*UserWithExpired
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var log *logger.AggregatedLogger
|
var log *logger.AggregatedLogger
|
||||||
var userCache *UserCache
|
var userCache map[string]*UserWithExpired
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log = logger.Logger()
|
log = logger.Logger()
|
||||||
|
|
||||||
userCache = &UserCache{users: make(map[string]*UserWithExpired)}
|
userCache = make(map[string]*UserWithExpired)
|
||||||
ticker := time.NewTicker(time.Hour * 8)
|
ticker := time.NewTicker(time.Hour)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@ -40,14 +36,14 @@ func init() {
|
|||||||
cleanID := utils.GenerateRandomString(10)
|
cleanID := utils.GenerateRandomString(10)
|
||||||
log.Info(fmt.Sprintf("Cache cleanup [user] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
log.Info(fmt.Sprintf("Cache cleanup [user] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
||||||
|
|
||||||
userCache.mu.Lock()
|
for _, user := range userCache {
|
||||||
for _, user := range userCache.users {
|
user.mu.Lock()
|
||||||
if currentTime.Sub(user.AccessAt) > time.Hour*8 {
|
if currentTime.Sub(user.AccessAt) > time.Hour*8 {
|
||||||
DeleteUser(user.Email)
|
delete(userCache, user.Email)
|
||||||
cacheClean++
|
cacheClean++
|
||||||
}
|
}
|
||||||
|
user.mu.Unlock()
|
||||||
}
|
}
|
||||||
userCache.mu.Unlock()
|
|
||||||
|
|
||||||
log.Info(fmt.Sprintf("Cache cleanup [user] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime)))
|
log.Info(fmt.Sprintf("Cache cleanup [user] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime)))
|
||||||
}
|
}
|
||||||
@ -55,10 +51,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetUser(email string) (*UserWithExpired, error) {
|
func GetUser(email string) (*UserWithExpired, error) {
|
||||||
userCache.mu.Lock()
|
if user, ok := userCache[email]; ok {
|
||||||
defer userCache.mu.Unlock()
|
|
||||||
|
|
||||||
if user, ok := userCache.users[email]; ok {
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +60,7 @@ func GetUser(email string) (*UserWithExpired, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userCache.users[email] = &UserWithExpired{
|
userCache[email] = &UserWithExpired{
|
||||||
UserID: userData.UserID,
|
UserID: userData.UserID,
|
||||||
Username: userData.Username,
|
Username: userData.Username,
|
||||||
Email: userData.Email,
|
Email: userData.Email,
|
||||||
@ -75,12 +68,12 @@ func GetUser(email string) (*UserWithExpired, error) {
|
|||||||
AccessAt: time.Now(),
|
AccessAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return userCache.users[email], nil
|
return userCache[email], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteUser(email string) {
|
func DeleteUser(email string) {
|
||||||
userCache.mu.Lock()
|
userCache[email].mu.Lock()
|
||||||
defer userCache.mu.Unlock()
|
defer userCache[email].mu.Unlock()
|
||||||
|
|
||||||
delete(userCache.users, email)
|
delete(userCache, email)
|
||||||
}
|
}
|
||||||
|
@ -69,11 +69,13 @@ func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
|
|||||||
next.ServeHTTP(w, req)
|
next.ServeHTTP(w, req)
|
||||||
return
|
return
|
||||||
case session.Unauthorized:
|
case session.Unauthorized:
|
||||||
|
if r.RequestURI != "/logout" {
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "redirect",
|
Name: "redirect",
|
||||||
Value: r.RequestURI,
|
Value: r.RequestURI,
|
||||||
Path: "/",
|
Path: "/",
|
||||||
})
|
})
|
||||||
|
}
|
||||||
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
case session.InvalidSession:
|
case session.InvalidSession:
|
||||||
|
Reference in New Issue
Block a user