Skip to content

Commit 93dd1a4

Browse files
thaJeztahndeloof
authored andcommitted
internal/sync: replace go-multierror.Group with golang.org/x/sync/errgroup
The go-multierror Group is just a shallow wrapper around sync.WaitGroup; https://github.com/hashicorp/go-multierror/blob/v1.1.1/group.go#L5-L38 It does not limit concurrency, but handles synchronisation to collect all errors (if any) in a go-multierror. This patch replaces the go-multierror.Group for a sync.ErrGroup (which is slightly easier to use, and does allow for limiting concurrency if wanted), and a basic slice with mutex to collect the errors and to produce a stdlib multi-error through errors.Join Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent ba3f566 commit 93dd1a4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

internal/sync/tar.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import (
2929
"path"
3030
"path/filepath"
3131
"strings"
32-
33-
"github.com/hashicorp/go-multierror"
32+
"sync"
3433

3534
"github.com/docker/docker/api/types/container"
3635
"github.com/moby/go-archive"
36+
"golang.org/x/sync/errgroup"
3737
)
3838

3939
type archiveEntry struct {
@@ -84,25 +84,38 @@ func (t *Tar) Sync(ctx context.Context, service string, paths []*PathMapping) er
8484
if len(pathsToDelete) != 0 {
8585
deleteCmd = append([]string{"rm", "-rf"}, pathsToDelete...)
8686
}
87-
var eg multierror.Group
87+
88+
var (
89+
eg errgroup.Group
90+
errMu sync.Mutex
91+
errs = make([]error, 0, len(containers)*2) // max 2 errs per container
92+
)
93+
94+
eg.SetLimit(16) // arbitrary limit, adjust to taste :D
8895
for i := range containers {
8996
containerID := containers[i].ID
9097
tarReader := tarArchive(pathsToCopy)
9198

9299
eg.Go(func() error {
93100
if len(deleteCmd) != 0 {
94101
if err := t.client.Exec(ctx, containerID, deleteCmd, nil); err != nil {
95-
return fmt.Errorf("deleting paths in %s: %w", containerID, err)
102+
errMu.Lock()
103+
errs = append(errs, fmt.Errorf("deleting paths in %s: %w", containerID, err))
104+
errMu.Unlock()
96105
}
97106
}
98107

99108
if err := t.client.Untar(ctx, containerID, tarReader); err != nil {
100-
return fmt.Errorf("copying files to %s: %w", containerID, err)
109+
errMu.Lock()
110+
errs = append(errs, fmt.Errorf("copying files to %s: %w", containerID, err))
111+
errMu.Unlock()
101112
}
102-
return nil
113+
return nil // don't fail-fast; collect all errors
103114
})
104115
}
105-
return eg.Wait().ErrorOrNil()
116+
117+
_ = eg.Wait()
118+
return errors.Join(errs...)
106119
}
107120

108121
type ArchiveBuilder struct {

0 commit comments

Comments
 (0)