podman
1// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package errgroup provides synchronization, error propagation, and Context
6// cancelation for groups of goroutines working on subtasks of a common task.
7//
8// [errgroup.Group] is related to [sync.WaitGroup] but adds handling of tasks
9// returning errors.
10package errgroup11
12import (13"context"14"fmt"15"sync"16)
17
18type token struct{}19
20// A Group is a collection of goroutines working on subtasks that are part of
21// the same overall task.
22//
23// A zero Group is valid, has no limit on the number of active goroutines,
24// and does not cancel on error.
25type Group struct {26cancel func(error)27
28wg sync.WaitGroup29
30sem chan token31
32errOnce sync.Once33err error34}
35
36func (g *Group) done() {37if g.sem != nil {38<-g.sem39}40g.wg.Done()41}
42
43// WithContext returns a new Group and an associated Context derived from ctx.
44//
45// The derived Context is canceled the first time a function passed to Go
46// returns a non-nil error or the first time Wait returns, whichever occurs
47// first.
48func WithContext(ctx context.Context) (*Group, context.Context) {49ctx, cancel := withCancelCause(ctx)50return &Group{cancel: cancel}, ctx51}
52
53// Wait blocks until all function calls from the Go method have returned, then
54// returns the first non-nil error (if any) from them.
55func (g *Group) Wait() error {56g.wg.Wait()57if g.cancel != nil {58g.cancel(g.err)59}60return g.err61}
62
63// Go calls the given function in a new goroutine.
64// It blocks until the new goroutine can be added without the number of
65// active goroutines in the group exceeding the configured limit.
66//
67// The first call to return a non-nil error cancels the group's context, if the
68// group was created by calling WithContext. The error will be returned by Wait.
69func (g *Group) Go(f func() error) {70if g.sem != nil {71g.sem <- token{}72}73
74g.wg.Add(1)75go func() {76defer g.done()77
78if err := f(); err != nil {79g.errOnce.Do(func() {80g.err = err81if g.cancel != nil {82g.cancel(g.err)83}84})85}86}()87}
88
89// TryGo calls the given function in a new goroutine only if the number of
90// active goroutines in the group is currently below the configured limit.
91//
92// The return value reports whether the goroutine was started.
93func (g *Group) TryGo(f func() error) bool {94if g.sem != nil {95select {96case g.sem <- token{}:97// Note: this allows barging iff channels in general allow barging.98default:99return false100}101}102
103g.wg.Add(1)104go func() {105defer g.done()106
107if err := f(); err != nil {108g.errOnce.Do(func() {109g.err = err110if g.cancel != nil {111g.cancel(g.err)112}113})114}115}()116return true117}
118
119// SetLimit limits the number of active goroutines in this group to at most n.
120// A negative value indicates no limit.
121//
122// Any subsequent call to the Go method will block until it can add an active
123// goroutine without exceeding the configured limit.
124//
125// The limit must not be modified while any goroutines in the group are active.
126func (g *Group) SetLimit(n int) {127if n < 0 {128g.sem = nil129return130}131if len(g.sem) != 0 {132panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))133}134g.sem = make(chan token, n)135}
136