Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ErrorGroup ¶
type ErrorGroup struct {
// contains filtered or unexported fields
}
ErrorGroup 协程池的错误组,提供错误收集和快速失败能力
func NewErrorGroup ¶
func NewErrorGroup() *ErrorGroup
NewErrorGroup 使用默认协程池创建 ErrorGroup
Example ¶
示例:全局 ErrorGroup
// 使用全局默认协程池
eg := NewErrorGroup()
// 模拟批量处理,其中一个失败
eg.Go(func() error {
return nil // 成功
})
eg.Go(func() error {
return fmt.Errorf("处理失败")
})
// 等待所有任务完成
if err := eg.Wait(); err != nil {
fmt.Printf("批量处理失败: %v\n", err)
} else {
fmt.Println("所有处理成功")
}
Output: 批量处理失败: 处理失败
func NewErrorGroupWithContext ¶
func NewErrorGroupWithContext(ctx context.Context) (*ErrorGroup, context.Context)
NewErrorGroupWithContext 使用默认协程池创建带上下文的 ErrorGroup
func (*ErrorGroup) Context ¶
func (eg *ErrorGroup) Context() context.Context
Context 返回 ErrorGroup 的上下文
func (*ErrorGroup) GoWithContext ¶
GoWithContext 提交带上下文的任务到协程池并加入错误组
type Option ¶
type Option func(p *wokerpool)
func WithPanicHandler ¶
WithPanicHandler 设置 panic 处理函数
Example ¶
示例:自定义 panic 处理
pool := New(WithPanicHandler(func(taskName string, panicValue any, stack []byte) {
// 这里可以记录日志、发送告警等
// 为了示例输出的一致性,这里不打印 panic 信息
}))
defer pool.Close()
wg := pool.NewWaitGroup()
wg.Go(func() {
panic("模拟错误")
})
wg.Wait()
fmt.Println("即使发生 panic,程序仍然继续运行")
Output: 即使发生 panic,程序仍然继续运行
func WithSetCap ¶
type Pool ¶
type Pool interface {
// Go 提交一个任务到协程池执行
Go(f func())
// GoWithContext 提交带上下文的任务
GoWithContext(ctx context.Context, f func(ctx context.Context))
// NewWaitGroup 创建一个新的 WaitGroup,用于批量任务管理
NewWaitGroup() *WaitGroup
// NewErrorGroup 创建一个新的 ErrorGroup,用于错误收集和快速失败
NewErrorGroup() *ErrorGroup
// NewErrorGroupWithContext 创建带上下文的 ErrorGroup
NewErrorGroupWithContext(ctx context.Context) (*ErrorGroup, context.Context)
// Workers 返回当前活跃的 worker 数量
Workers() int32
// PendingTasks 返回待处理的任务数量
PendingTasks() int
// Close 优雅关闭协程池,等待所有任务完成
Close() error
// CloseWithTimeout 带超时的关闭协程池
CloseWithTimeout(timeout time.Duration) error
}
Pool 协程池接口,提供安全的协程执行能力
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup 协程池的等待组,提供批量任务管理能力
func NewWaitGroup ¶
func NewWaitGroup() *WaitGroup
NewWaitGroup 使用默认协程池创建 WaitGroup
Example ¶
示例:全局便捷函数
// 使用全局默认协程池创建 WaitGroup
wg := NewWaitGroup()
// 提交任务
wg.Go(func() {
fmt.Println("全局任务 1")
})
wg.Go(func() {
fmt.Println("全局任务 2")
})
// 等待完成
wg.Wait()
fmt.Println("全局任务完成")
func (*WaitGroup) GoWithContext ¶
GoWithContext 提交带上下文的任务到协程池并加入等待组
Example ¶
示例:带上下文的任务控制
pool := New()
defer pool.Close()
// 创建带超时的上下文
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
wg := pool.NewWaitGroup()
wg.GoWithContext(ctx, func(ctx context.Context) {
select {
case <-ctx.Done():
fmt.Println("任务被取消:", ctx.Err())
case <-time.After(time.Millisecond * 100):
fmt.Println("任务完成")
}
})
wg.Wait()
Output: 任务被取消: context deadline exceeded
Click to show internal directories.
Click to hide internal directories.