Documentation ¶
Index ¶
- Variables
- type Pool
- func (p *Pool) Close()
- func (p *Pool) GetSize() int
- func (p *Pool) Process(payload interface{}) interface{}
- func (p *Pool) ProcessCtx(ctx context.Context, payload interface{}) (interface{}, error)
- func (p *Pool) ProcessTimed(payload interface{}, timeout time.Duration) (interface{}, error)
- func (p *Pool) QueueLength() int64
- func (p *Pool) SetSize(n int)
- type Worker
Constants ¶
This section is empty.
Variables ¶
var ( ErrPoolNotRunning = errors.New("the pool is not running") ErrJobNotFunc = errors.New("generic worker not given a func()") ErrWorkerClosed = errors.New("worker was closed") ErrJobTimedOut = errors.New("job request timed out") )
Errors that are used throughout the Tunny API.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a struct that manages a collection of workers, each with their own goroutine. The Pool can initialize, expand, compress and close the workers, as well as processing jobs with the workers synchronously.
func New ¶
New creates a new Pool of workers that starts with n workers. You must provide a constructor function that creates new Worker types and when you change the size of the pool the constructor will be called to create each new Worker.
func NewCallback ¶
NewCallback creates a new Pool of workers where workers cast the job payload into a func() and runs it, or returns ErrNotFunc if the cast failed.
func NewFunc ¶
NewFunc creates a new Pool of workers where each worker will process using the provided func.
func (*Pool) Close ¶
func (p *Pool) Close()
Close will terminate all workers and close the job channel of this Pool.
func (*Pool) Process ¶
func (p *Pool) Process(payload interface{}) interface{}
Process will use the Pool to process a payload and synchronously return the result. Process can be called safely by any goroutines, but will panic if the Pool has been stopped.
func (*Pool) ProcessCtx ¶
ProcessCtx will use the Pool to process a payload and synchronously return the result. If the context cancels before the job has finished the worker will be interrupted and ErrJobTimedOut will be returned. ProcessCtx can be called safely by any goroutines.
func (*Pool) ProcessTimed ¶
ProcessTimed will use the Pool to process a payload and synchronously return the result. If the timeout occurs before the job has finished the worker will be interrupted and ErrJobTimedOut will be returned. ProcessTimed can be called safely by any goroutines.
func (*Pool) QueueLength ¶
QueueLength returns the current count of pending queued jobs.
type Worker ¶
type Worker interface { // Process will synchronously perform a job and return the result. Process(interface{}) interface{} // BlockUntilReady is called before each job is processed and must block the // calling goroutine until the Worker is ready to process the next job. BlockUntilReady() // Interrupt is called when a job is cancelled. The worker is responsible // for unblocking the Process implementation. Interrupt() // Terminate is called when a Worker is removed from the processing pool // and is responsible for cleaning up any held resources. Terminate() }
Worker is an interface representing a Tunny working agent. It will be used to block a calling goroutine until ready to process a job, process that job synchronously, interrupt its own process call when jobs are abandoned, and clean up its resources when being removed from the pool.
Each of these duties are implemented as a single method and can be averted when not needed by simply implementing an empty func.