Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrPoolClosed = errors.New("pool is closed") ErrPoolStarted = errors.New("pool already started") ErrNotStarted = errors.New("pool has not started") ErrNotConfigured = errors.New("pool not configured") ErrInvalidMax = errors.New("maximum workers must be equal or greater than minimum") ErrInvalidMin = errors.New("minimum workers must be at least one") ErrInvalidInitial = errors.New("initial workers must match at least the minimum") ErrMinReached = errors.New("minimum number of workers reached") ErrMaxReached = errors.New("maximum number of workers reached") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Min indicates the minimum number of workers that can run concurrently.
// When 0 is given the minimum is defaulted to 1.
Min int
// Max indicates the maximum number of workers that can run concurrently.
// the default "0" indicates an infinite number of workers.
Max int
// Initial indicates the initial number of workers that should be running.
// When 0 is given the minimum is used.
Initial int
}
type Job ¶
Job is a function that does work.
The only parameter that will receive is a context, the job should try to honor the context cancellation signal as soon as possible.
The context will be cancelled when removing workers from the pool or stopping the pool completely.
type JobMiddleware ¶
JobMiddleware is a function that wraps the job and can be used to extend the functionality of the pool.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
func NewWithConfig ¶
func NewWithConfig(job Job, cfg Config, middlewares ...JobMiddleware) (*Pool, error)
func (*Pool) Close ¶
Close stops all the workers and closes the pool.
Only the first call to Close will shutdown the pool, the next calls will be ignored and return nil.
func (*Pool) CloseWIthTimeout ¶
CloseWIthTimeout displays the same behaviour as close, but instead of passing a context for cancellation we can pass a timeout value.
func (*Pool) Current ¶
Current returns the current number of workers.
There may be more workers executing job while they are pending to complete it's last job. See Less for an explanation why.
func (*Pool) Less ¶
Less signals the pool to reduce a worker and returns immediately without waiting for the worker to stop, which will eventually happen.
func (*Pool) StopOne ¶
StopOne stops one worker and removes it from the pool.
If the number of workers is already the minimum the call will return "ErrMinReached" error.
The current number of workers will decrement even if the given context is cancelled or times out. The worker may still be executing the job but it has a pending signal to terminate and will eventually stop.