singleflight

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package singleflight 提供重复函数调用抑制机制

Index

Constants

This section is empty.

Variables

View Source
var ErrForgotten = errors.New("call was forgotten")

ErrForgotten 表示调用被忘记

Functions

This section is empty.

Types

type GenericGroup

type GenericGroup[T any] struct {
	// contains filtered or unexported fields
}

GenericGroup 泛型版本的 singleflight

func NewGenericGroup

func NewGenericGroup[T any]() *GenericGroup[T]

NewGenericGroup 创建泛型 singleflight

func (*GenericGroup[T]) Do

func (g *GenericGroup[T]) Do(key string, fn func() (T, error)) (T, error, bool)

Do 执行并返回结果

func (*GenericGroup[T]) Forget

func (g *GenericGroup[T]) Forget(key string)

Forget 忘记一个 key

type GenericResult

type GenericResult[T any] struct {
	Val    T
	Err    error
	Shared bool
}

GenericResult 泛型结果

type Group

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

Group 表示一类工作的 singleflight 组

func (*Group) Do

func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool)

Do 执行给定函数并返回结果,确保同一时间 对于同一个 key 只有一个执行在进行中。 如果有重复调用,调用者会等待原始调用完成并收到相同的结果。

func (*Group) DoChan

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

DoChan 类似 Do,但返回一个 channel,当结果就绪时会接收到值

func (*Group) DoContext

func (g *Group) DoContext(ctx context.Context, key string, fn func(ctx context.Context) (interface{}, error)) (v interface{}, err error, shared bool)

DoContext 带有 context 支持的 Do

func (*Group) Forget

func (g *Group) Forget(key string)

Forget 告诉 singleflight 忘记一个 key, 之后的同 key 调用将执行函数而不是等待之前的调用完成

type Result

type Result struct {
	Val    interface{}
	Err    error
	Shared bool
}

Result 保存 Do 的结果

Jump to

Keyboard shortcuts

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