Documentation
¶
Overview ¶
Package xsync contains extensions to the standard library package sync.
Index ¶
- func Lazy[T any](f func() T) func() T
- type ContextCond
- type Future
- type Group
- func (g *Group) Once(f func(ctx context.Context))
- func (g *Group) Periodic(interval time.Duration, jitter time.Duration, f func(ctx context.Context))
- func (g *Group) PeriodicOrTrigger(interval time.Duration, jitter time.Duration, f func(ctx context.Context)) func()
- func (g *Group) Stop()
- func (g *Group) Trigger(f func(ctx context.Context)) func()
- func (g *Group) Wait()
- type Map
- 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) bool)
- func (m *Map[K, V]) Store(key K, value V)
- type Pool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Lazy ¶
func Lazy[T any](f func() T) func() T
Lazy makes a lazily-initialized value. On first access, it uses f to create the value. Later accesses all receive the same value.
Example ¶
var ( expensive = Lazy(func() string { fmt.Println("doing expensive init") return "foo" }) ) fmt.Println(expensive()) fmt.Println(expensive())
Output: doing expensive init foo foo
Types ¶
type ContextCond ¶
ContextCond is equivalent to sync.Cond, except its Wait function accepts a context.Context.
ContextConds should not be copied after first use.
func NewContextCond ¶
func NewContextCond(l sync.Locker) *ContextCond
NewContextCond returns a new ContextCond with l as its Locker.
func (*ContextCond) Broadcast ¶
func (c *ContextCond) Broadcast()
Broadcast wakes all goroutines blocked in Wait(), if there are any.
It is allowed but not required for the caller to hold c.L during the call.
func (*ContextCond) Signal ¶
func (c *ContextCond) Signal()
Signal wakes one goroutine blocked in Wait(), if there is any. No guarantee is made as to which goroutine will wake.
It is allowed but not required for the caller to hold c.L during the call.
func (*ContextCond) Wait ¶
func (c *ContextCond) Wait(ctx context.Context) error
Wait is equivalent to sync.Cond.Wait, except it accepts a context.Context. If the context expires before this goroutine is woken by Broadcast or Signal, it returns ctx.Err() immediately. If an error is returned, does not reaquire c.L before returning.
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future can be filled with a value exactly once. Many goroutines can concurrently wait for it to be filled. After filling, Wait() immediately returns the value it was filled with.
Futures must be created by NewFuture and should not be copied after first use.
func (*Future[T]) Fill ¶
func (f *Future[T]) Fill(x T)
Fill fills f with value x. All active calls to Wait return x, and all future calls to Wait return x immediately.
Panics if f has already been filled.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group manages a group of goroutines.
func NewGroup ¶
NewGroup returns a Group ready for use. The context passed to any of the f functions will be a descendant of ctx.
func (*Group) Periodic ¶
func (g *Group) Periodic( interval time.Duration, jitter time.Duration, f func(ctx context.Context), )
Periodic spawns a goroutine that calls f once per interval +/- jitter.
func (*Group) PeriodicOrTrigger ¶
func (g *Group) PeriodicOrTrigger( interval time.Duration, jitter time.Duration, f func(ctx context.Context), ) func()
PeriodicOrTrigger spawns a goroutine which calls f whenever the returned function is called. If f is already running when triggered, f will run again immediately when it finishes. Also calls f when it has been interval+/-jitter since the last trigger.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a typesafe wrapper over sync.Map.