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
GoWithDeadline 并发执行多个协程,并阻塞等待所有协程执行完毕或到达最后期限 与 Go 不同,本函数会阻塞当前协程直到所有 fn 执行完成或到达最后期限 @param deadline time.Time 最后期限,到达后停止等待 @param fns ...func() 待执行的协程函数
func GoWithTimeout ¶ added in v2.2.3
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 协程组实例(支持链式调用)