Fix: errgroup provides waitgroup within the Go function

Signed-off-by: pa250194 <pa250194@ncr.com>
This commit is contained in:
pa250194 2021-11-02 15:23:19 -05:00 committed by Michael Bridgen
parent b4bd037e5d
commit b3711bb5a1
2 changed files with 2 additions and 5 deletions

View File

@ -23,7 +23,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
gcpstorage "cloud.google.com/go/storage" gcpstorage "cloud.google.com/go/storage"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
@ -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 { func (c *GCPClient) ListObjects(ctx context.Context, matcher gitignore.Matcher, bucketName, tempDir string) error {
log := logr.FromContext(ctx) log := logr.FromContext(ctx)
items := c.Client.Bucket(bucketName).Objects(ctx, nil) items := c.Client.Bucket(bucketName).Objects(ctx, nil)
var wg sync.WaitGroup
g, ctx := errgroup.WithContext(ctx) g, ctx := errgroup.WithContext(ctx)
for { for {
object, err := items.Next() object, err := items.Next()
@ -193,9 +191,7 @@ func (c *GCPClient) ListObjects(ctx context.Context, matcher gitignore.Matcher,
return err return err
} }
if !(strings.HasSuffix(object.Name, "/") || object.Name == sourceignore.IgnoreFile || matcher.Match(strings.Split(object.Name, "/"), false)) { if !(strings.HasSuffix(object.Name, "/") || object.Name == sourceignore.IgnoreFile || matcher.Match(strings.Split(object.Name, "/"), false)) {
wg.Add(1)
g.Go(func() error { g.Go(func() error {
defer wg.Done()
if err := DownloadObject(ctx, c, object, matcher, bucketName, tempDir); err != nil { 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)) log.Error(err, fmt.Sprintf("Error downloading %s from bucket %s: ", object.Name, bucketName))
return err return err
@ -207,7 +203,6 @@ func (c *GCPClient) ListObjects(ctx context.Context, matcher gitignore.Matcher,
if err := g.Wait(); err != nil { if err := g.Wait(); err != nil {
return err return err
} }
wg.Wait()
return nil return nil
} }
@ -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 { func (c *GCPClient) ObjectIsNotFound(err error) bool {
return errors.Is(err, ErrorObjectDoesNotExist) return errors.Is(err, ErrorObjectDoesNotExist)
} }

View File

@ -121,6 +121,7 @@ func (c *MinioClient) Close(ctx context.Context) {
//minio client does not provide a close method //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 { func (c *MinioClient) ObjectIsNotFound(err error) bool {
resp, ok := err.(minio.ErrorResponse) resp, ok := err.(minio.ErrorResponse)
return ok && resp.Code != "NoSuchKey" return ok && resp.Code != "NoSuchKey"