workerspool

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

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

func (w *Pool) AdjustedMaxParallelism() int

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) IsEnabled

func (w *Pool) IsEnabled() bool

IsEnabled returns whether parallelism is enabled (maxParallelism is != 0)

func (*Pool) IsUnlimited

func (w *Pool) IsUnlimited() bool

IsUnlimited returns whether parallelism is unlimited (maxParallelism < 0)

func (*Pool) MaxParallelism

func (w *Pool) MaxParallelism() int

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.

Each task is expected to keep doing work until there nothing else to be done. As soon as any task finishes, it indicates there is no more need for more task, and it immediately stops spawning new tasks.

It returns when all started tasks have finished.

func (*Pool) SetMaxParallelism

func (w *Pool) SetMaxParallelism(maxParallelism int)

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

func (w *Pool) StartIfAvailable(task func()) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL