Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPoolClosed is triggered when trying to start and add or remove // workers from the pool after closing it. ErrPoolClosed = errors.New("pool is closed") // ErrPoolStarted is triggered when trying to start the pool when it's // already running. ErrPoolStarted = errors.New("pool already started") // ErrNotStarted is returned when trying to add or remove workers from // the pool after closing it. ErrNotStarted = errors.New("pool has not started") // ErrInvalidMax is triggered when configuring a pool with an invalid // maximum number of workers. ErrInvalidMax = errors.New("the maximum is less than the minimum workers") // ErrInvalidMin is triggered when configuring a pool with an invalid // minimum number of workers. ErrInvalidMin = errors.New("negative number of minimum workers") // ErrInvalidInitial is triggered when configuring a pool with an invalid // initial number of workers. ErrInvalidInitial = errors.New("the initial is less the minimum workers") // ErrMinReached is triggered when trying to remove a worker when the // pool is already running at minimum capacity. ErrMinReached = errors.New("minimum number of workers reached") // ErrMaxReached is triggered when trying to add a worker when the // pool is already running at maximum capacity. 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.
// By default the pool can have 0 workers, pausing it effectively.
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.
// The default value will be the greater number between 1 or the given minimum.
Initial int
}
Config allows to configure the number of workers that will be running in the pool.
type Job ¶
type Job interface {
// Do executes the job.
//
// The only parameter that will receive is the worker 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
Do(ctx context.Context) error
}
Job represents some work that needs to be done non-stop.
func Wrap ¶ added in v0.4.0
func Wrap(job Job, middlewares ...Middleware) Job
Wrap is a helper to apply a chain of middleware to a job.
type Middleware ¶ added in v0.3.0
Middleware is a function that wraps the job and can be used to extend the functionality of the pool.
type MiddlewareFunc ¶ added in v0.3.0
MiddlewareFunc is a function that implements the job middleware interface.
func (MiddlewareFunc) Wrap ¶ added in v0.3.0
func (f MiddlewareFunc) Wrap(job Job) Job
Wrap executes the middleware function wrapping the job.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a pool of workers that can be started to run a job non-stop concurrently.
func New ¶
func New(middlewares ...Middleware) (*Pool, error)
New creates a new pool with the default configuration.
It accepts an arbitrary number of job middlewares to run.
func NewWithConfig ¶
func NewWithConfig(cfg Config, middlewares ...Middleware) (*Pool, error)
NewWithConfig creates a new pool with an specific configuration.
It accepts an arbitrary number of job middlewares to run.
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 closes the pool waiting for a certain amount of time.
func (*Pool) Current ¶
Current returns the current number of workers.
There may be more workers executing jobs while they are to complete it's last job after being removed, but they will eventually finish and stop processing new jobs.