Documentation ¶
Index ¶
- func CtxGo(ctx context.Context, f func())
- func Go(f func())
- func Register(p *Pool) error
- type Config
- type Pool
- type TypedPool
- func (p *TypedPool) AdhocWorkerCount() int32
- func (p *TypedPool) AdhocWorkerLimit() int32
- func (p *TypedPool[T]) CtxGo(ctx context.Context, arg T)
- func (p *TypedPool[T]) Go(arg T)
- func (p *TypedPool) Name() string
- func (p *TypedPool) PermanentWorkerCount() int32
- func (p *TypedPool) SetAdhocWorkerLimit(limit int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // Name optionally specifies the name of a pool instance. Name string // New goroutine will be created if len(queued tasks) > ScaleThreshold, // it defaults to 0, which means always start a new adhoc worker before // reaching the limit of total adhoc worker number. ScaleThreshold int // PermanentWorkerNum specifies the number of permanent workers to spawn // when creating a Pool, it defaults to 0 (no permanent worker). // Note that a permanent worker's goroutine stack is reused, // the memory won't be freed in the entire program lifetime. // // Generally you may want to set this to zero for common workloads, // tweak it for special workloads which benefits from reusing goroutine stacks. PermanentWorkerNum int // AdhocWorkerLimit specifies the initial limit of adhoc workers, // 0 or negative value means no limit. // // The limit of adhoc worker number can be changed by calling // SetAdhocWorkerLimit. AdhocWorkerLimit int // PanicHandler specifies a handler when panic occurs. // By default, a panic message with stack information is logged. PanicHandler func(context.Context, any) }
Config is used to config a Pool instance.
type Pool ¶
type Pool = TypedPool[func()]
Pool manages a goroutine pool and tasks for better performance, it reuses goroutines and limits the number of goroutines.
func Default ¶
func Default() *Pool
Default returns the global default pool. The default pool does not enable permanent workers, the adhoc worker limit is configured to 10000. The package-level methods Go and CtxGo submit tasks to the default pool.
Note that it's not recommended to change the worker limit of the default pool, which affects other code that use the default pool.
type TypedPool ¶
type TypedPool[T any] struct { // contains filtered or unexported fields }
TypedPool is a task-specific pool. A TypedPool is like pool, but it executes a handler to process values of a specific type. Compared to Pool, it helps to reduce unnecessary memory allocation of closures when submitting tasks.
func NewTypedPool ¶
NewTypedPool creates a new task-specific pool with given handler and config.
func (*TypedPool) AdhocWorkerCount ¶
func (p *TypedPool) AdhocWorkerCount() int32
AdhocWorkerCount returns the number of running adhoc workers.
func (*TypedPool) AdhocWorkerLimit ¶
func (p *TypedPool) AdhocWorkerLimit() int32
AdhocWorkerLimit returns the current limit of adhoc workers.
func (*TypedPool) PermanentWorkerCount ¶
func (p *TypedPool) PermanentWorkerCount() int32
PermanentWorkerCount returns the number of permanent workers.
func (*TypedPool) SetAdhocWorkerLimit ¶
func (p *TypedPool) SetAdhocWorkerLimit(limit int)
SetAdhocWorkerLimit changes the limit of adhoc workers. 0 or negative value means no limit.