mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 19:51:50 +00:00
Wholesale migration away from go-multierror into a custom multierror implementation that is fully compatible with cockroachdb/errors, prints all errors, can be introspected with Is, As, and friends, and more. The new MultiError type is only available as an interface. Co-authored-by: Camden Cheek <camden@ccheek.com>
41 lines
889 B
Go
41 lines
889 B
Go
package errors
|
|
|
|
import "sync"
|
|
|
|
// Group is a collection of goroutines which return errors that need to be
|
|
// coalesced.
|
|
//
|
|
// Implementation and tests are based on https://sourcegraph.com/github.com/hashicorp/go-multierror/-/blob/group.go
|
|
type Group struct {
|
|
mutex sync.Mutex
|
|
err MultiError
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// Go calls the given function in a new goroutine.
|
|
//
|
|
// If the function returns an error it is added to the group multierror which
|
|
// is returned by Wait.
|
|
func (g *Group) Go(f func() error) {
|
|
g.wg.Add(1)
|
|
|
|
go func() {
|
|
defer g.wg.Done()
|
|
|
|
if err := f(); err != nil {
|
|
g.mutex.Lock()
|
|
g.err = CombineErrors(g.err, err)
|
|
g.mutex.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Wait blocks until all function calls from the Go method have returned, then
|
|
// returns the multierror.
|
|
func (g *Group) Wait() MultiError {
|
|
g.wg.Wait()
|
|
g.mutex.Lock()
|
|
defer g.mutex.Unlock()
|
|
return g.err
|
|
}
|