Documentation
¶
Overview ¶
Package gopool is a high-performance goroutine pool which aims to reuse goroutines and limit the number of goroutines.
Index ¶
- func CtxGo(ctx context.Context, f func())
- func Go(f func())
- 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(queuedTasks) >= ScaleThreshold, // it defaults to 1, 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.
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.