Skip to content

A safer errgroup wrapper with a more obvious API.

License

Notifications You must be signed in to change notification settings

pyr-sh/ctxgroup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
}

About

A safer errgroup wrapper with a more obvious API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages