[#137] Expire session tokens locally

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-02-16 09:57:14 +03:00 committed by Alex Vanin
parent b8d2158acd
commit 11a25bb413

View file

@ -2,6 +2,7 @@ package pool
import ( import (
"strings" "strings"
"sync/atomic"
"time" "time"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
@ -9,7 +10,8 @@ import (
) )
type sessionCache struct { type sessionCache struct {
cache *lru.Cache cache *lru.Cache
currentEpoch uint64
} }
type cacheValue struct { type cacheValue struct {
@ -36,6 +38,11 @@ func (c *sessionCache) Get(key string) *session.Token {
} }
value := valueRaw.(*cacheValue) value := valueRaw.(*cacheValue)
if c.expired(value) {
c.cache.Remove(key)
return nil
}
value.atime = time.Now() value.atime = time.Now()
if value.token == nil { if value.token == nil {
@ -70,3 +77,15 @@ func (c *sessionCache) DeleteByPrefix(prefix string) {
} }
} }
} }
func (c *sessionCache) UpdateEpoch(newEpoch uint64) {
epoch := atomic.LoadUint64(&c.currentEpoch)
if newEpoch > epoch {
atomic.StoreUint64(&c.currentEpoch, newEpoch)
}
}
func (c *sessionCache) expired(val *cacheValue) bool {
epoch := atomic.LoadUint64(&c.currentEpoch)
return val.token.Exp() <= epoch
}