From 11a25bb4130b625ab6fd2695d51f3c637729b630 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 16 Feb 2022 09:57:14 +0300 Subject: [PATCH] [#137] Expire session tokens locally Signed-off-by: Denis Kirillov --- pool/cache.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pool/cache.go b/pool/cache.go index c566f32..4281853 100644 --- a/pool/cache.go +++ b/pool/cache.go @@ -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 +}