common/helpers: fix race condition when checking last access on expire

This commit is contained in:
Vincent Bernat
2023-02-01 12:03:58 +01:00
parent 680433eef2
commit e4b30ecea7

View File

@@ -105,7 +105,8 @@ func (c *Cache[K, V]) DeleteLastAccessedBefore(before time.Time) int {
c.mu.Lock()
defer c.mu.Unlock()
for k, v := range c.items {
if v.LastAccessed < before.Unix() {
last := atomic.LoadInt64(&v.LastAccessed)
if last < before.Unix() {
delete(c.items, k)
count++
}