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
}
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)
}
Job represents some work that needs to be done non-stop.
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(job Job, 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(job Job, 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.