forked from earthboundkid/flowmatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
each.go
57 lines (54 loc) · 1.48 KB
/
each.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package flowmatic
import (
"errors"
)
// Each starts numWorkers concurrent workers (or GOMAXPROCS workers if numWorkers < 1)
// and processes each item as a task.
// Errors returned by a task do not halt execution,
// but are joined into a multierror return value.
// If a task panics during execution,
// the panic will be caught and rethrown in the parent Goroutine.
func Each[Input any](numWorkers int, items []Input, task func(Input) error) error {
return eachN(numWorkers, len(items), func(pos int) error {
return task(items[pos])
})
}
// eachN starts numWorkers concurrent workers (or GOMAXPROCS workers if numWorkers < 1)
// and starts a task for each number from 0 to numItems.
// Errors returned by a task do not halt execution,
// but are joined into a multierror return value.
// If a task panics during execution,
// the panic will be caught and rethrown in the parent Goroutine.
func eachN(numWorkers, numItems int, task func(int) error) error {
type void struct{}
inch, ouch := TaskPool(numWorkers, func(pos int) (void, error) {
return void{}, task(pos)
})
var (
panicVal any
errs []error
)
_ = Do(
func() error {
for i := 0; i < numItems; i++ {
inch <- i
}
close(inch)
return nil
},
func() error {
for r := range ouch {
if r.Panic != nil && panicVal == nil {
panicVal = r.Panic
}
if r.Err != nil {
errs = append(errs, r.Err)
}
}
return nil
})
if panicVal != nil {
panic(panicVal)
}
return errors.Join(errs...)
}