Commit 4f9ab3bb
4f9ab3bbc97aMerged
Svyatoslav Dumchenko <[email protected]> ยท 5 years ago ยท 2 files ยท +1 added ยท ~1 modified
Description
fix parallel race and add tests
Details
- Commit
- 4f9ab3bbc97a
- Parent
- fc3b27572e58
pkg/parallel/parallel.gomodifiedView file
@@ -6,9 +6,10 @@
66
)77
88
type WaitGroup struct {9-
syncWG *sync.WaitGroup10-
errChan chan error11-
err *multierror.Error9+
syncWG *sync.WaitGroup10+
errChan chan error11+
err *multierror.Error12+
closeMutex sync.Mutex1213
}1314
1415
func (wg *WaitGroup) ErrChan() chan<- error {@@ -16,25 +17,29 @@
1617
}1718
1819
func NewWaitGroup() *WaitGroup {19-
return &WaitGroup{20+
wg := &WaitGroup{2021
syncWG: &sync.WaitGroup{},2122
errChan: make(chan error),2223
err: &multierror.Error{},2324
}25+
go wg.gatherErrors()26+
return wg2427
}2528
2629
func (wg *WaitGroup) gatherErrors() {30+
wg.closeMutex.Lock()31+
defer wg.closeMutex.Unlock()32+
2733
for err := range wg.errChan {28-
if err != nil {29-
wg.err = multierror.Append(wg.err, err)30-
}34+
wg.err = multierror.Append(wg.err, err)3135
}3236
}3337
3438
func (wg *WaitGroup) Wait() error {35-
go wg.gatherErrors()3639
wg.syncWG.Wait()3740
close(wg.errChan)41+
wg.closeMutex.Lock()42+
wg.closeMutex.Unlock()3843
3944
return wg.err.ErrorOrNil()4045
}pkg/parallel/parallel_test.goaddedView file
Loading diffโฆ