errgroup

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package errgroup provides error synchronization and combination tools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(fns ...ErrFunc) error

All executes all of the given functions in parallel, and collects and combines all of their returned errors.

func AllInline

func AllInline(fns ...ErrFunc) error

AllInline executes all of the given functions serially in the calling goroutine, and collects and combines all of their returned errors.

func First

func First(fns ...ErrFunc) error

First executes all of the given functions in parallel, and returns the first error returned by them.

func FirstInline

func FirstInline(fns ...ErrFunc) error

FirstInline executes all of the given functions serially in the calling goroutine, and returns the first error returned by them.

Types

type ContextErrFunc

type ContextErrFunc = func(context.Context) error

A ContextErrFunc is a function that accepts a context.Context and returns an error.

type ErrFunc

type ErrFunc = func() error

An ErrFunc is a function that returns an error.

func WithoutContext

func WithoutContext(fn ContextErrFunc) ErrFunc

WithoutContext wraps a ContextErrFunc in an ErrFunc, providing a background context to the given ContextErrFunc.

type Group

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

Group is functionally similar to the standard library's x/sync/errgroup package, but offers more options to customize its behavior based on a given workflow. See the Options documentation for more information.

Groups cannot be reused. A zero-value Group is valid and ready to use.

func New

func New(opts ...Option) *Group

New creates a new Group with the given options.

func (*Group) Add

func (g *Group) Add(fns ...ErrFunc)

Add executes the provided functions and stores returned errors for retrieval with Wait(). If the Group was configured using the WithInline() option, the given functions are executed immediately and serially in the calling goroutine; otherwise, the given functions are executed in parallel.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all functions passed to Add have been executed and returns an error if any were encountered.

The error return depends upon whether the Group was configured using the WithFirstOnly() option. If WithFirstOnly was not used, the returned error is an error containing a chain of all non-nil errors returned by the executed functions; if WithFirstOnly was used, the returned error is the first non-nil error returned verbatim by the first function to finish executing.

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Group.

func WithFirstOnly

func WithFirstOnly() Option

WithFirstOnly returns an Option that configures a Group to return the first encountered error verbatim. Subsequently returned errors will be ignored.

func WithIgnoredErrors

func WithIgnoredErrors(errs ...error) Option

WithIgnoredErrors returns an Option that configures a Group to ignore errors that contain any of the given errors in their error chains.

func WithInline

func WithInline() Option

WithInline returns an Option that configures a Group to execute all functions provided to Group.Add inline and serially within the calling goroutine. Note that this will make Group.Add a blocking call.

type Options

type Options struct {
	// FirstOnly controls whether only the first non-nil error encountered will
	// be returned, or if all errors will be appended in a chain and returned.
	FirstOnly bool
	// IgnoredErrors is used to filter out unhelpful or immaterial errors,
	// such as io.EOF.
	IgnoredErrors []error
	// Inline controls whether functions passed to Group.Add are handled
	// inline and serially in the calling goroutine, or if they will be
	// executed in parallel in a background goroutine. Note that if Inline
	// is true, Group.Add becomes a blocking call.
	Inline bool
}

Options are used to configure a Group.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns a new Options with sane defaults. Using default Options verbatim is functionally equivalent to using a zero-value Group.

func (Options) With

func (o Options) With(opts ...Option) Options

With returns a new Options, using the current Options as a base and merging the given options down onto it.

Jump to

Keyboard shortcuts

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