tunny: github.com/Jeffail/tunny Index | Files

package tunny

import "github.com/Jeffail/tunny"


Package Files

tunny.go worker.go


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.

type Pool Uses

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 Uses

func New(n int, ctor func() Worker) *Pool

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 Uses

func NewCallback(n int) *Pool

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 Uses

func NewFunc(n int, f func(interface{}) interface{}) *Pool

NewFunc creates a new Pool of workers where each worker will process using the provided func.

func (*Pool) Close Uses

func (p *Pool) Close()

Close will terminate all workers and close the job channel of this Pool.

func (*Pool) GetSize Uses

func (p *Pool) GetSize() int

GetSize returns the current size of the pool.

func (*Pool) Process Uses

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) ProcessTimed Uses

func (p *Pool) ProcessTimed(
    payload interface{},
    timeout time.Duration,
) (interface{}, error)

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 Uses

func (p *Pool) QueueLength() int64

QueueLength returns the current count of pending queued jobs.

func (*Pool) SetSize Uses

func (p *Pool) SetSize(n int)

SetSize changes the total number of workers in the Pool. This can be called by any goroutine at any time unless the Pool has been stopped, in which case a panic will occur.

type Worker Uses

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.

    // Interrupt is called when a job is cancelled. The worker is responsible
    // for unblocking the Process implementation.

    // Terminate is called when a Worker is removed from the processing pool
    // and is responsible for cleaning up any held resources.

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.

Package tunny imports 4 packages (graph) and is imported by 24 packages. Updated 2020-06-03. Refresh now. Tools for package owners.