Documentation
¶
Index ¶
- type Group
- func (g *Group[K, V]) Do(key K, fn func() (V, error)) (v V, err error, shared bool)
- func (g *Group[K, V]) DoChan(key K, fn func() (V, error)) <-chan Result[V]
- func (g *Group[K, V]) DoChanContext(ctx context.Context, key K, fn func(context.Context) (V, error)) <-chan Result[V]
- func (g *Group[K, V]) DoChanX(keys []K, fn func([]K) (map[K]V, error)) map[K]chan Result[V]
- func (g *Group[K, V]) DoChanXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]chan Result[V]
- func (g *Group[K, V]) DoContext(ctx context.Context, key K, fn func(context.Context) (V, error)) (v V, err error, shared bool)
- func (g *Group[K, V]) DoX(keys []K, fn func([]K) (map[K]V, error)) (results map[K]Result[V])
- func (g *Group[K, V]) DoXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]Result[V]
- func (g *Group[K, V]) Forget(key K)
- func (g *Group[K, V]) ForgetX(keys []K)
- type Hasher
- type NullValue
- type Result
- type ShardedGroup
- func (sg *ShardedGroup[K, V]) Do(key K, fn func() (V, error)) (v V, err error, shared bool)
- func (sg *ShardedGroup[K, V]) DoChan(key K, fn func() (V, error)) <-chan Result[V]
- func (sg *ShardedGroup[K, V]) DoChanContext(ctx context.Context, key K, fn func(context.Context) (V, error)) <-chan Result[V]
- func (sg *ShardedGroup[K, V]) DoChanX(keys []K, fn func([]K) (map[K]V, error)) map[K]chan Result[V]
- func (sg *ShardedGroup[K, V]) DoChanXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]chan Result[V]
- func (sg *ShardedGroup[K, V]) DoContext(ctx context.Context, key K, fn func(context.Context) (V, error)) (v V, err error, shared bool)
- func (sg *ShardedGroup[K, V]) DoX(keys []K, fn func([]K) (map[K]V, error)) (results map[K]Result[V])
- func (sg *ShardedGroup[K, V]) DoXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]Result[V]
- func (sg *ShardedGroup[K, V]) Forget(key K)
- func (sg *ShardedGroup[K, V]) ForgetX(keys []K)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group[K comparable, V any] struct { // contains filtered or unexported fields }
Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.
func (*Group[K, V]) Do ¶
Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers.
func (*Group[K, V]) DoChan ¶
DoChan is like Do but returns a channel that will receive the results when they are ready.
The returned channel will not be closed.
func (*Group[K, V]) DoChanContext ¶ added in v1.0.0
func (g *Group[K, V]) DoChanContext(ctx context.Context, key K, fn func(context.Context) (V, error)) <-chan Result[V]
DoChanContext is like DoChan, but ctx bounds only this caller's channel: if ctx is done before fn returns, the returned channel receives ctx's error instead of fn's result. See DoContext for how fn's own cancellation is derived.
The returned channel will not be closed.
func (*Group[K, V]) DoChanX ¶
DoChanX is like Do but returns a channel that will receive the results when they are ready.
The returned channel will not be closed.
func (*Group[K, V]) DoChanXContext ¶ added in v1.0.0
func (g *Group[K, V]) DoChanXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]chan Result[V]
DoChanXContext is like DoChanX, but ctx bounds the wait for every key in this call: once ctx is done, the channels for keys still in flight receive ctx's error instead of fn's result. See DoXContext for how fn's own cancellation is derived.
The returned channels will not be closed.
func (*Group[K, V]) DoContext ¶ added in v1.0.0
func (g *Group[K, V]) DoContext(ctx context.Context, key K, fn func(context.Context) (V, error)) (v V, err error, shared bool)
DoContext is like Do, but the wait is bounded by ctx: if ctx is done before fn returns, DoContext returns immediately with ctx's error and shared reports whether any other caller was also attached to this key at that moment.
fn itself is not necessarily interrupted: it keeps running for every other caller still attached to the same key. It only receives a canceled context once every caller of this key — across every variant, context-aware or not — has stopped waiting. That context carries the values (but never the cancellation) of whichever caller happened to start it, since fn is shared and must not depend on a single caller's deadline or lifetime.
A plain Do call joining the same key never leaves early, so it prevents fn from ever being interrupted while it is attached.
func (*Group[K, V]) DoX ¶
DoX executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers. Even if fn does not return V on some keys, the results map will contain those keys with a `Valid` field set to false.
func (*Group[K, V]) DoXContext ¶ added in v1.0.0
func (g *Group[K, V]) DoXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]Result[V]
DoXContext is like DoX, but ctx bounds the wait for every key in this call. Once ctx is done, keys still in flight get ctx's error immediately; keys that already completed keep their real result. See DoContext for how fn's own cancellation is derived — for a batch, fn shares one context across every key it is asked to resolve, canceled only once every caller of every key in the batch has stopped waiting.
type Hasher ¶ added in v0.2.0
Hasher is responsible for generating unsigned, 16 bit hash of provided key. Hasher should minimize collisions. For great performance, a fast function is preferable.
type ShardedGroup ¶ added in v0.2.0
type ShardedGroup[K comparable, V any] struct { // contains filtered or unexported fields }
ShardedGroup is a duplicate of singleflight.Group, but with the ability to shard the map of calls.
func NewShardedGroup ¶ added in v0.2.0
func NewShardedGroup[K comparable, V any](count uint, hasher Hasher[K]) *ShardedGroup[K, V]
func (*ShardedGroup[K, V]) Do ¶ added in v0.2.0
func (sg *ShardedGroup[K, V]) Do(key K, fn func() (V, error)) (v V, err error, shared bool)
Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers. Even if fn does not return V on some keys, the results map will contain those keys with a `Valid` field set to false.
func (*ShardedGroup[K, V]) DoChan ¶ added in v0.2.0
func (sg *ShardedGroup[K, V]) DoChan(key K, fn func() (V, error)) <-chan Result[V]
DoChan is like Do but returns a channel that will receive the results when they are ready.
The returned channel will not be closed.
func (*ShardedGroup[K, V]) DoChanContext ¶ added in v1.0.0
func (sg *ShardedGroup[K, V]) DoChanContext(ctx context.Context, key K, fn func(context.Context) (V, error)) <-chan Result[V]
DoChanContext routes to the shard responsible for key. See Group.DoChanContext.
The returned channel will not be closed.
func (*ShardedGroup[K, V]) DoChanX ¶ added in v0.2.0
func (sg *ShardedGroup[K, V]) DoChanX(keys []K, fn func([]K) (map[K]V, error)) map[K]chan Result[V]
DoChanX is like Do but returns a channel that will receive the results when they are ready.
The returned channel will not be closed.
func (*ShardedGroup[K, V]) DoChanXContext ¶ added in v1.0.0
func (sg *ShardedGroup[K, V]) DoChanXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]chan Result[V]
DoChanXContext routes keys to their shards, calling fn once per shard they span. See Group.DoChanXContext.
The returned channels will not be closed.
func (*ShardedGroup[K, V]) DoContext ¶ added in v1.0.0
func (sg *ShardedGroup[K, V]) DoContext(ctx context.Context, key K, fn func(context.Context) (V, error)) (v V, err error, shared bool)
DoContext routes to the shard responsible for key. See Group.DoContext.
func (*ShardedGroup[K, V]) DoX ¶ added in v0.2.0
func (sg *ShardedGroup[K, V]) DoX(keys []K, fn func([]K) (map[K]V, error)) (results map[K]Result[V])
DoX executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers.
func (*ShardedGroup[K, V]) DoXContext ¶ added in v1.0.0
func (sg *ShardedGroup[K, V]) DoXContext(ctx context.Context, keys []K, fn func(context.Context, []K) (map[K]V, error)) map[K]Result[V]
DoXContext routes keys to their shards, calling fn once per shard they span, and reports each key's result the same way Group.DoXContext does — except a panic or runtime.Goexit in fn is always delivered as a Result here (as DoChanXContext already does), never re-raised in the caller's own goroutine, since this method is built on the channel-based DoChanXContext.
func (*ShardedGroup[K, V]) Forget ¶ added in v0.3.0
func (sg *ShardedGroup[K, V]) Forget(key K)
Forget tells the singleflight to forget about a key. Future calls to Do for this key will call the function rather than waiting for an earlier call to complete.
func (*ShardedGroup[K, V]) ForgetX ¶ added in v0.3.0
func (sg *ShardedGroup[K, V]) ForgetX(keys []K)
ForgetX tells the singleflight to forget about many keys. Future calls to Do for this key will call the function rather than waiting for an earlier call to complete.