From f8c33075f145ec877b82412d21813173861b09c5 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Sun, 16 Oct 2022 21:12:22 +0200 Subject: [PATCH] pkg/lazy: Remove unusued methods --- pkg/lazy/lazy.go | 50 +++--------------------------------------------- 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/pkg/lazy/lazy.go b/pkg/lazy/lazy.go index 2590572..4c38be5 100644 --- a/pkg/lazy/lazy.go +++ b/pkg/lazy/lazy.go @@ -2,18 +2,16 @@ package lazy import ( "sync" - "time" ) // Lazy is an object that a lazily-initialized value of type T. // // A Lazy must not be copied after first use. type Lazy[T any] struct { - once sync.Once - value T + once sync.Once - m sync.RWMutex // m protects resetting this lazy - lastReset time.Time // last time this mutex was reset + m sync.RWMutex // m protects setting the value of this T + value T } // Get returns the value associated with this Lazy. @@ -44,45 +42,3 @@ func (lazy *Lazy[T]) Set(value T) { lazy.value = value lazy.once.Do(func() {}) } - -// Reset resets this Lazy, deleting any previously associated value. -// -// May be called concurrently with [Get]. -// Future calls to [Get] will invoke init. -func (lazy *Lazy[T]) Reset() { - lazy.m.Lock() - defer lazy.m.Unlock() - - lazy.reset() -} - -// ResetAfter resets this lazy if more than d time has passed since the last reset. -// If ResetAfter cannot lock, then it does not reset. -// -// May be called concurrently with other functions on this lazy. -func (lazy *Lazy[T]) ResetAfter(d time.Duration) { - if !lazy.m.TryLock() { - return - } - defer lazy.m.Unlock() - - if time.Since(lazy.lastReset) < d { - return - } - - lazy.reset() -} - -// reset implements resetting this lazy. -// m myst be held for writing. -func (lazy *Lazy[T]) reset() { - // reset the once - lazy.once = sync.Once{} - - // reset the value - var t T - lazy.value = t - - // time of the last reset - lazy.lastReset = time.Now() -}