From 1d2ae644a769bf6d326c0f5dbbd05c743859e9d1 Mon Sep 17 00:00:00 2001 From: Camden Cheek Date: Tue, 11 Oct 2022 10:31:22 -0600 Subject: [PATCH] Backend: replace uses of `errors.Group` with `lib/group` (#42787) --- dev/sg/internal/generate/golang/golang.go | 15 +++----- lib/errors/group.go | 40 --------------------- lib/errors/group_test.go | 43 ----------------------- 3 files changed, 4 insertions(+), 94 deletions(-) delete mode 100644 lib/errors/group.go delete mode 100644 lib/errors/group_test.go diff --git a/dev/sg/internal/generate/golang/golang.go b/dev/sg/internal/generate/golang/golang.go index 57a389d471f..754ccf6d063 100644 --- a/dev/sg/internal/generate/golang/golang.go +++ b/dev/sg/internal/generate/golang/golang.go @@ -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) } diff --git a/lib/errors/group.go b/lib/errors/group.go deleted file mode 100644 index 5a2c1033914..00000000000 --- a/lib/errors/group.go +++ /dev/null @@ -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 -} diff --git a/lib/errors/group_test.go b/lib/errors/group_test.go deleted file mode 100644 index 17a81aac3b4..00000000000 --- a/lib/errors/group_test.go +++ /dev/null @@ -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) - } - } -}