Documentation ¶
Overview ¶
Package taskrunner provides an API for running concurrent tasks in a promise-like style without having to deal with concurrency directly.
The library provides an interface that must be implemented to run a Task concurrently and a concurrency-safe API for running tasks concurrently without managing the channels, goroutines and waitgroups yourself.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*TaskRunner) error
Option is a function that allows configuration of unexported TaskRunner fields. Allows the package to validate input parameters so that a TaskRunner cannot be incorrectly created.
func OptionMaxGoroutines ¶
OptionMaxGoroutines is a functional option for configuring the number of workers in a TaskRunner.
func OptionTaskCounter ¶
OptionTaskCounter allows access to the a metrics.Counter which aggregates the number of tasks processed.
func OptionTaskTimeHistogram ¶
OptionTaskTimeHistogram allows access to customize a histogram for sampling average task times.
func OptionUnhandledPromisesGauge ¶
OptionUnhandledPromisesGauge allows a go-kit metrics.Gauge to be passed-in collect the number of unhandled promises. Useful to discover if there is a leak of unhandled promises in-memory.
func OptionWorkersGauge ¶
OptionWorkersGauge allows access to the current number of workers via a go-kit metrics.Gauge.
type Promise ¶
type Promise func() (interface{}, error)
Promise is a function that returns the result of an asynchronous task.
type Task ¶
Task is an interface for performing a given task. Task self-describes how the job is to be handled by the Task. Returns an error to report task completion.
type TaskRunner ¶
type TaskRunner struct {
// contains filtered or unexported fields
}
TaskRunner is a Runner capable of concurrently running Tasks. Runs multiple goroutines to process Tasks concurrently.
func NewTaskRunner ¶
func NewTaskRunner(options ...func(*TaskRunner) error) (*TaskRunner, error)
NewTaskRunner creates an TaskRunner. Provides functional options for configuring the TaskRunner while also validating the input configurations. Returns an error if the TaskRunner is improperly configured.
func (*TaskRunner) Run ¶
func (p *TaskRunner) Run(ctx context.Context, w Task) Promise
Run gives the Task to an available worker. The given context is used as a hook to cancel a running worker task. Run returns a closure over the result of a Task. When the result of a Task is desired, you can call the function to retrieve the result.
func (*TaskRunner) Start ¶
func (p *TaskRunner) Start() error
Start prepares the TaskRunner for processing tasks. Once started, a TaskRunner is ready to accept Tasks.
func (*TaskRunner) Stop ¶
func (p *TaskRunner) Stop() error
Stop performs a graceful shutdown of the Runner. In-progress Tasks are given some time to finish before exitting.