Documentation
¶
Index ¶
Constants ¶
const ( Uninitialised = State(iota) // Uninitialised Waiting // Waiting for a task Running // Running Error // Exited in an error state Shutdown // Exited cleanly )
The valid worker states.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a group of workers, and satisfies the worker Interface.
func NewGroupFromWorkFunc ¶
NewGroupUsingWorkFunc returns a new group populated with n workers created by wrapping the given WorkFunc f (which must be non-nil).
func (*Group) Add ¶
Add adds a task for a worker in the group. This will block until space becomes available on the worker's queue.
func (*Group) Done ¶
func (g *Group) Done() <-chan struct{}
Done returns a channel that will be closed when the workers in the group have shut down (or have entered an error state).
func (*Group) Err ¶
Err returns an error if one of the workers in the group has entered an error state.
func (*Group) Shutdown ¶
Shutdown asks the workers in the group to shut down. This will block until all the workers have shut down (or have entered an error state). If after shut down one of the workers in the group is in an error state, that error will be returned.
type Interface ¶
type Interface interface { Add(x interface{}) error // Add submits the task x to the worker. Close() error // Close asks the worker to exit. This will block until the worker has exited. }
Interface defines the interface satisfied by a worker.
type WorkFunc ¶
WorkFunc defines a function that can be wrapped by Wrap to create a worker. A task x will be passed to the function. If the function returns an error then the worker will be placed in an error state and will stop accepting tasks.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker provides a worker by wrapping a WorkFunc.
func Wrap ¶
Wrap returns a new worker wrapping the given function. The work function f (which must be non-nil) will be called for every task to be processed by the worker. If the work function returns an error then the worker will be placed into an error state and will stop. The given context will be passed to f.
func (*Worker) Add ¶
Add submits the task x to the worker. This will block until the worker is free to accept the task. If the worker has exited (because it has Shutdown or is in an Error state) then ErrWorkerNotRunning will be returned.
Note that Add returning nil only guarantees that the task was passed to the worker's WorkFunc, and not that the WorkFunc successfully processed the task. If the WorkFunc fails to successfully process the task (i.e. if the WorkFunc returns an error) then the worker will be placed in an Error state, and the task x, along with the resulting error, can be recovered by calling TaskAndErr.
func (*Worker) Close ¶
Close asks the worker to exit. This will block until the worker has exited (because it has Shutdown or is in an Error state). If the worker exited in an Error state, the worker's last error will be returned. On return, Done is closed.
func (*Worker) Done ¶
func (w *Worker) Done() <-chan struct{}
Done returns a channel that will be closed when the worker has exited (because it has Shutdown or is in an Error state).
func (*Worker) Err ¶
Err returns the worker's last error (if any). This is non-nil if and only if the worker is in an Error state. After Err returns a non-nil error, successive calls to Err return the same error.
func (*Worker) String ¶
String returns the string "Worker(%s)" where "%s" is the worker's current state.
func (*Worker) TaskAndErr ¶
TaskAndErr returns the worker's last error err (if any) and the task x that caused this error. This is non-nil if and only if the worker is in an Error state. After TaskAndErr returns a non-nil task and error, successive calls to TaskAndErr return the same task and error.