goroutine

package
v0.0.0-...-3e81ad6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2016 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPoolAlreadyRunning = errors.New("the pool is already running")
	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")
)

Functions

This section is empty.

Types

type GoroutineExtendedWorker

type GoroutineExtendedWorker interface {

	// Called when the pool is opened, this will be called before any jobs are sent.
	Initialize()

	// Called when the pool is closed, this will be called after all jobs are completed.
	Terminate()
}

GoroutineExtendedWorker - An optional interface that can be implemented if the worker needs more control over its state.

type GoroutineInterruptable

type GoroutineInterruptable interface {

	// Called when the current job has been abandoned by the client.
	Interrupt()
}

GoroutineInterruptable - An optional interface that can be implemented in order to allow the worker to drop jobs when they are abandoned.

type GoroutineWorker

type GoroutineWorker interface {

	// Called for each job, expects the result to be returned synchronously
	Job(interface{}) interface{}

	// Called after each job, this indicates whether the worker is ready for the next job.
	// The default implementation is to return true always. If false is returned then the
	// method is called every five milliseconds until either true is returned or the pool
	// is closed.
	Ready() bool
}

type WorkPool

type WorkPool struct {
	// contains filtered or unexported fields
}

WorkPool contains the structures and methods required to communicate with your pool, it must be opened before sending work and closed when all jobs are completed.

You may open and close a pool as many times as you wish, calling close is a blocking call that guarantees all goroutines are stopped.

func CreateCustomPool

func CreateCustomPool(customWorkers []GoroutineWorker) *WorkPool

CreateCustomPool - Creates a pool for an array of custom workers. The custom workers must implement TunnyWorker, and may also optionally implement TunnyExtendedWorker and TunnyInterruptable.

func CreatePool

func CreatePool(numWorkers int, job func(interface{}) interface{}) *WorkPool

CreatePool - Creates a pool of workers, and takes a closure argument which is the action to perform for each job.

func CreatePoolGeneric

func CreatePoolGeneric(numWorkers int) *WorkPool

CreatePoolGeneric - Creates a pool of generic workers. When sending work to a pool of generic workers you send a closure (func()) which is the job to perform.

func (*WorkPool) Close

func (pool *WorkPool) Close() error

Close all channels and goroutines managed by the pool.

func (*WorkPool) NumPendingAsyncJobs

func (pool *WorkPool) NumPendingAsyncJobs() int32

NumPendingAsyncJobs - Get the current count of async jobs either in flight, or waiting for a worker

func (*WorkPool) NumWorkers

func (pool *WorkPool) NumWorkers() int

NumWorkers - Number of workers in the pool

func (*WorkPool) Open

func (pool *WorkPool) Open() (*WorkPool, error)

Open all channels and launch the background goroutines managed by the pool.

func (*WorkPool) PublishExpvarMetrics

func (pool *WorkPool) PublishExpvarMetrics(poolName string)

PublishExpvarMetrics - Publishes the NumWorkers and NumPendingAsyncJobs to expvars

func (*WorkPool) SendWork

func (pool *WorkPool) SendWork(jobData interface{}) (interface{}, error)

SendWork - Send a job to a worker and return the result, this is a synchronous call.

func (*WorkPool) SendWorkAsync

func (pool *WorkPool) SendWorkAsync(jobData interface{}, after func(interface{}, error))

SendWorkAsync - Send a job to a worker without blocking, and optionally send the result to a receiving closure. You may set the closure to nil if no further actions are required.

func (*WorkPool) SendWorkTimed

func (pool *WorkPool) SendWorkTimed(milliTimeout time.Duration, jobData interface{}) (interface{}, error)

SendWorkTimed - Send a job to a worker and return the result, this is a synchronous call with a timeout.

func (*WorkPool) SendWorkTimedAsync

func (pool *WorkPool) SendWorkTimedAsync(
	milliTimeout time.Duration,
	jobData interface{},
	after func(interface{}, error),
)

SendWorkTimedAsync - Send a timed job to a worker without blocking, and optionally send the result to a receiving closure. You may set the closure to nil if no further actions are required.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL