check the data again after the GetOrSet write lock

This commit is contained in:
Gani Georgiev
2026-02-24 23:02:11 +02:00
parent c157331721
commit bc7080337e

View File

@@ -178,15 +178,22 @@ func (s *Store[K, T]) GetOrSet(key K, setFunc func() T) T {
s.mu.RLock()
v, ok := s.data[key]
s.mu.RUnlock()
if ok {
return v
}
// lock again for write
s.mu.Lock()
defer s.mu.Unlock()
// check again in case it was set between the 2 locks
v, ok = s.data[key]
if !ok {
s.mu.Lock()
v = setFunc()
if s.data == nil {
s.data = make(map[K]T)
}
s.data[key] = v
s.mu.Unlock()
}
return v