Documentation ¶
Overview ¶
The parallel package provides utilities for running tasks concurrently.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrStopped = errors.New("try was stopped") ErrClosed = errors.New("try was closed") )
Functions ¶
This section is empty.
Types ¶
type Run ¶
type Run struct {
// contains filtered or unexported fields
}
Run represents a number of functions running concurrently.
func NewRun ¶
NewRun returns a new parallel instance. It provides a way of running functions concurrently while limiting the maximum number running at once to max.
type Try ¶
type Try struct {
// contains filtered or unexported fields
}
Try represents an attempt made concurrently by a number of goroutines.
func NewTry ¶
NewTry returns an object that runs functions concurrently until one succeeds. The result of the first function that returns without an error is available from the Result method. If maxParallel is positive, it limits the number of concurrently running functions.
The function combineErrors(oldErr, newErr) is called to determine the error return (see the Result method). The first time it is called, oldErr will be nil; subsequently oldErr will be the error previously returned by combineErrors. If combineErrors is nil, the last encountered error is chosen.
func (*Try) Close ¶
func (t *Try) Close()
Close closes the Try. No more functions will be started if Start is called, and the Try will terminate when all outstanding functions have completed (or earlier if one succeeds)
func (*Try) Dead ¶
func (t *Try) Dead() <-chan struct{}
Dead returns a channel that is closed when the Try completes.
func (*Try) Kill ¶
func (t *Try) Kill()
Kill stops the try and all its currently executing functions.
func (*Try) Result ¶
Result waits for the Try to complete and returns the result of the first successful function started by Start.
If no function succeeded, the last error returned by combineErrors is returned. If there were no errors or combineErrors returned nil, ErrStopped is returned.
func (*Try) Start ¶
Start requests the given function to be started, waiting until there are less than maxParallel functions running if necessary. It returns an error if the function has not been started (ErrClosed if the Try has been closed, and ErrStopped if the try is finishing).
The function should listen on the stop channel and return if it receives a value, though this is advisory only - the Try does not wait for all started functions to return before completing.
If the function returns a nil error but some earlier try was successful (that is, the returned value is being discarded), its returned value will be closed by calling its Close method.