xcall

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 5 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff added in v2.6.0

func Backoff(ctx context.Context, fn func(ctx context.Context, attempt int) (bool, error), retry int, baseDelay, maxDelay time.Duration) error

Backoff 指数退避调用函数 按指数递增的间隔重试调用 fn:第 i 次尝试的间隔为 1<<i 倍的基础延迟(封顶为 maxDelay), 直到 fn 返回 next=false、ctx 被取消或达到最大重试次数 retry @param ctx context.Context 上下文 @param fn func(ctx context.Context, attempt int) (bool, error) 待调用的函数,attempt 为当前尝试次数(从1开始),返回值 next 表示是否继续重试 @param retry int 最大重试次数 @param baseDelay time.Duration 基础延迟时间 @param maxDelay time.Duration 最大延迟时间 @return @1 error 最后一次调用返回的错误;若 ctx 被取消,则返回 ctx.Err()

func Call

func Call[T funcType](fn T) error

Call 安全地调用函数 捕获函数执行过程中产生的 panic:运行时错误(runtime.Error)记录为致命错误,其他 panic 记录错误信息 @param fn func() 待调用的函数

func Go added in v2.1.0

func Go[T funcType](fn T)

Go 执行单个协程 将函数放入新的协程中执行,并自动捕获 panic,避免协程崩溃导致整个进程退出 @param fn func() 待执行的函数

func GoWithDeadline added in v2.2.3

func GoWithDeadline(deadline time.Time, fns ...func())

GoWithDeadline 并发执行多个协程,并阻塞等待所有协程执行完毕或到达最后期限 与 Go 不同,本函数会阻塞当前协程直到所有 fn 执行完成或到达最后期限 @param deadline time.Time 最后期限,到达后停止等待 @param fns ...func() 待执行的协程函数

func GoWithTimeout added in v2.2.3

func GoWithTimeout(timeout time.Duration, fns ...func())

GoWithTimeout 并发执行多个协程,并阻塞等待所有协程执行完毕或超时 与 Go 不同,本函数会阻塞当前协程直到所有 fn 执行完成或达到超时时间 @param timeout time.Duration 整体执行的超时时间 @param fns ...func() 待执行的协程函数

Types

type Goroutines added in v2.2.3

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

Goroutines 协程组,用于批量管理需要并发执行的协程函数

func NewGoroutines added in v2.2.3

func NewGoroutines() *Goroutines

NewGoroutines 创建一个协程组实例 @return @1 *Goroutines 协程组实例

func (*Goroutines) Add added in v2.2.3

func (g *Goroutines) Add(fns ...func()) *Goroutines

Add 添加协程函数 @param fns ...func() 待添加的协程函数 @return @1 *Goroutines 协程组实例(支持链式调用)

func (*Goroutines) Run added in v2.2.3

func (g *Goroutines) Run(ctx context.Context, timeout ...time.Duration)

Run 运行协程函数,所有函数在独立的协程中并发执行 执行完毕后会清空已添加的协程函数,以便实例可被复用 未指定超时时间时会等待所有协程执行完毕; 指定超时时间后,若超时则提前返回,不再等待剩余协程 @param ctx context.Context 上下文 @param timeout ...time.Duration 可选,整体执行的超时时间

Jump to

Keyboard shortcuts

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