pkg/lazy: Remove unusued methods

This commit is contained in:
Tom Wiesing 2022-10-16 21:12:22 +02:00
parent 8701fab93b
commit f8c33075f1
No known key found for this signature in database

View file

@ -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()
}