Documentation
¶
Overview ¶
Package flight collapses concurrent calls for the same key into one execution whose result every caller shares.
Index ¶
- Variables
- type Group
- func (g *Group[K, V]) Do(ctx context.Context, key K, fn func(context.Context) (V, error)) (V, error)
- func (g *Group[K, V]) DoDetached(ctx context.Context, key K, fn func(context.Context) (V, error)) (V, error)
- func (g *Group[K, V]) DoDetachedLimit(ctx context.Context, key K, max int, fn func(context.Context) (V, error)) (V, error)
- func (g *Group[K, V]) Has(key K) bool
- func (g *Group[K, V]) Len() int
Constants ¶
This section is empty.
Variables ¶
var ErrCapacity = errors.New("flight: capacity reached")
ErrCapacity means a new key was refused before its function started.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group[K comparable, V any] struct { // contains filtered or unexported fields }
Group runs at most one call per key at a time. The zero Group is ready.
func (*Group[K, V]) Do ¶
func (g *Group[K, V]) Do(ctx context.Context, key K, fn func(context.Context) (V, error)) (V, error)
Do shares fn among concurrent callers of key. The first caller runs fn on its own goroutine and context, so no work outlives a caller that owns it. Others wait; if the leader's context ended the call, a waiter whose own context is live runs fn itself instead of inheriting that cancellation. Callers share the returned value and must not mutate it.
func (*Group[K, V]) DoDetached ¶
func (g *Group[K, V]) DoDetached(ctx context.Context, key K, fn func(context.Context) (V, error)) (V, error)
DoDetached shares fn among concurrent callers of key. fn runs to completion on its own goroutine with a context that keeps the first caller's values but none of its cancellation, so one caller giving up never fails the others; fn must bound its own work. Every caller, including the first, returns ctx.Err() as soon as its context ends. Callers share the returned value and must not mutate it.
func (*Group[K, V]) DoDetachedLimit ¶ added in v0.2.48
func (g *Group[K, V]) DoDetachedLimit(ctx context.Context, key K, max int, fn func(context.Context) (V, error)) (V, error)
DoDetachedLimit is DoDetached with atomic admission of new keys. A positive max bounds the total keys in flight; zero or negative means unlimited. Existing-key waiters always join, even at capacity. Refused calls return ErrCapacity without running fn. Cancellation of a caller does not release capacity: the detached function owns its slot until it completes. Unrestricted Do/DoDetached calls do not enforce this per-call limit.