Documentation ¶
Index ¶
- Variables
- type ResizingStrategy
- type TaskFunc
- type WorkOption
- type WorkerPool
- func (wp *WorkerPool) Capacity() int
- func (wp *WorkerPool) Close()
- func (wp *WorkerPool) CloseAndWait(ctx context.Context) error
- func (wp *WorkerPool) CompletedTasks() uint64
- func (wp *WorkerPool) FailedTasks() uint64
- func (wp *WorkerPool) IdleWorkers() int
- func (wp *WorkerPool) MaxWorkers() int
- func (wp *WorkerPool) MinWorkers() int
- func (wp *WorkerPool) RunningTasks() uint64
- func (wp *WorkerPool) RunningWorkers() int
- func (wp *WorkerPool) Strategy() ResizingStrategy
- func (wp *WorkerPool) Submit(ctx context.Context, task TaskFunc)
- func (wp *WorkerPool) SubmitAndWait(ctx context.Context, task TaskFunc)
- func (wp *WorkerPool) SubmittedTasks() uint64
- func (wp *WorkerPool) SuccessfulTasks() uint64
- func (wp *WorkerPool) TrySubmit(ctx context.Context, task TaskFunc) bool
- func (wp *WorkerPool) TrySubmitAndWait(ctx context.Context, task TaskFunc) bool
- func (wp *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
Functions ¶
This section is empty.
Types ¶
type ResizingStrategy ¶
type ResizingStrategy interface { // Resize return true if should resize Resize(runningWorkers int) bool }
func RatedResizer ¶
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 WorkOption ¶
type WorkOption func(wp *WorkerPool)
func WorkerWithCapacity ¶
func WorkerWithCapacity(n int) WorkOption
WorkerWithCapacity set capacity of task buffer for a worker pool. if n == 0, no buffer TODO: if n < 0, no limit (maybe persistent store task data) TODO: design a storage interface? to storage tasks in memory or disks
func WorkerWithIdle ¶
func WorkerWithIdle(d time.Duration) WorkOption
WorkWithIdle set idle timeout for a worker pool
func WorkerWithMaxWorkers ¶
func WorkerWithMaxWorkers(n int) WorkOption
WorkerWithMaxWorkers set maximum number of workers for a worker pool
func WorkerWithMinWorkers ¶
func WorkerWithMinWorkers(n int) WorkOption
WorkerWithMinWorkers set minimum number of workers for a worker pool
func WorkerWithPanicHandler ¶
func WorkerWithPanicHandler(h func(interface{})) WorkOption
WorkerWithPanicHandler set panic handler for a worker pool
func WorkerWithStrategy ¶
func WorkerWithStrategy(s ResizingStrategy) WorkOption
WorkerWithStrategy set strategy for resizing the worker pool
type WorkerPool ¶
type WorkerPool struct {
// contains filtered or unexported fields
}
func New ¶
func New(options ...WorkOption) *WorkerPool
func (*WorkerPool) Capacity ¶
func (wp *WorkerPool) Capacity() int
Capacity returns the current capacity of task buffer
func (*WorkerPool) Close ¶
func (wp *WorkerPool) Close()
Close shutdown the pool and remove all waiting tasks. Panics if called for a closed worker pool.
func (*WorkerPool) CloseAndWait ¶
func (wp *WorkerPool) CloseAndWait(ctx context.Context) error
CloseAndWait causes this pool to stop accepting tasks, waiting for all the submitted tasks to complete.
func (*WorkerPool) CompletedTasks ¶
func (wp *WorkerPool) CompletedTasks() uint64
CompletedTasks returns the total number of tasks that have completed their exection either successfully or failed since the pool was created
func (*WorkerPool) FailedTasks ¶
func (wp *WorkerPool) FailedTasks() uint64
FailedTasks returns the total number of tasks that completed with error or panic since the pool was created
func (*WorkerPool) IdleWorkers ¶
func (wp *WorkerPool) IdleWorkers() int
IdleWorkers returns the current number of idle workers
func (*WorkerPool) MaxWorkers ¶
func (wp *WorkerPool) MaxWorkers() int
MaxWorkers returns the maximum number of worker goroutines
func (*WorkerPool) MinWorkers ¶
func (wp *WorkerPool) MinWorkers() int
MinWorkers returns the minimum number of worker goroutines
func (*WorkerPool) RunningTasks ¶
func (wp *WorkerPool) RunningTasks() uint64
RunningTasks returns the current number of running tasks, maybe not accuracy
func (*WorkerPool) RunningWorkers ¶
func (wp *WorkerPool) RunningWorkers() int
RunningWorkers returns the current number of running workers
func (*WorkerPool) Strategy ¶
func (wp *WorkerPool) Strategy() ResizingStrategy
Strategy returns the configured pool resizing strategy
func (*WorkerPool) Submit ¶
func (wp *WorkerPool) Submit(ctx context.Context, task TaskFunc)
Submit submit a task to the worker pool. It blocks until the task is dispatched to a worker.
func (*WorkerPool) SubmitAndWait ¶
func (wp *WorkerPool) SubmitAndWait(ctx context.Context, task TaskFunc)
SubmitAndWait submit a task to the worker pool and waits for complete.
func (*WorkerPool) SubmittedTasks ¶
func (wp *WorkerPool) SubmittedTasks() uint64
SubmittedTasks returns the total number of tasks submitted since the pool was created
func (*WorkerPool) SuccessfulTasks ¶
func (wp *WorkerPool) SuccessfulTasks() uint64
SuccessfulTasks returns the total number of tasks that have successfully completed their exection since the pool was created
func (*WorkerPool) TrySubmit ¶
func (wp *WorkerPool) TrySubmit(ctx context.Context, task TaskFunc) bool
TrySubmit attempts to submit a task to the worker pool. It would not block if there's no idle worker. It returns true if it the task has been dispatched to a worker.
func (*WorkerPool) TrySubmitAndWait ¶
func (wp *WorkerPool) TrySubmitAndWait(ctx context.Context, task TaskFunc) bool
TrySubmitAndWait submit a task to the worker pool and waits for complete. It would not block if there's no idle worker. It returns true if it the task has been dispatched to a worker.
func (*WorkerPool) WaitingTasks ¶
func (wp *WorkerPool) WaitingTasks() uint64
WaitingTasks returns the current number of submitted that are waiting to be executed