2018-05-01 23:45:06 +00:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package errgroup provides synchronization, error propagation, and Context
|
|
|
|
// cancelation for groups of goroutines working on subtasks of a common task.
|
2024-05-07 10:19:58 +00:00
|
|
|
//
|
|
|
|
// [errgroup.Group] is related to [sync.WaitGroup] but adds handling of tasks
|
|
|
|
// returning errors.
|
2018-05-01 23:45:06 +00:00
|
|
|
package errgroup
|
|
|
|
|
|
|
|
import (
|
2019-11-04 20:28:48 +00:00
|
|
|
"context"
|
2022-09-21 14:17:44 +00:00
|
|
|
"fmt"
|
2018-05-01 23:45:06 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-09-21 14:17:44 +00:00
|
|
|
type token struct{}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// A Group is a collection of goroutines working on subtasks that are part of
|
|
|
|
// the same overall task.
|
|
|
|
//
|
2022-09-21 14:17:44 +00:00
|
|
|
// A zero Group is valid, has no limit on the number of active goroutines,
|
|
|
|
// and does not cancel on error.
|
2018-05-01 23:45:06 +00:00
|
|
|
type Group struct {
|
2023-12-11 11:58:19 +00:00
|
|
|
cancel func(error)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2022-09-21 14:17:44 +00:00
|
|
|
sem chan token
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
errOnce sync.Once
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2022-09-21 14:17:44 +00:00
|
|
|
func (g *Group) done() {
|
|
|
|
if g.sem != nil {
|
|
|
|
<-g.sem
|
|
|
|
}
|
|
|
|
g.wg.Done()
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// WithContext returns a new Group and an associated Context derived from ctx.
|
|
|
|
//
|
|
|
|
// The derived Context is canceled the first time a function passed to Go
|
|
|
|
// returns a non-nil error or the first time Wait returns, whichever occurs
|
|
|
|
// first.
|
|
|
|
func WithContext(ctx context.Context) (*Group, context.Context) {
|
2023-12-11 11:58:19 +00:00
|
|
|
ctx, cancel := withCancelCause(ctx)
|
2018-05-01 23:45:06 +00:00
|
|
|
return &Group{cancel: cancel}, ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until all function calls from the Go method have returned, then
|
|
|
|
// returns the first non-nil error (if any) from them.
|
|
|
|
func (g *Group) Wait() error {
|
|
|
|
g.wg.Wait()
|
|
|
|
if g.cancel != nil {
|
2023-12-11 11:58:19 +00:00
|
|
|
g.cancel(g.err)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
return g.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go calls the given function in a new goroutine.
|
2022-09-21 14:17:44 +00:00
|
|
|
// It blocks until the new goroutine can be added without the number of
|
|
|
|
// active goroutines in the group exceeding the configured limit.
|
2018-05-01 23:45:06 +00:00
|
|
|
//
|
2022-11-11 23:48:45 +00:00
|
|
|
// The first call to return a non-nil error cancels the group's context, if the
|
|
|
|
// group was created by calling WithContext. The error will be returned by Wait.
|
2018-05-01 23:45:06 +00:00
|
|
|
func (g *Group) Go(f func() error) {
|
2022-09-21 14:17:44 +00:00
|
|
|
if g.sem != nil {
|
|
|
|
g.sem <- token{}
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
g.wg.Add(1)
|
2022-09-21 14:17:44 +00:00
|
|
|
go func() {
|
|
|
|
defer g.done()
|
|
|
|
|
|
|
|
if err := f(); err != nil {
|
|
|
|
g.errOnce.Do(func() {
|
|
|
|
g.err = err
|
|
|
|
if g.cancel != nil {
|
2023-12-11 11:58:19 +00:00
|
|
|
g.cancel(g.err)
|
2022-09-21 14:17:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryGo calls the given function in a new goroutine only if the number of
|
|
|
|
// active goroutines in the group is currently below the configured limit.
|
|
|
|
//
|
|
|
|
// The return value reports whether the goroutine was started.
|
|
|
|
func (g *Group) TryGo(f func() error) bool {
|
|
|
|
if g.sem != nil {
|
|
|
|
select {
|
|
|
|
case g.sem <- token{}:
|
|
|
|
// Note: this allows barging iff channels in general allow barging.
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2022-09-21 14:17:44 +00:00
|
|
|
g.wg.Add(1)
|
2018-05-01 23:45:06 +00:00
|
|
|
go func() {
|
2022-09-21 14:17:44 +00:00
|
|
|
defer g.done()
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
if err := f(); err != nil {
|
|
|
|
g.errOnce.Do(func() {
|
|
|
|
g.err = err
|
|
|
|
if g.cancel != nil {
|
2023-12-11 11:58:19 +00:00
|
|
|
g.cancel(g.err)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
2022-09-21 14:17:44 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLimit limits the number of active goroutines in this group to at most n.
|
|
|
|
// A negative value indicates no limit.
|
|
|
|
//
|
|
|
|
// Any subsequent call to the Go method will block until it can add an active
|
|
|
|
// goroutine without exceeding the configured limit.
|
|
|
|
//
|
|
|
|
// The limit must not be modified while any goroutines in the group are active.
|
|
|
|
func (g *Group) SetLimit(n int) {
|
|
|
|
if n < 0 {
|
|
|
|
g.sem = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(g.sem) != 0 {
|
|
|
|
panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))
|
|
|
|
}
|
|
|
|
g.sem = make(chan token, n)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|