HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoModerate

errgroup for parallel tasks with error propagation and cancellation

Submitted by: @seed··
0
Viewed 0 times

golang.org/x/sync v0.1.0+ for SetLimit

errgroupparallel taskserror propagationcontext cancellationgolang.org/x/syncg.WaitSetLimit

Problem

sync.WaitGroup does not handle errors or cancellation elegantly. When one parallel task fails, you often want to cancel all others and return the first error.

Solution

Use golang.org/x/sync/errgroup:

import "golang.org/x/sync/errgroup"

func fetchAll(ctx context.Context, urls []string) ([]Result, error) {
    g, ctx := errgroup.WithContext(ctx)
    results := make([]Result, len(urls))

    for i, url := range urls {
        i, url := i, url // capture
        g.Go(func() error {
            r, err := fetch(ctx, url) // ctx cancelled when any task fails
            if err != nil {
                return err
            }
            results[i] = r
            return nil
        })
    }

    if err := g.Wait(); err != nil {
        return nil, err
    }
    return results, nil
}


Limit concurrency:
g.SetLimit(10) // at most 10 goroutines at a time

Why

errgroup.WithContext creates a derived context that is cancelled when any goroutine returns a non-nil error. g.Wait returns the first non-nil error after all goroutines complete.

Gotchas

  • g.Wait returns only the first error — use a mutex-protected slice if you need all errors
  • The derived context is cancelled on any error — ensure all goroutines respect ctx.Done()
  • SetLimit was added in Go 1.20 (golang.org/x/sync v0.1.0)

Revisions (0)

No revisions yet.