Skip to content

Commit

Permalink
Revert singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Sep 27, 2024
1 parent b45e276 commit 547ac1c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/pkg/cache/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ type Singleton[V any] struct {
get func(context.Context) (V, error)
value V
err error
once sync.Once
// Storing a bool to not deal with generic zero/nil comparisons.
called bool
lock sync.RWMutex
}

// NewSingleton returns a new Singleton.
Expand All @@ -49,8 +51,17 @@ func (s *Singleton[V]) Get(ctx context.Context) (V, error) {
var zero V
return zero, errors.New("must create singleton with NewSingleton and a non-nil get function")
}
s.once.Do(func() {
s.lock.RLock()
if s.called {
s.lock.RUnlock()
return s.value, s.err
}
s.lock.RUnlock()
s.lock.Lock()
defer s.lock.Unlock()
if !s.called {
s.value, s.err = s.get(ctx)
})
s.called = true
}
return s.value, s.err
}

0 comments on commit 547ac1c

Please sign in to comment.