Documentation
¶
Index ¶
- type WorkerPool
- func (p *WorkerPool) Pause(ctx context.Context)
- func (p *WorkerPool) Size() int
- func (p *WorkerPool) Stop()
- func (p *WorkerPool) StopWait()
- func (p *WorkerPool) Stopped() bool
- func (p *WorkerPool) Submit(task func())
- func (p *WorkerPool) SubmitWait(task func())
- func (p *WorkerPool) WaitingQueueSize() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type WorkerPool ¶
type WorkerPool struct {
// contains filtered or unexported fields
}
WorkerPool is a collection of goroutines, where the number of concurrent goroutines processing requests does not exceed the specified maximum.
func New ¶
func New(maxWorkers int) *WorkerPool
New creates and starts a pool of worker goroutines.
The maxWorkers parameter specifies the maximum number of workers that can execute tasks concurrently. When there are no incoming tasks, workers are gradually stopped until there are no remaining workers.
func (*WorkerPool) Pause ¶
func (p *WorkerPool) Pause(ctx context.Context)
Pause causes all workers to wait on the given Context, thereby making them unavailable to run tasks. Pause returns when all workers are waiting. Tasks can continue to be queued to the workerpool, but are not executed until the Context is canceled or times out.
Calling Pause when the worker pool is already paused causes Pause to wait until all previous pauses are canceled. This allows a goroutine to take control of pausing and unpausing the pool as soon as other goroutines have unpaused it.
When the workerpool is stopped, workers are unpaused and queued tasks are executed during StopWait.
func (*WorkerPool) Size ¶
func (p *WorkerPool) Size() int
Size returns the maximum number of concurrent workers.
func (*WorkerPool) Stop ¶
func (p *WorkerPool) Stop()
Stop stops the worker pool and waits for only currently running tasks to complete. Pending tasks that are not currently running are abandoned. Tasks must not be submitted to the worker pool after calling stop.
Since creating the worker pool starts at least one goroutine, for the dispatcher, Stop() or StopWait() should be called when the worker pool is no longer needed.
func (*WorkerPool) StopWait ¶
func (p *WorkerPool) StopWait()
StopWait stops the worker pool and waits for all queued tasks tasks to complete. No additional tasks may be submitted, but all pending tasks are executed by workers before this function returns.
func (*WorkerPool) Stopped ¶
func (p *WorkerPool) Stopped() bool
Stopped returns true if this worker pool has been stopped.
func (*WorkerPool) Submit ¶
func (p *WorkerPool) Submit(task func())
Submit enqueues a function for a worker to execute.
Any external values needed by the task function must be captured in a closure. Any return values should be returned over a channel that is captured in the task function closure.
Submit will not block regardless of the number of tasks submitted. Each task is immediately given to an available worker or to a newly started worker. If there are no available workers, and the maximum number of workers are already created, then the task is put onto a waiting queue.
When there are tasks on the waiting queue, any additional new tasks are put on the waiting queue. Tasks are removed from the waiting queue as workers become available.
As long as no new tasks arrive, one available worker is shutdown each time period until there are no more idle workers. Since the time to start new goroutines is not significant, there is no need to retain idle workers indefinitely.
func (*WorkerPool) SubmitWait ¶
func (p *WorkerPool) SubmitWait(task func())
SubmitWait enqueues the given function and waits for it to be executed.
func (*WorkerPool) WaitingQueueSize ¶
func (p *WorkerPool) WaitingQueueSize() int
WaitingQueueSize returns the count of tasks in the waiting queue.