diff --git a/core/utils/lazy.go b/core/utils/lazy.go deleted file mode 100644 index 43e84808159..00000000000 --- a/core/utils/lazy.go +++ /dev/null @@ -1,36 +0,0 @@ -package utils - -import ( - "sync" -) - -type LazyLoad[T any] struct { - f func() (T, error) - state T - ok bool - lock sync.Mutex -} - -func NewLazyLoad[T any](f func() (T, error)) *LazyLoad[T] { - return &LazyLoad[T]{ - f: f, - } -} - -func (l *LazyLoad[T]) Get() (out T, err error) { - l.lock.Lock() - defer l.lock.Unlock() - - if l.ok { - return l.state, nil - } - l.state, err = l.f() - l.ok = err == nil - return l.state, err -} - -func (l *LazyLoad[T]) Reset() { - l.lock.Lock() - defer l.lock.Unlock() - l.ok = false -} diff --git a/core/utils/lazy_test.go b/core/utils/lazy_test.go deleted file mode 100644 index 88d02eea7f2..00000000000 --- a/core/utils/lazy_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package utils - -import ( - "sync" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestLazyLoad(t *testing.T) { - var clientwg sync.WaitGroup - - tc := func() (int, error) { - clientwg.Done() - return 10, nil - } - - // Get should only request a client once, use cached afterward - t.Run("get", func(t *testing.T) { - clientwg.Add(1) // expect one call to get client - c := NewLazyLoad(tc) - rw, err := c.Get() - assert.NoError(t, err) - assert.NotNil(t, rw) - assert.NotNil(t, c.state) - - // used cached client - rw, err = c.Get() - assert.NoError(t, err) - assert.NotNil(t, rw) - clientwg.Wait() - }) - - // Clear removes the cached client, should refetch - t.Run("clear", func(t *testing.T) { - clientwg.Add(2) // expect two calls to get client - - c := NewLazyLoad(tc) - rw, err := c.Get() - assert.NotNil(t, rw) - assert.NoError(t, err) - - c.Reset() - - rw, err = c.Get() - assert.NotNil(t, rw) - assert.NoError(t, err) - clientwg.Wait() - }) - - // Race checks a race condition of Getting and Clearing a new client - t.Run("race", func(t *testing.T) { - clientwg.Add(1) // expect one call to get client - - c := NewLazyLoad(tc) - var wg sync.WaitGroup - wg.Add(2) - go func() { - rw, err := c.Get() - assert.NoError(t, err) - assert.NotNil(t, rw) - wg.Done() - }() - go func() { - c.Reset() - wg.Done() - }() - wg.Wait() - clientwg.Wait() - }) -}