errgroupx

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errgroupx provides an opinionated convenience wrapper around package golang.org/x/sync/errgroup that provides a consistent manner of dealing with Contexts and (error) Groups.

A typical usecase might be:

// The run methods executes its receiver's doTHIS and doTHAT methods
// concurrently until either both succeed or one of them fails.
func (t *thing) run(ctx context.Context) error {
	eg, ctx, cancel := errgroupx.WithCancel(ctx)
	defer cancel()

	eg.GoContext(ctx, t.doTHIS)
	eg.GoContext(ctx, t.doTHAT)

	return eg.Wait()
}

func (t *thing) doTHIS(ctx context.Context) error {
	//...do THIS with t and ctx returning an error as appropriate.
}

func (t *thing) doTHAT(ctx context.Context) error {
	//...do THAT with t and ctx returning an error as appropriate.
}

Note that if you make no use of the added constructor functions ('WithCancel', 'WithDeadline' or 'WithTimeout') or the Context aware methods ('GoContext' or 'TryGoContext'), then this package can be used as a drop-in replacement for golang.org/x/sync/errgroup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextFunc added in v0.3.2

type ContextFunc func(context.Context) error

ContextFunc is the function type passed to GoContext or TryGoContext.

type Group

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

Group is a convenience wrapper around an embedded Group from package golang.org/x/sync/errgroup which adds two new methods: GoContext and TryGoContext. All of the other methods on the embedded Group type are available here as well.

func New deprecated

New is equivalent to WithCancel.

Deprecated: use WithCancel instead.

func WithCancel added in v0.3.2

func WithCancel(ctx context.Context) (*Group, context.Context, context.CancelFunc)

WithCancel is a wrapper around errgroup.WithContext and context.WithCancel returning a new Group, a derived Context, and a CancelFunc. The derived Context is canceled the first time a function passed to GoContext (or similar) returns a non-nil error, or the first time Wait returns, whichever occurs first.

See package 'context' about what to do with the CancelFunc.

func WithDeadline added in v0.3.2

func WithDeadline(ctx context.Context, d time.Time) (*Group, context.Context, context.CancelFunc)

WithDeadline is a similar to WithCancel but wraps context.WithDeadline instead of context.WithCancel.

func WithTimeout added in v0.3.2

func WithTimeout(ctx context.Context, timeout time.Duration) (*Group, context.Context, context.CancelFunc)

WithTimeout is a similar to WithCancel but wraps context.WithTimeout instead of context.WithCancel.

func (*Group) GoContext

func (g *Group) GoContext(ctx context.Context, cfunc ContextFunc)

GoContext is a wrapper around the (*Group).Go method from package golang.org/x/sync/errgroup that accepts an anonymous function with a Context parameter. The Context provided here is passed to the ContextFunc unchanged.

func (*Group) TryGoContext

func (g *Group) TryGoContext(ctx context.Context, cfunc ContextFunc) bool

TryGoContext is a wrapper around the (*Group).TryGo method from package golang.org/x/sync/errgroup that accepts an anonymous function with a Context parameter. The Context provided here is passed to the ContextFunc unchanged.

Jump to

Keyboard shortcuts

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