From e27907560ab6e5037d8af904aeead012fd95e639 Mon Sep 17 00:00:00 2001 From: Magnus Kokk Date: Mon, 6 Nov 2023 16:47:20 +0200 Subject: [PATCH] backend: Validate element before pushing to list --- internal/backend/backend.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/backend/backend.go b/internal/backend/backend.go index 2e308ed..c8a69ed 100644 --- a/internal/backend/backend.go +++ b/internal/backend/backend.go @@ -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)