Documentation
¶
Overview ¶
Package pooler implements a worker-pool paradigm, relying on channels for all goroutine interoperation in order to achieve high speed an thread-safety. The only other dependency is the sync/atomic package, but it's kept down to a minimum, because we want pooler's operation to be as non-blocking as possible.
Index ¶
- type CallbackFuncQueue
- type CallbackFuncQueueErr
- type CallbackFuncTask
- type CallbackFuncTaskErr
- type CallbackFuncWrk
- type Config
- func (c *Config) OnTaskCrashed(fn CallbackFuncTaskErr) *Config
- func (c *Config) OnTaskDone(fn CallbackFuncTask) *Config
- func (c *Config) OnTaskDoneWithError(fn CallbackFuncTaskErr) *Config
- func (c *Config) OnTaskQueued(fn CallbackFuncQueue) *Config
- func (c *Config) OnTaskQueuingError(fn CallbackFuncQueueErr) *Config
- func (c *Config) OnTaskStarted(fn CallbackFuncTask) *Config
- func (c *Config) OnWorkerCreated(fn CallbackFuncWrk) *Config
- func (c *Config) OnWorkerShutdown(fn CallbackFuncWrk) *Config
- type Pool
- func (p *Pool) ActiveTasks() int64
- func (p *Pool) ActiveWorkers() int64
- func (p *Pool) ConfiguredRoutines() int64
- func (p *Pool) Enqueue(task Runnable) error
- func (p *Pool) IsShuttingDown() bool
- func (p *Pool) MaxTasks() int64
- func (p *Pool) PrepareToWait()
- func (p *Pool) QueueLen() int
- func (p *Pool) Resize(newGoroutines int64) error
- func (p *Pool) Shutdown()
- func (p *Pool) ShutdownWithTimeout(timeout time.Duration) bool
- func (p *Pool) Wait()
- type Runnable
- type Task
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallbackFuncQueue ¶ added in v1.0.0
type CallbackFuncQueue func(task *Task)
CallbackFuncQueue is the prototype of a function that will be called by the pool to notify of successful events pertaining the queue (like a task successfully enqueued, for example).
type CallbackFuncQueueErr ¶ added in v1.0.0
CallbackFuncQueueErr is the prototype of a function that will be called by the pool to notify of errors pertaining the queue (queuing errors).
type CallbackFuncTask ¶ added in v1.0.0
CallbackFuncTask is the prototype of a function that will be called by the pool to notify when workers start/stop tasks.
type CallbackFuncTaskErr ¶ added in v1.0.0
CallbackFuncTaskErr is the prototype of a function that will be called by the pool to notify of errors pertaining tasks (typically runtime errors).
type CallbackFuncWrk ¶ added in v1.0.0
type CallbackFuncWrk func(routine int)
CallbackFuncWrk is the prototype of a function that will be called by the pool to notify when workers are created or shutdown.
type Config ¶ added in v1.0.0
type Config struct {
// Routines is the desired number of "worker" goroutines
Routines atomic.Int64
// MaxTasks is the maximum number of tasks that can be in this pool's queue at any given time
MaxTasks atomic.Int64
// WorkerCreatedCB is an optional callback func that will be called every time a "worker" goroutine is created
WorkerCreatedCB CallbackFuncWrk
// WorkerShutdownCB is an optional callback func that will be called every time a "worker" goroutine is shutdown
WorkerShutdownCB CallbackFuncWrk
// TaskQueuedCB is an optional callback func that will be called every time a task is successfully added to the pool's queue
TaskQueuedCB CallbackFuncQueue
// TaskQueuingErrorCB is an optional callback func that will be called every time there's a problem adding a task to the pool's queue
TaskQueuingErrorCB CallbackFuncQueueErr
// TaskStartedCB is an optional callback func that will be called every time a task is picked up by a "worker" routine and its execution begins
TaskStartedCB CallbackFuncTask
// TaskDoneCB is an optional callback func that will be called every time a task is done running without errors
TaskDoneCB CallbackFuncTask
// TaskDoneWithErrorCB is an optional callback func that will be called every time a task is done running but has returned an error
TaskDoneWithErrorCB CallbackFuncTaskErr
// TaskCrashedCB is an optional callback func that will be called every time a `panic` has occurred within the Run() method while a task was running
TaskCrashedCB CallbackFuncTaskErr
}
Config is the global pool configuration struct.
func NewConfig ¶ added in v1.0.0
NewConfig creates and returns a basic/initial Config struct, with a specified numer of worker `routines` and a specified maximum number of queueable `maxTasks`.
func (*Config) OnTaskCrashed ¶ added in v1.0.0
func (c *Config) OnTaskCrashed(fn CallbackFuncTaskErr) *Config
OnTaskCrashed sets the callback function that's called when a "worker" goroutine suddenly crashed (panic) while running a task.
func (*Config) OnTaskDone ¶ added in v1.0.0
func (c *Config) OnTaskDone(fn CallbackFuncTask) *Config
OnTaskDone sets the callback function that's called when a "worker" goroutine is done running a task, and no error is returned.
func (*Config) OnTaskDoneWithError ¶ added in v1.0.0
func (c *Config) OnTaskDoneWithError(fn CallbackFuncTaskErr) *Config
OnTaskDoneWithError sets the callback function that's called when a "worker" goroutine is done running a task, but an error is returned.
func (*Config) OnTaskQueued ¶ added in v1.0.0
func (c *Config) OnTaskQueued(fn CallbackFuncQueue) *Config
OnTaskQueued sets the callback function that's called when a new task is successfully added to the pending queue.
func (*Config) OnTaskQueuingError ¶ added in v1.0.0
func (c *Config) OnTaskQueuingError(fn CallbackFuncQueueErr) *Config
OnTaskQueuingError sets the callback function that's called when a "worker" goroutine is done running a task, but an error is returned.
func (*Config) OnTaskStarted ¶ added in v1.0.0
func (c *Config) OnTaskStarted(fn CallbackFuncTask) *Config
OnTaskStarted sets the callback function that's called when a "worker" goroutine picks up a task from the queue and starts running it.
func (*Config) OnWorkerCreated ¶ added in v1.0.0
func (c *Config) OnWorkerCreated(fn CallbackFuncWrk) *Config
OnWorkerCreated sets the callback function that's called when a new "worker" goroutine is created.
func (*Config) OnWorkerShutdown ¶ added in v1.0.0
func (c *Config) OnWorkerShutdown(fn CallbackFuncWrk) *Config
OnWorkerShutdown sets the callback function that's called when a new "worker" goroutine is shutdown.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a container for a pool of goroutines that will run the queued tasks.
func New ¶
New creates a new pooler.Pool object without any Callback functions. `routines` is the maximum number of "worker" goroutines that are allowed to run concurrently. `maxTasks` is the maximum number of tasks that can be waiting in line to be executed by the next available goroutine.
func NewWithConfig ¶ added in v1.0.0
NewWithConfig creates a new pooler.Pool object with a user-provided configuration. `config` is a pointer to a pooler.Config object (see types.go).
func (*Pool) ActiveTasks ¶ added in v1.0.3
ActiveTasks retuns the number of tasks that are REALLY being executed at this time.
func (*Pool) ActiveWorkers ¶
ActiveWorkers returns the number of running goroutines, including the ones that are idle.
func (*Pool) ConfiguredRoutines ¶ added in v1.0.3
ConfiguredRoutines returns the number of configured goroutines in a thread-safe way
func (*Pool) Enqueue ¶
Enqueue adds a task to the queue of tasks waiting to be executed. `task` can be any object that implements the pooler.Runnable interface (see types.go).
func (*Pool) IsShuttingDown ¶
IsShuttingDown returns false during normal operation and true if the pool is shutting down; all tasks should periodically check it inside of their "Run" func.
func (*Pool) MaxTasks ¶ added in v1.0.3
MaxTasks returns the maximum number of queueable tasks in a thread-safe way
func (*Pool) PrepareToWait ¶ added in v1.0.7
func (p *Pool) PrepareToWait()
func (*Pool) QueueLen ¶
QueueLen returns the number of tasks currently queued, and waiting to be executed.
func (*Pool) Resize ¶ added in v1.0.3
Resize attempts to resize the pool, adding or terminating goroutines as needed. It returns an error if resizing conditions aren't met.
func (*Pool) Shutdown ¶
func (p *Pool) Shutdown()
Shutdown stops all goroutines running all tasks, and shuts down the entire pool. Please note that this method could actually wait forever untill all pending tasks are done.
func (*Pool) ShutdownWithTimeout ¶ added in v1.0.1
ShutdownWithTimeout stops all goroutines running all tasks, and shuts down the entire pool. It doesn't wait forever, and it always returns on or before `timeout`. It returns true if it times out, and false if it shuts down regularly (before timeout occurs). Please note that if this function returns true (a timeout has occurred) you may still have "orphan" goroutines running; it is, therefore, recommended that this is the among the last methods you call just before your program terminates.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
with_callback
command
|
|
|
with_callback_and_customdata
command
|
|
|
with_resizing
command
|