Skip to content

Commit

Permalink
Fix and optimize memory storage (#2207)
Browse files Browse the repository at this point in the history
* internal/memory: cache timestamp

* internal/memory: ensure to never delete non-expired items

This fixes a TOCTOU problem between a mutex rlock and a mutex lock.

* internal/memory: move costly operations outside of locked area

* internal/storage: cache timestamp

* internal/storage: ensure to never delete non-expired items

This fixes a TOCTOU problem between a mutex rlock and a mutex lock.

* internal/storage: move costly operations outside of locked area
  • Loading branch information
leonklingele committed Nov 14, 2022
1 parent e5ae764 commit a0645af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions internal/memory/memory.go
Expand Up @@ -47,8 +47,9 @@ func (s *Storage) Set(key string, val interface{}, ttl time.Duration) {
if ttl > 0 {
exp = uint32(ttl.Seconds()) + atomic.LoadUint32(&utils.Timestamp)
}
i := item{exp, val}
s.Lock()
s.data[key] = item{exp, val}
s.data[key] = i
s.Unlock()
}

Expand All @@ -61,8 +62,9 @@ func (s *Storage) Delete(key string) {

// Reset all keys
func (s *Storage) Reset() {
nd := make(map[string]item)
s.Lock()
s.data = make(map[string]item)
s.data = nd
s.Unlock()
}

Expand All @@ -74,17 +76,23 @@ func (s *Storage) gc(sleep time.Duration) {
for {
select {
case <-ticker.C:
ts := atomic.LoadUint32(&utils.Timestamp)
expired = expired[:0]
s.RLock()
for key, v := range s.data {
if v.e != 0 && v.e <= atomic.LoadUint32(&utils.Timestamp) {
if v.e != 0 && v.e <= ts {
expired = append(expired, key)
}
}
s.RUnlock()
s.Lock()
// Double-checked locking.
// We might have replaced the item in the meantime.
for i := range expired {
delete(s.data, expired[i])
v := s.data[expired[i]]
if v.e != 0 && v.e <= ts {
delete(s.data, expired[i])
}
}
s.Unlock()
}
Expand Down
16 changes: 12 additions & 4 deletions internal/storage/memory/memory.go
Expand Up @@ -70,8 +70,9 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
expire = uint32(exp.Seconds()) + atomic.LoadUint32(&utils.Timestamp)
}

e := entry{val, expire}
s.mux.Lock()
s.db[key] = entry{val, expire}
s.db[key] = e
s.mux.Unlock()
return nil
}
Expand All @@ -90,8 +91,9 @@ func (s *Storage) Delete(key string) error {

// Reset all keys
func (s *Storage) Reset() error {
ndb := make(map[string]entry)
s.mux.Lock()
s.db = make(map[string]entry)
s.db = ndb
s.mux.Unlock()
return nil
}
Expand All @@ -112,17 +114,23 @@ func (s *Storage) gc() {
case <-s.done:
return
case <-ticker.C:
ts := atomic.LoadUint32(&utils.Timestamp)
expired = expired[:0]
s.mux.RLock()
for id, v := range s.db {
if v.expiry != 0 && v.expiry < atomic.LoadUint32(&utils.Timestamp) {
if v.expiry != 0 && v.expiry <= ts {
expired = append(expired, id)
}
}
s.mux.RUnlock()
s.mux.Lock()
// Double-checked locking.
// We might have replaced the item in the meantime.
for i := range expired {
delete(s.db, expired[i])
v := s.db[expired[i]]
if v.expiry != 0 && v.expiry <= ts {
delete(s.db, expired[i])
}
}
s.mux.Unlock()
}
Expand Down

0 comments on commit a0645af

Please sign in to comment.