errgroupx

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package errgroupx provides an opinionated convenience wrapper around package golang.org/x/sync/errgroup that makes it easier to deal with Groups and Contexts in a consistent manner.

A typical usecase would be:

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

	for _, gf := range[]errgroupx.GoFunc{t.doTHIS, t.doTHAT} {
		eg.GoContext(ctx, gf)
	}

	return eg.Wait()
}

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

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

Note that if you do not use the added constructor function (`New`) or the Context-aware methods, this package is 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 GoFunc

type GoFunc func(context.Context) error

GoFunc 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 that adds two new methods: GoContext and TryGoContext. All of the other methods on the embedded Group type are available here as well.

func New

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

func (*Group) GoContext

func (g *Group) GoContext(ctx context.Context, f GoFunc)

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

func (*Group) TryGoContext

func (g *Group) TryGoContext(ctx context.Context, f GoFunc) bool

TryGoContext is a wrapper around the similarly named TryGo method from golang.org/x/sync/errgroup that accepts an anonymous function with a Context parameter. The Context provided here is passed to the GoFunc unchanged.

Jump to

Keyboard shortcuts

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