Documentation
¶
Overview ¶
Package syncutil contains extensions and utilities for package sync from the standard library.
Index ¶
- type ChanSemaphore
- type EmptySemaphore
- type Map
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)
- func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Load(key K) (value V, ok bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *Map[K, V]) Range(f func(key K, value V) (cont bool))
- func (m *Map[K, V]) Store(key K, value V)
- func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)
- type OnceConstructor
- type Pool
- type Semaphore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChanSemaphore ¶ added in v0.18.1
type ChanSemaphore struct {
// contains filtered or unexported fields
}
ChanSemaphore is a channel-based semaphore.
It must be initialized with NewChanSemaphore.
func NewChanSemaphore ¶ added in v0.18.1
func NewChanSemaphore(maxRes uint) (c *ChanSemaphore)
NewChanSemaphore returns a new *ChanSemaphore with the provided maximum resource number.
func (*ChanSemaphore) Acquire ¶ added in v0.18.1
func (c *ChanSemaphore) Acquire(ctx context.Context) (err error)
Acquire implements the Semaphore interface for *ChanSemaphore.
func (*ChanSemaphore) Release ¶ added in v0.18.1
func (c *ChanSemaphore) Release()
Release implements the Semaphore interface for *ChanSemaphore.
type EmptySemaphore ¶ added in v0.18.1
type EmptySemaphore struct{}
EmptySemaphore is a semaphore that has no limit.
func (EmptySemaphore) Acquire ¶ added in v0.18.1
func (EmptySemaphore) Acquire(_ context.Context) (err error)
Acquire implements the Semaphore interface for EmptySemaphore. It always returns nil.
func (EmptySemaphore) Release ¶ added in v0.18.1
func (EmptySemaphore) Release()
Release implements the Semaphore interface for EmptySemaphore.
type Map ¶ added in v0.34.1
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a generic wrapper around sync.Map for better type safety.
TODO(a.garipov): Remove once https://github.com/golang/go/issues/71076 is implemented.
func NewMap ¶ added in v0.34.1
func NewMap[K comparable, V any]() (m *Map[K, V])
NewMap returns a new properly initialized *Map.
func (*Map[K, V]) Clear ¶ added in v0.34.1
func (m *Map[K, V]) Clear()
Clear deletes all the entries, resulting in an empty Map.
See sync.Map.Clear.
func (*Map[K, V]) CompareAndDelete ¶ added in v0.34.1
CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.
If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).
func (*Map[K, V]) CompareAndSwap ¶ added in v0.34.1
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old.
func (*Map[K, V]) Delete ¶ added in v0.34.1
func (m *Map[K, V]) Delete(key K)
Delete deletes the value for a key.
See sync.Map.Delete.
func (*Map[K, V]) Load ¶ added in v0.34.1
Load returns the value stored in the map for a key, or its zero value if no value is present.
See sync.Map.Load.
func (*Map[K, V]) LoadAndDelete ¶ added in v0.34.1
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*Map[K, V]) LoadOrStore ¶ added in v0.34.1
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value.
See sync.Map.LoadOrStore.
func (*Map[K, V]) Range ¶ added in v0.34.1
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
See sync.Map.Range.
func (*Map[K, V]) Store ¶ added in v0.34.1
func (m *Map[K, V]) Store(key K, value V)
Store sets the value for a key.
See sync.Map.Store.
func (*Map[K, V]) Swap ¶ added in v0.34.1
Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.
See sync.Map.Swap.
type OnceConstructor ¶ added in v0.27.0
type OnceConstructor[K comparable, V any] struct { // contains filtered or unexported fields }
OnceConstructor initializes a value for one key only once.
TODO(a.garipov): Add benchmarks.
func NewOnceConstructor ¶ added in v0.27.0
func NewOnceConstructor[K comparable, V any](newFunc func(k K) (v V)) (c *OnceConstructor[K, V])
NewOnceConstructor returns a new properly initialized *OnceConstructor that uses newFunc to construct a value for the given key.
func (*OnceConstructor[K, V]) Get ¶ added in v0.27.0
func (c *OnceConstructor[K, V]) Get(key K) (v V)
Get returns a value for the given key. If a value isn't available, it waits until it is.
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
Pool is the strongly typed version of sync.Pool that manages pointers to T.
func NewSlicePool ¶
NewSlicePool is a helper for constructing pools with pointers to slices of a type with the given length.
func (*Pool[T]) Get ¶
func (p *Pool[T]) Get() (v *T)
Get selects an arbitrary item from the pool, removes it from the pool, and returns it to the caller.
See sync.Pool.Get.
type Semaphore ¶ added in v0.18.1
type Semaphore interface {
// Acquire gets the resource, will block until the resource can be acquired.
// ctx is used for cancellation.
Acquire(ctx context.Context) (err error)
// Release the resource, never blocks.
Release()
}
Semaphore is the semaphore interface.