Documentation ¶
Overview ¶
Package lockutil provides a sync.Lock implementation that ensures a certain interval of time has passed since the last time a lock was acquired.
Index ¶
- func Compute[T, O any](lv *LockedValue[T], fn func(val T) O) O
- func DeleteMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K)
- func GetMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K) V
- func LoadOrStoreMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K, value V) (actual V, loaded bool)
- func SetMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K, value V)
- func WithGuaranteedTimeSinceLastRelease(l sync.Locker, minTimeSinceUnlock time.Duration) sync.Locker
- type LockedValue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compute ¶
func Compute[T, O any](lv *LockedValue[T], fn func(val T) O) O
Compute calls the callback with the current value while the lock is held and computes a value. Care must be taken by the callback to avoid deadlock.
func DeleteMapValue ¶
func DeleteMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K)
DeleteMapValue returns the value of a key within a locked map.
func GetMapValue ¶
func GetMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K) V
GetMapValue returns the value of a key within a locked map.
func LoadOrStoreMapValue ¶
func LoadOrStoreMapValue[K comparable, V any]( lv *LockedValue[map[K]V], key K, value V) (actual V, loaded bool)
LoadOrStoreMapValue sets a value in a locked map
func SetMapValue ¶
func SetMapValue[K comparable, V any](lv *LockedValue[map[K]V], key K, value V)
SetMapValue sets the value of a value within a locked map.
func WithGuaranteedTimeSinceLastRelease ¶
func WithGuaranteedTimeSinceLastRelease(l sync.Locker, minTimeSinceUnlock time.Duration) sync.Locker
WithGuaranteedTimeSinceLastRelease returns a sync.Locker based on another locker that guarantees the lock can only be acquired a certain period after the most recent Unlock.
Types ¶
type LockedValue ¶
type LockedValue[T any] struct { // contains filtered or unexported fields }
LockedValue holds a value of a particular type with a sync.Mutex.
func NewLockedValue ¶
func NewLockedValue[T any](v T) *LockedValue[T]
NewLockedValue returns a new locked value that holds the provided value.
func (*LockedValue[T]) Load ¶
func (lv *LockedValue[T]) Load() T
func (*LockedValue[T]) Observe ¶
func (lv *LockedValue[T]) Observe(fn func(val T))
Observe calls the callback with the current value while the lock is held. Care must be taken by the callback to avoid deadlock.
func (*LockedValue[T]) Store ¶
func (lv *LockedValue[T]) Store(v T)
func (*LockedValue[T]) Update ¶
func (lv *LockedValue[T]) Update(update func(old T) T)
Update calls the callback with the old value while the lock is held and then sets the value to the result of the callback. Care must be taken by the callback to avoid deadlock.