Documentation ¶
Index ¶
- Variables
- type Option
- type ResizingStrategy
- type TaskGroup
- type TaskGroupWithContext
- type WorkerPool
- func (p *WorkerPool) CompletedTasks() uint64
- func (p *WorkerPool) FailedTasks() uint64
- func (p *WorkerPool) Group() *TaskGroup
- func (p *WorkerPool) GroupContext(ctx context.Context) (*TaskGroupWithContext, context.Context)
- func (p *WorkerPool) IdleWorkers() int
- func (p *WorkerPool) MaxCapacity() int
- func (p *WorkerPool) MaxWorkers() int
- func (p *WorkerPool) MinWorkers() int
- func (p *WorkerPool) RunningWorkers() int
- func (p *WorkerPool) Stop() context.Context
- func (p *WorkerPool) StopAndWait()
- func (p *WorkerPool) StopAndWaitFor(deadline time.Duration)
- func (p *WorkerPool) Stopped() bool
- func (p *WorkerPool) Strategy() ResizingStrategy
- func (p *WorkerPool) Submit(task func())
- func (p *WorkerPool) SubmitAndWait(task func())
- func (p *WorkerPool) SubmitBefore(task func(), deadline time.Duration)
- func (p *WorkerPool) SubmittedTasks() uint64
- func (p *WorkerPool) SuccessfulTasks() uint64
- func (p *WorkerPool) TrySubmit(task func()) bool
- func (p *WorkerPool) WaitingTasks() uint64
Constants ¶
This section is empty.
Variables ¶
var ( // Eager maximizes responsiveness at the expense of higher resource usage, // which can reduce throughput under certain conditions. // This strategy is meant for worker pools that will operate at a small percentage of their capacity // most of the time and may occasionally receive bursts of tasks. It's the default strategy. Eager = func() ResizingStrategy { return RatedResizer(1) } // Balanced tries to find a balance between responsiveness and throughput. // It's suitable for general purpose worker pools or those // that will operate close to 50% of their capacity most of the time. Balanced = func() ResizingStrategy { return RatedResizer(maxProcs / 2) } // Lazy maximizes throughput at the expense of responsiveness. // This strategy is meant for worker pools that will operate close to their max. capacity most of the time. Lazy = func() ResizingStrategy { return RatedResizer(maxProcs) } )
Preset pool resizing strategies
var ( // ErrSubmitOnStoppedPool is thrown when attempting to submit a task to a pool that has been stopped ErrSubmitOnStoppedPool = errors.New("worker pool has been stopped and is no longer accepting tasks") )
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*WorkerPool)
Option represents an option that can be passed when instantiating a worker pool to customize it
func Context ¶ added in v1.7.0
Context configures a parent context on a worker pool to stop all workers when it is cancelled
func IdleTimeout ¶
IdleTimeout allows to change the idle timeout for a worker pool
func MinWorkers ¶
MinWorkers allows to change the minimum number of workers of a worker pool
func PanicHandler ¶
func PanicHandler(panicHandler func(interface{})) Option
PanicHandler allows to change the panic handler function of a worker pool
func Strategy ¶ added in v1.3.0
func Strategy(strategy ResizingStrategy) Option
Strategy allows to change the strategy used to resize the pool
type ResizingStrategy ¶ added in v1.3.0
ResizingStrategy represents a pool resizing strategy
func RatedResizer ¶ added in v1.4.0
func RatedResizer(rate int) ResizingStrategy
RatedResizer creates a resizing strategy which can be configured to create workers at a specific rate when the pool has no idle workers. rate: determines the number of tasks to receive before creating an extra worker. A value of 3 can be interpreted as: "Create a new worker every 3 tasks".
type TaskGroup ¶
type TaskGroup struct {
// contains filtered or unexported fields
}
TaskGroup represents a group of related tasks
type TaskGroupWithContext ¶ added in v1.8.0
type TaskGroupWithContext struct { TaskGroup // contains filtered or unexported fields }
TaskGroupWithContext represents a group of related tasks associated to a context
func (*TaskGroupWithContext) Submit ¶ added in v1.8.0
func (g *TaskGroupWithContext) Submit(task func() error)
Submit adds a task to this group and sends it to the worker pool to be executed
func (*TaskGroupWithContext) Wait ¶ added in v1.8.0
func (g *TaskGroupWithContext) Wait() error
Wait blocks until either all the tasks submitted to this group have completed, one of them returned a non-nil error or the context associated to this group was canceled.
type WorkerPool ¶
type WorkerPool struct {
// contains filtered or unexported fields
}
WorkerPool models a pool of workers
func New ¶
func New(maxWorkers, maxCapacity int, options ...Option) *WorkerPool
New creates a worker pool with that can scale up to the given maximum number of workers (maxWorkers). The maxCapacity parameter determines the number of tasks that can be submitted to this pool without blocking, because it defines the size of the buffered channel used to receive tasks. The options parameter can take a list of functions to customize configuration values on this worker pool.
func (*WorkerPool) CompletedTasks ¶ added in v1.5.0
func (p *WorkerPool) CompletedTasks() uint64
CompletedTasks returns the total number of tasks that have completed their exection either successfully or with panic since the pool was created
func (*WorkerPool) FailedTasks ¶ added in v1.5.0
func (p *WorkerPool) FailedTasks() uint64
FailedTasks returns the total number of tasks that completed with panic since the pool was created
func (*WorkerPool) GroupContext ¶ added in v1.8.0
func (p *WorkerPool) GroupContext(ctx context.Context) (*TaskGroupWithContext, context.Context)
GroupContext creates a new task group and an associated Context derived from ctx.
The derived Context is canceled the first time a function submitted to the group returns a non-nil error or the first time Wait returns, whichever occurs first.
func (*WorkerPool) IdleWorkers ¶ added in v1.5.0
func (p *WorkerPool) IdleWorkers() int
IdleWorkers returns the current number of idle workers
func (*WorkerPool) MaxCapacity ¶ added in v1.5.0
func (p *WorkerPool) MaxCapacity() int
MaxCapacity returns the maximum number of tasks that can be waiting in the queue at any given time (queue size)
func (*WorkerPool) MaxWorkers ¶ added in v1.5.0
func (p *WorkerPool) MaxWorkers() int
MaxWorkers returns the maximum number of worker goroutines
func (*WorkerPool) MinWorkers ¶ added in v1.5.0
func (p *WorkerPool) MinWorkers() int
MinWorkers returns the minimum number of worker goroutines
func (*WorkerPool) RunningWorkers ¶ added in v1.5.0
func (p *WorkerPool) RunningWorkers() int
RunningWorkers returns the current number of running workers
func (*WorkerPool) Stop ¶
func (p *WorkerPool) Stop() context.Context
Stop causes this pool to stop accepting new tasks and signals all workers to exit. Tasks being executed by workers will continue until completion (unless the process is terminated). Tasks in the queue will not be executed. This method returns a context object that is cancelled when the pool has stopped completely.
func (*WorkerPool) StopAndWait ¶
func (p *WorkerPool) StopAndWait()
StopAndWait causes this pool to stop accepting new tasks and then waits for all tasks in the queue to complete before returning.
func (*WorkerPool) StopAndWaitFor ¶ added in v1.7.0
func (p *WorkerPool) StopAndWaitFor(deadline time.Duration)
StopAndWaitFor stops this pool and waits until either all tasks in the queue are completed or the given deadline is reached, whichever comes first.
func (*WorkerPool) Stopped ¶ added in v1.6.1
func (p *WorkerPool) Stopped() bool
Stopped returns true if the pool has been stopped and is no longer accepting tasks, and false otherwise.
func (*WorkerPool) Strategy ¶ added in v1.5.0
func (p *WorkerPool) Strategy() ResizingStrategy
Strategy returns the configured pool resizing strategy
func (*WorkerPool) Submit ¶
func (p *WorkerPool) Submit(task func())
Submit sends a task to this worker pool for execution. If the queue is full, it will wait until the task is dispatched to a worker goroutine.
func (*WorkerPool) SubmitAndWait ¶
func (p *WorkerPool) SubmitAndWait(task func())
SubmitAndWait sends a task to this worker pool for execution and waits for it to complete before returning
func (*WorkerPool) SubmitBefore ¶
func (p *WorkerPool) SubmitBefore(task func(), deadline time.Duration)
SubmitBefore attempts to send a task for execution to this worker pool but aborts it if the task did not start before the given deadline.
func (*WorkerPool) SubmittedTasks ¶ added in v1.5.0
func (p *WorkerPool) SubmittedTasks() uint64
SubmittedTasks returns the total number of tasks submitted since the pool was created
func (*WorkerPool) SuccessfulTasks ¶ added in v1.5.0
func (p *WorkerPool) SuccessfulTasks() uint64
SuccessfulTasks returns the total number of tasks that have successfully completed their exection since the pool was created
func (*WorkerPool) TrySubmit ¶ added in v1.4.0
func (p *WorkerPool) TrySubmit(task func()) bool
TrySubmit attempts to send a task to this worker pool for execution. If the queue is full, it will not wait for a worker to become idle. It returns true if it was able to dispatch the task and false otherwise.
func (*WorkerPool) WaitingTasks ¶ added in v1.5.0
func (p *WorkerPool) WaitingTasks() uint64
WaitingTasks returns the current number of tasks in the queue that are waiting to be executed
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
dynamic_size
Module
|
|
fixed_size
Module
|
|
group_context
Module
|
|
pool_context
Module
|
|
task_group
Module
|