forked from TrueCloudLab/restic
Merge pull request #3022 from greatroar/refactor-runworkers
Defer channel closing outside repository.RunWorkers
This commit is contained in:
commit
ce87fbd7dc
3 changed files with 7 additions and 24 deletions
|
@ -135,14 +135,10 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// final closes indexCh after all workers have terminated
|
||||
final := func() {
|
||||
close(resultCh)
|
||||
}
|
||||
|
||||
// run workers on ch
|
||||
wg.Go(func() error {
|
||||
return repository.RunWorkers(defaultParallelism, worker, final)
|
||||
defer close(resultCh)
|
||||
return repository.RunWorkers(defaultParallelism, worker)
|
||||
})
|
||||
|
||||
// receive decoded indexes
|
||||
|
|
|
@ -476,14 +476,10 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// final closes indexCh after all workers have terminated
|
||||
final := func() {
|
||||
close(indexCh)
|
||||
}
|
||||
|
||||
// run workers on ch
|
||||
wg.Go(func() error {
|
||||
return RunWorkers(loadIndexParallelism, worker, final)
|
||||
defer close(indexCh)
|
||||
return RunWorkers(loadIndexParallelism, worker)
|
||||
})
|
||||
|
||||
// receive decoded indexes
|
||||
|
|
|
@ -5,10 +5,8 @@ import (
|
|||
)
|
||||
|
||||
// RunWorkers runs count instances of workerFunc using an errgroup.Group.
|
||||
// After all workers have terminated, finalFunc is run. If an error occurs in
|
||||
// one of the workers, it is returned. FinalFunc is always run, regardless of
|
||||
// any other previous errors.
|
||||
func RunWorkers(count int, workerFunc func() error, finalFunc func()) error {
|
||||
// If an error occurs in one of the workers, it is returned.
|
||||
func RunWorkers(count int, workerFunc func() error) error {
|
||||
var wg errgroup.Group
|
||||
|
||||
// run workers
|
||||
|
@ -16,12 +14,5 @@ func RunWorkers(count int, workerFunc func() error, finalFunc func()) error {
|
|||
wg.Go(workerFunc)
|
||||
}
|
||||
|
||||
// wait for termination
|
||||
err := wg.Wait()
|
||||
|
||||
// make sure finalFunc is run
|
||||
finalFunc()
|
||||
|
||||
// return error from workers to the caller
|
||||
return err
|
||||
return wg.Wait()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue