Documentation ¶
Index ¶
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 will execute tasks concurrently. After each timeout period, a worker goroutine is stopped until there are no remaining 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 passed to a goroutine to be given to the next available worker. If there are no available workers, the dispatcher adds a worker, until the maximum number of workers are running.
After the maximum number of workers are running, and no workers are available, incoming tasks are put onto a queue and will be executed as workers become available.
When no new tasks have been submitted for a time period and a worker is available, the worker is shutdown. 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.
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 will return the size of the waiting queue