utils: github.com/juju/utils/parallel Index | Files

package parallel

import "github.com/juju/utils/parallel"

The parallel package provides utilities for running tasks concurrently.

Index

Package Files

parallel.go try.go

Variables

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

type Errors Uses

type Errors []error

Errors holds any errors encountered during the parallel run.

func (Errors) Error Uses

func (errs Errors) Error() string

type Run Uses

type Run struct {
    // contains filtered or unexported fields
}

Run represents a number of functions running concurrently.

func NewRun Uses

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 Uses

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 Uses

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 Uses

type Try struct {
    // contains filtered or unexported fields
}

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

func NewTry Uses

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 Uses

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 Uses

func (t *Try) Dead() <-chan struct{}

Dead returns a channel that is closed when the Try completes.

func (*Try) Kill Uses

func (t *Try) Kill()

Kill stops the try and all its currently executing functions.

func (*Try) Result Uses

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 Uses

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 Uses

func (t *Try) Wait() error

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

Package parallel imports 5 packages (graph) and is imported by 129 packages. Updated 2019-01-30. Refresh now. Tools for package owners.