Documentation
¶
Overview ¶
Package pool 提供 Hexagon 框架的协程池和对象池封装
本包封装了 toolkit/util/poolx,提供:
- 全局协程池管理
- 并发任务执行
- 对象池复用
使用示例:
// 使用全局协程池
pool.Submit(func() {
// 任务逻辑
})
// 并行 Map
results, err := pool.Map(ctx, items, func(item T) (R, error) {
return process(item), nil
})
Index ¶
- func Cap() int32
- func ForEach[T any](ctx context.Context, items []T, fn func(T) error, opts ...BatchOption) error
- func Free() int32
- func GlobalPool() *poolx.Pool
- func Map[T, R any](ctx context.Context, items []T, fn func(T) (R, error), opts ...BatchOption) ([]R, error)
- func Metrics() poolx.MetricsSnapshot
- func Parallel(fns ...func())
- func Running() int32
- func Submit(fn func()) error
- func SubmitWait(fn func()) error
- func SubmitWithContext(ctx context.Context, fn func(context.Context)) error
- func TrySubmit(fn func()) bool
- func Waiting() int32
- type BatchConfig
- type BatchOption
- type BufferPool
- type BytePool
- type ObjectPool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Map ¶
func Map[T, R any](ctx context.Context, items []T, fn func(T) (R, error), opts ...BatchOption) ([]R, error)
Map 并行 Map 操作 使用协程池并发执行,结果保持输入顺序
func SubmitWithContext ¶
SubmitWithContext 提交接收 context 的协作式任务
Types ¶
type BatchConfig ¶
type BatchConfig struct {
// MaxConcurrency 最大并发数(0 表示使用 CPU 核心数)
MaxConcurrency int
// StopOnError 遇到错误时是否停止
StopOnError bool
}
BatchConfig 批量执行配置
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool 缓冲池
type ObjectPool ¶
type ObjectPool[T any] struct { // contains filtered or unexported fields }
ObjectPool 泛型对象池 封装 toolkit 的 ObjectPool
func NewObjectPool ¶
func NewObjectPool[T any](factory func() T, reset func(*T)) (*ObjectPool[T], error)
NewObjectPool 创建对象池
Click to show internal directories.
Click to hide internal directories.