Skip to content

Commit

Permalink
backend: Validate element before pushing to list
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsk committed Nov 6, 2023
1 parent b25883b commit e279075
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,20 @@ func (b *Backend[K, V]) Initialize(key K, value V, ttl time.Duration) {
panic("Initialize: expected element to exist")
}

if elem.Value.initialized {
panic("Initialize: expected an uninitialized element")
}

defer elem.Value.wg.Done()

elem.Value.value = value
if ttl > 0 {
elem.Value.deadline = time.Now().Add(ttl).UnixNano()
}

b.list.PushBack(elem)
if elem.Value.initialized {
panic("Initialize: expected an uninitialized element")
}
elem.Value.initialized = true

defer elem.Value.wg.Done()

if n := b.overflow(); n > 0 {
b.startGCOnce()
b.timer.Reset(0)
Expand Down

0 comments on commit e279075

Please sign in to comment.