Documentation
¶
Index ¶
- Variables
- type Config
- type Job
- type JobBuilder
- type JobBuilderFunc
- type JobFunc
- type Middleware
- type MiddlewareFunc
- type Pool
- func (p *Pool) Close(ctx context.Context) error
- func (p *Pool) CloseWithTimeout(timeout time.Duration) error
- func (p *Pool) Current() int
- func (p *Pool) Less() error
- func (p *Pool) More() error
- func (p *Pool) Run(ctx context.Context, job Job) error
- func (p *Pool) RunWithBuilder(ctx context.Context, jobBuilder JobBuilder) error
- func (p *Pool) Start(job Job) error
- func (p *Pool) StartWithBuilder(jobBuilder JobBuilder) error
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 // StopOnErrors indicates whether the pool should stop when job a returns an error. StopOnErrors bool }
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 JobBuilder ¶ added in v0.6.0
type JobBuilder interface { // New generates a new job for a new worker. New() Job }
JobBuilder is a job that needs to be built during the initialization for each worker.
type JobBuilderFunc ¶ added in v0.6.0
type JobBuilderFunc func() Job
JobBuilderFunc is a helper to use function as job builders.
func (JobBuilderFunc) New ¶ added in v0.6.0
func (f JobBuilderFunc) New() Job
New builds the new job using the underlying function.
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
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 ¶ added in v0.6.0
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.
func (*Pool) Less ¶
Less removes the number of workers in the pool.
This call does not wait for the worker to finish its current job, if the pool is closed though, the call to close it will wait for all removed workers to finish before returning.
func (*Pool) Run ¶ added in v0.7.0
Run is a blocking call that will start the pool and keep it running until the context is terminated.
The pool then will be stopped without a timeout
func (*Pool) RunWithBuilder ¶ added in v0.7.0
func (p *Pool) RunWithBuilder(ctx context.Context, jobBuilder JobBuilder) error
RunWithBuilder is a blocking call that will start the pool with a job builder and keep it running until the context is terminated.
The pool then will be stopped without a timeout
func (*Pool) StartWithBuilder ¶ added in v0.7.0
func (p *Pool) StartWithBuilder(jobBuilder JobBuilder) error
StartWithBuilder launches the workers and keeps them running until the pool is closed.