Movatterモバイル変換


[0]ホーム

URL:


singleflight

package
v1.92.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2025 License:BSD-3-ClauseImports:8Imported by:15

Details

Repository

github.com/tailscale/tailscale

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:

This fork adds generics.

Index

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

func (g *Group[K, V]) Do(key K, fn func() (V,error)) (v V, errerror, sharedbool)

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

func (g *Group[K, V]) DoChan(key K, fn func() (V,error)) <-chanResult[V]

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])DoChanContextadded 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.

func (*Group[K, V])Forget

func (g *Group[K, V]) Forget(key K)

Forget tells the singleflight to forget about a key. Future callsto Do for this key will call the function rather than waiting foran earlier call to complete.

typeResult

type Result[Vany] struct {Val    VErrerrorSharedbool}

Result holds the results of Do, so they can be passedon a channel.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp