pool

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 9 Imported by: 0

README

pool

pool 是一个有界 goroutine 池。它不追求花哨调度策略,核心目标是把并发上限、队列背压、panic 隔离和关闭语义做干净。

设计原则

  • worker 数固定,队列有界,容量语义明确。
  • 默认阻塞提交,优先把背压还给调用方,而不是静默丢任务。
  • 需要取消或超时控制时,显式使用 SubmitContext
  • worker panic 不会炸掉整个池;可以走自定义 PanicHandler 或结构化日志。
  • Release 幂等,且会等待 worker 退出。

快速开始

pool, err := pool.New(
    8,
    pool.WithQueueSize(32),
    pool.WithNonBlocking(false),
)
if err != nil {
    panic(err)
}
defer pool.Release()

if err := pool.SubmitContext(context.Background(), func() {
    fmt.Println("run task")
}); err != nil {
    panic(err)
}

API 摘要

type Pool interface {
    Submit(func()) error
    SubmitContext(context.Context, func()) error
    Release()
    Running() int
    Pending() int
    Cap() int
    IsClosed() bool
}

func New(size int, opts ...Option) (Pool, error)
func WithPanicHandler(func(interface{})) Option
func WithLogger(*logger.Logger) Option
func WithNonBlocking(bool) Option
func WithQueueSize(int) Option

默认行为

  • Submit 使用 context.Background();如果你需要取消和超时,请直接用 SubmitContext
  • SubmitContext 现在要求非 nil context
  • 默认队列长度等于池大小
  • WithNonBlocking(true) 时,队列满会直接返回 ErrPoolFull
  • 没有自定义 panic handler 时,panic 会记日志但不会中断其他 worker

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrContextRequired = errors.New("pool: context is required")
	ErrPoolClosed      = errors.New("pool: closed")
	ErrPoolFull        = errors.New("pool: full")
	ErrNilTask         = errors.New("pool: task is required")
)

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option defines a functional option for the Pool.

func WithLogger

func WithLogger(l *logger.Logger) Option

WithLogger sets the logger for the pool to log panics if no PanicHandler is set.

func WithNonBlocking

func WithNonBlocking(b bool) Option

WithNonBlocking makes Submit return immediately with ErrPoolFull if the pool (queue) is full. Default is false (blocking).

func WithPanicHandler

func WithPanicHandler(h func(interface{})) Option

WithPanicHandler sets a callback for when a worker panics. If set, this handler is called with the recover() result.

func WithQueueSize

func WithQueueSize(size int) Option

WithQueueSize sets the maximum number of queued tasks. If 0 or negative, it defaults to the pool size.

type Pool

type Pool interface {
	Submit(task func()) error
	SubmitContext(context.Context, func()) error
	Release()
	Running() int
	Pending() int
	Cap() int
	IsClosed() bool
}

func New

func New(size int, opts ...Option) (Pool, error)

Jump to

Keyboard shortcuts

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