Documentation
¶
Index ¶
- type Pool
- func (w *Pool) AdjustedMaxParallelism() int
- func (w *Pool) IsEnabled() bool
- func (w *Pool) IsUnlimited() bool
- func (w *Pool) MaxParallelism() int
- func (w *Pool) Saturate(task func())
- func (w *Pool) SetMaxParallelism(maxParallelism int)
- func (w *Pool) StartIfAvailable(task func()) bool
- func (w *Pool) WaitToStart(task func())
- func (w *Pool) WorkerIsAsleep()
- func (w *Pool) WorkerRestarted()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
func New ¶
func New() *Pool
New return a new Pool of workers with the default parallelism (2 * runtime.GOMAXPROCS()).
func (*Pool) AdjustedMaxParallelism ¶
AdjustedMaxParallelism returns the adjusted soft-target for parallelism (>= 1).
If the target is set to -1 (unlimited parallelism) it returns runtime.GOMAXPROCS. If the target is 0 (no parallelism) it returns 1.
Also, it limits the number of workers to runtime.GOMAXPROCS.
func (*Pool) IsUnlimited ¶
IsUnlimited returns whether parallelism is unlimited (maxParallelism < 0)
func (*Pool) MaxParallelism ¶
MaxParallelism returns the soft-target for parallelism. If set to 0 parallelism is disabled. If set to -1 parallelism is unlimited.
func (*Pool) Saturate ¶
func (w *Pool) Saturate(task func())
Saturate fans out as many workers as available, each running the given task. It keeps spawning workers if more workers become available.
When the first task finishes, it indicates there is not more work to be done, and it stops spawning new tasks.
It returns when all started tasks have finished.
func (*Pool) SetMaxParallelism ¶
SetMaxParallelism sets the maxParallelism.
You should only change the parallelism before any workers start running. If changed during the execution the behavior is undefined.
func (*Pool) StartIfAvailable ¶
StartIfAvailable runs the task in a separate goroutine, if there are enough workers left. It returns true if it found workers to run the function, false otherwise.
It's up to the client to synchronize the end of the function execution.
func (*Pool) WaitToStart ¶
func (w *Pool) WaitToStart(task func())
WaitToStart waits until there is a worker available to run the task.
If parallelism is disabled (maxParallelism is 0), it runs the task inline and returns when it is finished. This is risky if one is relying on concurrency, and it can lead to deadlocks. Avoid using this function if the parallelism is disabled.
func (*Pool) WorkerIsAsleep ¶
func (w *Pool) WorkerIsAsleep()
WorkerIsAsleep indicates the worker (the one that called the method) is going to sleep waiting for other workers, and temporarily increases the available number of workers.
Call WorkerRestarted when the worker is ready to run again.
func (*Pool) WorkerRestarted ¶
func (w *Pool) WorkerRestarted()
WorkerRestarted indicates the worker (the one that called the method) is ready to run again. It should only be called after WorkerIsAsleep.
It returns the temporary number of extra available workers.