mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 15:51:43 +00:00
Backend: replace uses of errors.Group with lib/group (#42787)
This commit is contained in:
parent
9c6d443ff7
commit
1d2ae644a7
@ -16,12 +16,12 @@ import (
|
||||
|
||||
"github.com/grafana/regexp"
|
||||
"github.com/sourcegraph/run"
|
||||
"golang.org/x/sync/semaphore"
|
||||
|
||||
"github.com/sourcegraph/sourcegraph/dev/sg/internal/generate"
|
||||
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
|
||||
"github.com/sourcegraph/sourcegraph/dev/sg/root"
|
||||
"github.com/sourcegraph/sourcegraph/lib/errors"
|
||||
"github.com/sourcegraph/sourcegraph/lib/group"
|
||||
"github.com/sourcegraph/sourcegraph/lib/output"
|
||||
)
|
||||
|
||||
@ -214,22 +214,15 @@ func runGoGenerateOnPaths(ctx context.Context, pkgPaths []string, progressBar bo
|
||||
}
|
||||
|
||||
var (
|
||||
m sync.Mutex
|
||||
g = errors.Group{}
|
||||
sem = semaphore.NewWeighted(int64(runtime.GOMAXPROCS(0)))
|
||||
m sync.Mutex
|
||||
g = group.New().WithContext(ctx).WithMaxConcurrency(runtime.GOMAXPROCS(0))
|
||||
)
|
||||
|
||||
for _, pkgPath := range pkgPaths {
|
||||
if err := sem.Acquire(ctx, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Do not capture loop variable in goroutine below
|
||||
pkgPath := pkgPath
|
||||
|
||||
g.Go(func() error {
|
||||
defer sem.Release(1)
|
||||
|
||||
g.Go(func(ctx context.Context) error {
|
||||
if verbosity == VerboseOutput {
|
||||
progress.Writef("Generating %s...", pkgPath)
|
||||
}
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
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
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGroup(t *testing.T) {
|
||||
err1 := New("group_test: 1")
|
||||
err2 := New("group_test: 2")
|
||||
|
||||
cases := []struct {
|
||||
errs []error
|
||||
nilResult bool
|
||||
}{
|
||||
{errs: []error{}, nilResult: true},
|
||||
{errs: []error{nil}, nilResult: true},
|
||||
{errs: []error{err1}},
|
||||
{errs: []error{err1, nil}},
|
||||
{errs: []error{err1, nil, err2}},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var g Group
|
||||
|
||||
for _, err := range tc.errs {
|
||||
err := err
|
||||
g.Go(func() error { return err })
|
||||
|
||||
}
|
||||
|
||||
gErr := g.Wait()
|
||||
if gErr != nil {
|
||||
for i := range tc.errs {
|
||||
if tc.errs[i] != nil && !strings.Contains(gErr.Error(), tc.errs[i].Error()) {
|
||||
t.Fatalf("expected error to contain %q, actual: %v", tc.errs[i].Error(), gErr)
|
||||
}
|
||||
}
|
||||
} else if !tc.nilResult {
|
||||
t.Fatalf("Group.Wait() should not have returned nil for errs: %v", tc.errs)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user