mirror of
https://github.com/ysoftdevs/gardener-extension-shoot-fleet-agent.git
synced 2026-03-17 23:04:11 +01:00
Add the forgotten utils module
This commit is contained in:
43
pkg/utils/parallel.go
Normal file
43
pkg/utils/parallel.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// RunParallelFunctions runs a list of functions in parallel
|
||||
// returns nil if all functions return nil
|
||||
// returns an error which wraps all errors that occurred within the functions
|
||||
func RunParallelFunctions(functions []func() error) error {
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var errorList []error
|
||||
|
||||
for _, function := range functions {
|
||||
wg.Add(1)
|
||||
|
||||
// create an intermediate variable so each goroutine uses the correct function and doesn't get overwritten
|
||||
function := function
|
||||
go func(wg *sync.WaitGroup, mu *sync.Mutex) {
|
||||
defer wg.Done()
|
||||
err := function()
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errorList = append(errorList, err)
|
||||
mu.Unlock()
|
||||
}
|
||||
}(&wg, &mu)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if len(errorList) > 0 {
|
||||
err := errors.New("not all functions returned nil error")
|
||||
for _, errorItem := range errorList {
|
||||
err = errors.Wrap(err, errorItem.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user