Documentation ¶
Index ¶
- func Run(ctx context.Context, fn func()) error
- func RunTimeout(ctx context.Context, fn func(), timeout time.Duration) error
- type LockedValueWatcher
- type Map
- func (m *Map[K, T]) All() iter.Seq2[K, T]
- func (m *Map[K, T]) Clear()
- func (m *Map[K, T]) Copy() *Map[K, T]
- func (m *Map[K, T]) Delete(key K)
- func (m *Map[K, T]) Get(key K) (T, bool)
- func (m *Map[K, T]) Keys() []K
- func (m *Map[K, T]) Len() int
- func (m *Map[K, T]) Merge(other *Map[K, T])
- func (m *Map[K, T]) Set(key K, value T)
- func (m *Map[K, T]) ToGoMap() map[K]T
- func (m *Map[K, T]) Values() []T
- type ValueWatcher
- func (vw *ValueWatcher[T]) CompareAndSwap(val T, shouldSwap func(T) bool) bool
- func (vw *ValueWatcher[T]) Get() T
- func (vw *ValueWatcher[T]) Lock() *LockedValueWatcher[T]
- func (vw *ValueWatcher[T]) Set(val T)
- func (vw *ValueWatcher[T]) Watch(ctx context.Context, f ValueWatcherCompareFunc[T]) (T, error)
- type ValueWatcherCompareFunc
- type WaitGroup
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run executes fn in a goroutine and waits for it to return. If the context gets canceled before that happens the method returns the context error.
This is useful for executing long-running functions like sync.WaitGroup.Wait that don't take a context and can potentially block the execution forever.
Types ¶
type LockedValueWatcher ¶
type LockedValueWatcher[T any] struct { // contains filtered or unexported fields }
func (*LockedValueWatcher[T]) Get ¶
func (lvw *LockedValueWatcher[T]) Get() T
Get returns the current value stored in ValueWatcher.
func (*LockedValueWatcher[T]) Set ¶
func (lvw *LockedValueWatcher[T]) Set(val T)
Set stores val in ValueWatcher and notifies all goroutines that called Watch about the new value, if such goroutines exists.
func (*LockedValueWatcher[T]) Unlock ¶
func (lvw *LockedValueWatcher[T]) Unlock() *ValueWatcher[T]
Unlock unlocks the ValueWatcher. After this the LockedValueWatcher should be discarded.
type Map ¶ added in v0.4.0
type Map[K comparable, T any] struct { // contains filtered or unexported fields }
Map is a thread-safe map.
func NewMap ¶ added in v0.4.0
func NewMap[K comparable, T any]() *Map[K, T]
NewMap creates a new map.
func (*Map[K, T]) All ¶ added in v0.4.0
All returns an iterator over the map's key-value pairs. This can be used to iterate over the map using a for-range loop.
Example:
for key, value := range m.All() { fmt.Println(key, value) }
func (*Map[K, T]) Clear ¶ added in v0.4.0
func (m *Map[K, T]) Clear()
Clear removes all items from the map.
func (*Map[K, T]) Delete ¶ added in v0.4.0
func (m *Map[K, T]) Delete(key K)
Delete removes the value for a key.
func (*Map[K, T]) Keys ¶ added in v0.4.0
func (m *Map[K, T]) Keys() []K
Keys returns all the keys in the map.
func (*Map[K, T]) Merge ¶ added in v0.4.0
Merge adds all key-value pairs from another map to this map.
func (*Map[K, T]) Set ¶ added in v0.4.0
func (m *Map[K, T]) Set(key K, value T)
Set sets the value for a key.
type ValueWatcher ¶
type ValueWatcher[T any] struct { // contains filtered or unexported fields }
ValueWatcher holds a reference to a value. Multiple goroutines are able to put or retrieve the value into/from the ValueWatcher, as well as wait for a certain value to be put into the ValueWatcher. It is similar to atomic.Value except the caller can call Watch to be notified each time the value in ValueWatcher changes.
func (*ValueWatcher[T]) CompareAndSwap ¶
func (vw *ValueWatcher[T]) CompareAndSwap(val T, shouldSwap func(T) bool) bool
CompareAndSwap allows the caller to peek at the currently stored value in ValueWatcher and decide if it should be swapped for a new val, all while holding the lock. If the value was swapped, it notifies all goroutines that called Watch about the new value and returns true. Otherwise it returns false.
func (*ValueWatcher[T]) Get ¶
func (vw *ValueWatcher[T]) Get() T
Get returns the current value stored in ValueWatcher.
func (*ValueWatcher[T]) Lock ¶
func (vw *ValueWatcher[T]) Lock() *LockedValueWatcher[T]
Lock locks the ValueWatcher and returns an instance of LockedValueWatcher that allows one to set and get the value while holding the lock. After the caller executed the operations and doesn't need the lock anymore it should call Unlock on the LockedValueWatcher before discarding it.
func (*ValueWatcher[T]) Set ¶
func (vw *ValueWatcher[T]) Set(val T)
Set stores val in ValueWatcher and notifies all goroutines that called Watch about the new value, if such goroutines exists.
func (*ValueWatcher[T]) Watch ¶
func (vw *ValueWatcher[T]) Watch(ctx context.Context, f ValueWatcherCompareFunc[T]) (T, error)
Watch blocks and calls f for every value that is put into the ValueWatcher. Once f returns true it stops blocking and returns the value. First call to f will be with the current value stored in ValueWatcher. Note that if no value was stored in ValueWatcher yet, the zero value of type T will be passed to f. If the context gets canceled before the value is found, the function returns the last seen value and the context error.
Watch can be safely called by multiple goroutines. If the context gets cancelled before f returns true, the function will return the context error.
type ValueWatcherCompareFunc ¶
func WatchValues ¶
func WatchValues[T comparable](want ...T) ValueWatcherCompareFunc[T]
WatchValues is a utility function for creating a simple ValueWatcherCompareFunc that waits for one of the supplied values.
type WaitGroup ¶
WaitGroup is a sync.WaitGroup with utility methods.
func (*WaitGroup) Add ¶
Add adds delta, which may be negative, to the WaitGroup counter. If the counter becomes zero, all goroutines blocked on Wait are released. If the counter goes negative, Add panics.
Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. If a WaitGroup is reused to wait for several independent sets of events, new Add calls must happen after all previous Wait calls have returned. See the WaitGroup example.