singleflight
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package singleflight provides a duplicate function call suppressionmechanism.
This is a Tailscale fork of Go's singleflight package which has had severalhomes in the past:
- https://github.com/golang/go/commit/61d3b2db6292581fc07a3767ec23ec94ad6100d1
- https://github.com/golang/groupcache/tree/master/singleflight
- https://pkg.go.dev/golang.org/x/sync/singleflight
This fork adds generics.
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]) Forget(key K)
- type Result
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeGroup¶
type Group[Kcomparable, Vany] struct {// contains filtered or unexported fields}
Group represents a class of work and forms a namespace inwhich units of work can be executed with duplicate suppression.
func (*Group[K, V])Do¶
Do executes and returns the results of the given function, makingsure that only one execution is in-flight for a given key at atime. If a duplicate comes in, the duplicate caller waits for theoriginal 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 theresults when they are ready.
The returned channel will not be closed.
func (*Group[K, V])DoChanContext¶added inv1.68.0
func (g *Group[K, V]) DoChanContext(ctxcontext.Context, key K, fn func(context.Context) (V,error)) <-chanResult[V]
DoChanContext is likeGroup.DoChan, but supports context cancelation. Thecontext passed to the fn function is a context that is canceled only whenthere are no callers waiting on a result (i.e. all callers have canceledtheir contexts).
The context that is passed to the fn function is not derived from any of theinput contexts, so context values will not be propagated. If context valuesare needed, they must be propagated explicitly.
The returned channel will not be closed. The Result.Err field is set to thecontext error if the context is canceled.