Back to godoc.org
github.com/Jeffail/tunny

package tunny

v0.0.0 (f13eb66)
Latest Go to latest
Published: Sep 30, 2019 | License: MIT | Module: github.com/Jeffail/tunny

Index

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.

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

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

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

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

func (p *Pool) Close()

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

func (*Pool) GetSize

func (p *Pool) GetSize() int

GetSize returns the current size of the 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) ProcessTimed

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

func (p *Pool) QueueLength() int64

QueueLength returns the current count of pending queued jobs.

func (*Pool) SetSize

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

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.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier