singleflight

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package singleflight provides a mechanism to suppress duplicate function calls. When multiple goroutines call Group.Do with the same key, only the first call executes the function; subsequent callers block and share the result.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group manages deduplicated function calls keyed by string.

func NewGroup

func NewGroup() *Group

NewGroup creates a new Group ready to track deduplicated calls.

func (*Group) Do

func (g *Group) Do(key string, fn func() (any, error)) (any, bool, error)

Do executes fn for the given key, ensuring that concurrent callers with the same key share the same result. Returns the value, whether this call shared the result with another in-flight call, and an error.

func (*Group) DoChan

func (g *Group) DoChan(key string, fn func() (any, error)) <-chan Result

DoChan is like Group.Do but returns a channel that receives the Result when the function completes.

func (*Group) Forget

func (g *Group) Forget(key string)

Forget discards the in-flight call for the given key, if any. Subsequent calls to Group.Do will execute the function again.

type Result

type Result struct {
	// Val is the value returned by the function.
	Val any
	// Err is the error returned by the function.
	Err error
	// Shared reports whether the result was shared with other callers.
	Shared bool
}

Result holds the outcome of a deduplicated function call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL