grpool

package
v2.10.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 10 Imported by: 150

Documentation

Overview

Package grpool implements a goroutine reusable pool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(ctx context.Context, f Func) error

Add pushes a new job to the default goroutine pool. The job will be executed asynchronously.

func AddWithRecover

func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error

AddWithRecover pushes a new job to the default pool with specified recover function.

The optional `recoverFunc` is called when any panic during executing of `userFunc`. If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`. The job will be executed asynchronously.

func IsPaused added in v2.10.3

func IsPaused() bool

IsPaused returns whether the default pool is paused.

func Jobs

func Jobs() int

Jobs returns current job count of default goroutine pool.

func Pause added in v2.10.3

func Pause() bool

Pause pauses the default pool work.

func Resume added in v2.10.3

func Resume() bool

Resume resumes the default pool work.

func Size

func Size() int

Size returns current goroutine count of default goroutine pool.

Types

type Func

type Func func(ctx context.Context)

Func is the pool function which contains context parameter.

type LimitChangerFunc added in v2.10.3

type LimitChangerFunc func(ctx context.Context, value *atomic.Int64) (changed bool)

LimitChangerFunc updates the pool's goroutine limit dynamically. It should update value and return true if the limit was changed.

type Pool

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

Pool manages the goroutines using pool.

func New

func New(limit ...int) *Pool

New creates and returns a new goroutine pool object. The parameter `limit` is used to limit the max goroutine count, which is not limited in default.

func NewWithOption added in v2.10.3

func NewWithOption(option ...PoolOption) *Pool

NewWithOption creates and returns a new goroutine pool object. The parameter `option` is used to limit the max goroutine count or set limit changer, which is not limited in default.

func (*Pool) Add

func (p *Pool) Add(ctx context.Context, f Func) error

Add pushes a new job to the pool. The job will be executed asynchronously.

func (*Pool) AddWithRecover

func (p *Pool) AddWithRecover(ctx context.Context, userFunc Func, recoverFunc RecoverFunc) error

AddWithRecover pushes a new job to the pool with specified recover function.

The optional `recoverFunc` is called when any panic during executing of `userFunc`. If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`. The job will be executed asynchronously.

func (*Pool) Cap

func (p *Pool) Cap() int

Cap returns the capacity of the pool. This capacity is defined when pool is created. It returns -1 if there's no limit.

func (*Pool) ClearJobs added in v2.10.3

func (p *Pool) ClearJobs() (count int)

ClearJobs clears all queued jobs and returns how many were cleared.

func (*Pool) Close

func (p *Pool) Close()

Close closes the goroutine pool, which makes all goroutines exit.

func (*Pool) IsClosed

func (p *Pool) IsClosed() bool

IsClosed returns if pool is closed.

func (*Pool) IsPaused added in v2.10.3

func (p *Pool) IsPaused() bool

IsPaused returns whether the pool is paused.

func (*Pool) Jobs

func (p *Pool) Jobs() int

Jobs returns current job count of the pool. Note that, it does not return worker/goroutine count but the job/task count.

func (*Pool) Pause added in v2.10.3

func (p *Pool) Pause() bool

Pause pauses pool work. Jobs are kept in the queue and will be processed when the pool resumes.

func (*Pool) Resume added in v2.10.3

func (p *Pool) Resume() bool

Resume resumes pool work.

func (*Pool) SetCap added in v2.10.3

func (p *Pool) SetCap(cap int) int

SetCap changes the capacity and returns the pool capacity before the change. It returns -1 if there's no limit.

func (*Pool) Size

func (p *Pool) Size() int

Size returns current goroutine count of the pool.

type PoolOption added in v2.10.3

type PoolOption struct {
	Limit        int              // Max goroutine count limit.
	LimitChanger LimitChangerFunc // Function used to change max goroutine count limit. Let it nil to disable.
}

PoolOption is used to pass options for creating a Pool.

type RecoverFunc added in v2.1.0

type RecoverFunc func(ctx context.Context, exception error)

RecoverFunc is the pool runtime panic recover function which contains context parameter.

Jump to

Keyboard shortcuts

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