♻️ DRY
This commit is contained in:
parent
e53c7e663d
commit
80f455ae77
1 changed files with 14 additions and 17 deletions
31
errgroup.go
31
errgroup.go
|
|
@ -59,6 +59,18 @@ func (g *Group) error() error {
|
||||||
return v.(error)
|
return v.(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Group) setError(err error) {
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
g.errOnce.Do(func() {
|
||||||
|
g.err.Store(err)
|
||||||
|
if g.cancel != nil {
|
||||||
|
g.cancel(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Wait blocks until all function calls from the Go method have returned, then
|
// Wait blocks until all function calls from the Go method have returned, then
|
||||||
// returns the first non-nil error (if any) from them.
|
// returns the first non-nil error (if any) from them.
|
||||||
func (g *Group) Wait() error {
|
func (g *Group) Wait() error {
|
||||||
|
|
@ -102,14 +114,7 @@ func (g *Group) Go(f func() error) {
|
||||||
// that prevents the Wait call from being reached.
|
// that prevents the Wait call from being reached.
|
||||||
// See #53757, #74275, #74304, #74306.
|
// See #53757, #74275, #74304, #74306.
|
||||||
|
|
||||||
if err := f(); err != nil {
|
g.setError(f())
|
||||||
g.errOnce.Do(func() {
|
|
||||||
g.err.Store(err)
|
|
||||||
if g.cancel != nil {
|
|
||||||
g.cancel(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -133,15 +138,7 @@ func (g *Group) TryGo(f func() error) bool {
|
||||||
g.wg.Add(1)
|
g.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer g.done()
|
defer g.done()
|
||||||
|
g.setError(f())
|
||||||
if err := f(); err != nil {
|
|
||||||
g.errOnce.Do(func() {
|
|
||||||
g.err.Store(err)
|
|
||||||
if g.cancel != nil {
|
|
||||||
g.cancel(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue