Skip to content

Commit

Permalink
Fix: errgroup provides waitgroup within the Go function
Browse files Browse the repository at this point in the history
Signed-off-by: pa250194 <pa250194@ncr.com>
  • Loading branch information
pa250194 committed Nov 2, 2021
1 parent b874468 commit 66682e5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
6 changes: 1 addition & 5 deletions pkg/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os"
"path/filepath"
"strings"
"sync"

gcpstorage "cloud.google.com/go/storage"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
Expand Down Expand Up @@ -181,7 +180,6 @@ func (c *GCPClient) FGetObject(ctx context.Context, bucketName, objectName, loca
func (c *GCPClient) ListObjects(ctx context.Context, matcher gitignore.Matcher, bucketName, tempDir string) error {
log := logr.FromContext(ctx)
items := c.Client.Bucket(bucketName).Objects(ctx, nil)
var wg sync.WaitGroup
g, ctx := errgroup.WithContext(ctx)
for {
object, err := items.Next()
Expand All @@ -193,9 +191,7 @@ func (c *GCPClient) ListObjects(ctx context.Context, matcher gitignore.Matcher,
return err
}
if !(strings.HasSuffix(object.Name, "/") || object.Name == sourceignore.IgnoreFile || matcher.Match(strings.Split(object.Name, "/"), false)) {
wg.Add(1)
g.Go(func() error {
defer wg.Done()
if err := DownloadObject(ctx, c, object, matcher, bucketName, tempDir); err != nil {
log.Error(err, fmt.Sprintf("Error downloading %s from bucket %s: ", object.Name, bucketName))
return err
Expand All @@ -207,7 +203,6 @@ func (c *GCPClient) ListObjects(ctx context.Context, matcher gitignore.Matcher,
if err := g.Wait(); err != nil {
return err
}
wg.Wait()
return nil
}

Expand All @@ -219,6 +214,7 @@ func (c *GCPClient) Close(ctx context.Context) {
}
}

// ObjectIsNotFound checks if the error provided is ErrorObjectDoesNotExist(object does not exist)
func (c *GCPClient) ObjectIsNotFound(err error) bool {
return errors.Is(err, ErrorObjectDoesNotExist)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (c *MinioClient) Close(ctx context.Context) {
//minio client does not provide a close method
}

// ObjectIsNotFound checks if the error provided is NoSuchKey(object does not exist)
func (c *MinioClient) ObjectIsNotFound(err error) bool {
resp, ok := err.(minio.ErrorResponse)
return ok && resp.Code != "NoSuchKey"
Expand Down

0 comments on commit 66682e5

Please sign in to comment.