Version: v0.0.0-...-9d78121 Latest Latest

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

Go to latest
Published: Jun 4, 2020 License: LGPL-3.0 Imports: 5 Imported by: 37



The parallel package provides utilities for running tasks concurrently.



This section is empty.


View Source
var (
	ErrStopped = errors.New("try was stopped")
	ErrClosed  = errors.New("try was closed")


This section is empty.


type Errors

type Errors []error

Errors holds any errors encountered during the parallel run.

func (Errors) Error

func (errs Errors) Error() string

type Run

type Run struct {
	// contains filtered or unexported fields

Run represents a number of functions running concurrently.

func NewRun

func NewRun(max int) *Run

NewRun returns a new parallel instance. It provides a way of running functions concurrently while limiting the maximum number running at once to max.

func (*Run) Do

func (r *Run) Do(f func() error)

Do requests that r run f concurrently. If there are already the maximum number of functions running concurrently, it will block until one of them has completed. Do may itself be called concurrently, but may not be called concurrently with Wait.

func (*Run) Wait

func (r *Run) Wait() error

Wait marks the parallel instance as complete and waits for all the functions to complete. If any errors were encountered, it returns an Errors value describing all the errors in arbitrary order.

type Try

type Try struct {
	// contains filtered or unexported fields

Try represents an attempt made concurrently by a number of goroutines.

func NewTry

func NewTry(maxParallel int, combineErrors func(err0, err1 error) error) *Try

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

func (t *Try) Result() (io.Closer, error)

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

func (t *Try) Start(try func(stop <-chan struct{}) (io.Closer, error)) error

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.

func (*Try) Wait

func (t *Try) Wait() error

Wait waits for the Try to complete and returns the same error returned by Result.

Source Files

Jump to

Keyboard shortcuts

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