-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
41 lines (35 loc) · 765 Bytes
/
exec.go
File metadata and controls
41 lines (35 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package parallel_exec
import (
"sync"
)
func Execute(funcs []func() error, countParallelExec int, errCount int) {
var wg sync.WaitGroup
var mutex sync.RWMutex
chFunc := make(chan func() error, len(funcs))
counter := 0
for _, fun := range funcs {
chFunc <- fun
}
close(chFunc)
for i := 0; i < countParallelExec; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup, mutex *sync.RWMutex, chFunc <-chan func() error, counter, errCount int) {
defer wg.Done()
for fun := range chFunc {
mutex.RLock()
if counter >= errCount {
mutex.RUnlock()
return
}
mutex.RUnlock()
err := fun()
if err != nil {
mutex.Lock()
counter++
mutex.Unlock()
}
}
}(&wg, &mutex, chFunc, counter, errCount)
}
wg.Wait()
}