Skip to content

Commit

Permalink
Remove unnecessary cancel context
Browse files Browse the repository at this point in the history
errorgroup.WithContext can cancel sibling routines when a routine returns non-nil error.
See https://cs.opensource.google/go/x/sync/+/refs/tags/v0.3.0:errgroup/errgroup.go;l=40-48
  • Loading branch information
otiai10 committed Sep 7, 2023
1 parent 79342f5 commit a5478ea
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,36 +233,26 @@ func dcopySequential(srcdir, destdir string, contents []os.FileInfo, opt Options
// Copy this directory concurrently regarding semaphore of opt.intent
func dcopyConcurrent(srcdir, destdir string, contents []os.FileInfo, opt Options) error {
group, ctx := errgroup.WithContext(opt.intent.ctx)
cancelctx, cancel := context.WithCancel(ctx)
getDcopyRoutine := func(cs, cd string, content os.FileInfo) func() error {
getRoutine := func(cs, cd string, content os.FileInfo) func() error {
return func() error {
select {
case <-cancelctx.Done():
return nil
case <-opt.intent.ctx.Done():
return nil
default:
if content.IsDir() {
return copyNextOrSkip(cs, cd, content, opt)
}
if err := opt.intent.sem.Acquire(cancelctx, 1); err != nil {
cancel()
return err
}
err := copyNextOrSkip(cs, cd, content, opt)
opt.intent.sem.Release(1)
if err != nil {
cancel()
return err
}
return nil
if content.IsDir() {
return copyNextOrSkip(cs, cd, content, opt)
}
if err := opt.intent.sem.Acquire(ctx, 1); err != nil {
return err
}
err := copyNextOrSkip(cs, cd, content, opt)
opt.intent.sem.Release(1)
if err != nil {
return err
}
return nil
}
}
for _, content := range contents {
csd := filepath.Join(srcdir, content.Name())
cdd := filepath.Join(destdir, content.Name())
group.Go(getDcopyRoutine(csd, cdd, content))
group.Go(getRoutine(csd, cdd, content))
}
return group.Wait()
}
Expand Down

0 comments on commit a5478ea

Please sign in to comment.