♻️ DRY

This commit is contained in:
Dan Jones 2025-09-07 23:50:23 -05:00
commit 80f455ae77

View file

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