Documentation ¶
Overview ¶
Package worker contains helpers for working with long-running goroutines that need to be destroyed.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct { // WG is the wait group for goroutines started by a Run call. This can be incremented and // decremented inside Run callbacks (n increments should have a matching number of n decrements // when Run cleans up after itself). Close will wait on the wait group before returning. WG sync.WaitGroup // contains filtered or unexported fields }
Base provides basic operations for objects designed to run as goroutines that have the following properties:
1. Run starts the worker's task and blocks until the worker has finished or it has been shut down via Close.
2.Close stops a running worker. If called before the worker has started, the worker will skip its task. In this case, both Run and Close will return nil.
Base should be added to objects that provide Run and Close methods, and those methods should use the runWrapper and closeWrapper decorators to invoke the run and close logic. For an example, see the testWorker in this file.
Base ensures that calling Run more than once returns an error, calling Close before a Run cancels the run and that Close will wait for Run to finish if it has already started.
If the Run method of the worker spawns additional goroutines, it can use the wg field to add them to the wait group. Close will wait on the wg for all of them to finish before returning.
Similarly to the sync primitives on which it relies, Base cannot be copied.
func (*Base) CloseWrapper ¶
CloseWrapper is used to shut down the worker while waiting for its work to complete.
if closeF is nil, it will be skipped.
func (*Base) GetDoneChan ¶
func (wb *Base) GetDoneChan() chan struct{}
func (*Base) RunWrapper ¶
setupWrapper is used to call a worker's setup and run functions.
The functions are guaranteed to execute only once. Future attempts will return an error.
The setup function and close function share a mutex, guaranteeing they never execute in parallel. The run function is not subject to the same constraint.
The return value is the return value of runF (if it executes), or setupF (otherwise).
If setupF or runF are nil, they will be skipped.