[#137] Expire session tokens locally

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
remotes/fyrchik/split-info-format
Denis Kirillov 2022-02-16 09:57:14 +03:00 committed by Alex Vanin
parent b8d2158acd
commit 11a25bb413
1 changed files with 20 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package pool
import (
"strings"
"sync/atomic"
"time"
lru "github.com/hashicorp/golang-lru"
@ -9,7 +10,8 @@ import (
)
type sessionCache struct {
cache *lru.Cache
cache *lru.Cache
currentEpoch uint64
}
type cacheValue struct {
@ -36,6 +38,11 @@ func (c *sessionCache) Get(key string) *session.Token {
}
value := valueRaw.(*cacheValue)
if c.expired(value) {
c.cache.Remove(key)
return nil
}
value.atime = time.Now()
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
}