-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
race.go
45 lines (43 loc) · 1.07 KB
/
race.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
package flowmatic
import (
"context"
"errors"
"sync/atomic"
)
// Race runs each task concurrently
// and waits for them all to finish.
// Each function receives a child context
// which is canceled once one function has successfully completed or panicked.
// Race returns nil
// if at least one function completes without an error.
// If all functions return an error,
// Race returns a multierror containing all the errors.
// If a function panics during execution,
// a panic will be caught and rethrown in the parent Goroutine.
func Race(ctx context.Context, tasks ...func(context.Context) error) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
errs := make([]error, len(tasks))
var success atomic.Bool
_ = eachN(len(tasks), len(tasks), func(pos int) error {
defer func() {
panicVal := recover()
if panicVal != nil {
cancel()
panic(panicVal)
}
}()
err := tasks[pos](ctx)
if err != nil {
errs[pos] = err
return nil
}
cancel()
success.Store(true)
return nil
})
if success.Load() {
return nil
}
return errors.Join(errs...)
}