ctxgroup

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: MIT Imports: 2 Imported by: 0

README

ctxgroup

A wrapper around errgroup, making context usage more obvious. Instead of relying on shadowing errgroup's sub-context variables, it follows the "pass context.Context as the first argument of the function" idiom, which simplifies the context scoping through variable shadowing.

Example usage:

ctx := context.Background()
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
    // ctx gets cancelled when the other goroutine errors out or when Wait() returns
    <-ctx.Done()
    fmt.Println("first goroutine cancelled")
    return nil
})
g.GoCtx(func(ctx context.Context) error {
    return fmt.Errorf("second goroutine erroring out")
})
if err := g.Wait(); err != nil {
    fmt.Printf("ctxgroup.Wait returned: %v\n", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	*errgroup.Group
	// contains filtered or unexported fields
}

func WithContext

func WithContext(ctx context.Context) Group

func (Group) GoCtx

func (g Group) GoCtx(f func(ctx context.Context) error)

func (Group) Wait

func (g Group) Wait() error

Jump to

Keyboard shortcuts

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