Documentation
¶
Index ¶
- type WorkerPool
- func (wp WorkerPool) CurWorks() int
- func (wp WorkerPool) MaxWorks() int
- func (wp WorkerPool) Running() bool
- func (wp WorkerPool) SetIdleTimeout(timeout time.Duration)
- func (wp WorkerPool) SetMaxWorks(maxWorks int)
- func (wp WorkerPool) Start()
- func (wp WorkerPool) Stop()
- func (wp WorkerPool) StopWait()
- func (wp WorkerPool) Submit(task func())
- func (wp WorkerPool) SubmitWait(task func())
- func (wp WorkerPool) Working() bool
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 NewWorkerPool ¶
func NewWorkerPool(maxWorks, maxWaits int) *WorkerPool
NewWorkerPool creates and starts a pool of worker goroutines.
The maxWorks 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) CurWorks ¶
func (wp WorkerPool) CurWorks() int
CurWorks returns the current number of concurrent workers.
func (WorkerPool) MaxWorks ¶
func (wp WorkerPool) MaxWorks() int
MaxWorks returns the maximum number of concurrent workers.
func (WorkerPool) Running ¶
func (wp WorkerPool) Running() bool
Running returns true if this worker pool is running.
func (WorkerPool) SetIdleTimeout ¶
SetIdleTimeout set the timeout to stop a idle worker, panic if timeout < 1ms.
func (WorkerPool) SetMaxWorks ¶
func (wp WorkerPool) SetMaxWorks(maxWorks int)
SetMaxWorks set the maximum number of concurrent workers, panic if maxWorks < 1.
func (WorkerPool) Stop ¶
func (wp 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() should be called when the worker pool is no longer needed.
func (WorkerPool) StopWait ¶
func (wp 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) Submit ¶
func (wp 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 block if the task wait channel is full.
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 go-routines is not significant, there is no need to retain idle workers indefinitely.
func (WorkerPool) SubmitWait ¶
func (wp WorkerPool) SubmitWait(task func())
SubmitWait enqueues the given function and waits for it to be executed.