pool

package
v0.5.10 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cap

func Cap() int32

Cap 获取协程池容量

func ForEach

func ForEach[T any](ctx context.Context, items []T, fn func(T) error, opts ...BatchOption) error

ForEach 并行 ForEach 操作

func Free

func Free() int32

Free 获取空闲的 worker 数量

func GlobalPool

func GlobalPool() *poolx.Pool

GlobalPool 获取全局协程池

func Map

func Map[T, R any](ctx context.Context, items []T, fn func(T) (R, error), opts ...BatchOption) ([]R, error)

Map 并行 Map 操作 使用协程池并发执行,结果保持输入顺序

func Metrics

func Metrics() poolx.MetricsSnapshot

Metrics 获取协程池指标

func Parallel

func Parallel(fns ...func())

Parallel 并行执行多个任务

func Running

func Running() int32

Running 获取正在运行的 worker 数量

func Submit

func Submit(fn func()) error

Submit 提交任务到全局协程池

func SubmitWait

func SubmitWait(fn func()) error

SubmitWait 提交任务并等待完成

func SubmitWithContext

func SubmitWithContext(ctx context.Context, fn func(context.Context)) error

SubmitWithContext 提交接收 context 的协作式任务

func TrySubmit

func TrySubmit(fn func()) bool

TrySubmit 尝试提交任务(非阻塞)

func Waiting

func Waiting() int32

Waiting 获取等待中的任务数量

Types

type BatchConfig

type BatchConfig struct {
	// MaxConcurrency 最大并发数(0 表示使用 CPU 核心数)
	MaxConcurrency int

	// StopOnError 遇到错误时是否停止
	StopOnError bool
}

BatchConfig 批量执行配置

func DefaultBatchConfig

func DefaultBatchConfig() *BatchConfig

DefaultBatchConfig 默认批量配置

type BatchOption

type BatchOption func(*BatchConfig)

BatchOption 批量执行选项

func WithMaxConcurrency

func WithMaxConcurrency(n int) BatchOption

WithMaxConcurrency 设置最大并发数

func WithStopOnError

func WithStopOnError(stop bool) BatchOption

WithStopOnError 设置遇到错误时是否停止

type BufferPool

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

BufferPool 缓冲池

func LargeBuffer

func LargeBuffer() *BufferPool

LargeBuffer 获取大缓冲 (1MB)

func MediumBuffer

func MediumBuffer() *BufferPool

MediumBuffer 获取中缓冲 (64KB)

func NewBufferPool

func NewBufferPool(initialSize int) *BufferPool

NewBufferPool 创建缓冲池

func SmallBuffer

func SmallBuffer() *BufferPool

SmallBuffer 获取小缓冲 (4KB)

func (*BufferPool) Get

func (p *BufferPool) Get() []byte

Get 获取缓冲

func (*BufferPool) Put

func (p *BufferPool) Put(b []byte)

Put 归还缓冲

type BytePool

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

BytePool 字节切片池

func NewBytePool

func NewBytePool(size int) *BytePool

NewBytePool 创建字节池

func (*BytePool) Get

func (p *BytePool) Get() []byte

Get 获取字节切片

func (*BytePool) Put

func (p *BytePool) Put(b []byte)

Put 归还字节切片

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 创建对象池

func (*ObjectPool[T]) Get

func (p *ObjectPool[T]) Get() T

Get 获取对象

func (*ObjectPool[T]) Put

func (p *ObjectPool[T]) Put(obj T)

Put 归还对象

Jump to

Keyboard shortcuts

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