2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

Merge pull request #3022 from greatroar/refactor-runworkers

Defer channel closing outside repository.RunWorkers
This commit is contained in:
MichaelEischer 2020-10-14 22:44:42 +02:00 committed by GitHub
commit ce87fbd7dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 24 deletions

View File

@ -135,14 +135,10 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) {
return nil return nil
} }
// final closes indexCh after all workers have terminated
final := func() {
close(resultCh)
}
// run workers on ch // run workers on ch
wg.Go(func() error { wg.Go(func() error {
return repository.RunWorkers(defaultParallelism, worker, final) defer close(resultCh)
return repository.RunWorkers(defaultParallelism, worker)
}) })
// receive decoded indexes // receive decoded indexes

View File

@ -476,14 +476,10 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
return nil return nil
} }
// final closes indexCh after all workers have terminated
final := func() {
close(indexCh)
}
// run workers on ch // run workers on ch
wg.Go(func() error { wg.Go(func() error {
return RunWorkers(loadIndexParallelism, worker, final) defer close(indexCh)
return RunWorkers(loadIndexParallelism, worker)
}) })
// receive decoded indexes // receive decoded indexes

View File

@ -5,10 +5,8 @@ import (
) )
// RunWorkers runs count instances of workerFunc using an errgroup.Group. // RunWorkers runs count instances of workerFunc using an errgroup.Group.
// After all workers have terminated, finalFunc is run. If an error occurs in // If an error occurs in one of the workers, it is returned.
// one of the workers, it is returned. FinalFunc is always run, regardless of func RunWorkers(count int, workerFunc func() error) error {
// any other previous errors.
func RunWorkers(count int, workerFunc func() error, finalFunc func()) error {
var wg errgroup.Group var wg errgroup.Group
// run workers // run workers
@ -16,12 +14,5 @@ func RunWorkers(count int, workerFunc func() error, finalFunc func()) error {
wg.Go(workerFunc) wg.Go(workerFunc)
} }
// wait for termination return wg.Wait()
err := wg.Wait()
// make sure finalFunc is run
finalFunc()
// return error from workers to the caller
return err
} }