-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforkjoin.go
More file actions
70 lines (57 loc) · 1.67 KB
/
forkjoin.go
File metadata and controls
70 lines (57 loc) · 1.67 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
package reflow
import (
"context"
"sync"
)
// ForkJoin runs multiple nodes concurrently on the same input and merges
// the results. All nodes must share the same input and output type.
//
// The merge function combines the collected outputs into a single envelope.
// If any node returns an error, ForkJoin returns that error.
func ForkJoin[T any](merge func([]Envelope[T]) Envelope[T], nodes ...Node[T, T]) Runner[T, T] {
return &forkJoinNode[T]{merge: merge, nodes: nodes}
}
type forkJoinNode[T any] struct {
merge func([]Envelope[T]) Envelope[T]
nodes []Node[T, T]
}
func (f *forkJoinNode[T]) Resolve(_ context.Context, in Envelope[T]) (Envelope[T], error) {
return in, nil
}
func (f *forkJoinNode[T]) Act(ctx context.Context, in Envelope[T]) (Envelope[T], error) {
return f.run(ctx, in)
}
func (f *forkJoinNode[T]) Settle(_ context.Context, _ Envelope[T], out Envelope[T], actErr error) (Envelope[T], bool, error) {
if actErr != nil {
return out, false, actErr
}
return out, true, nil
}
func (f *forkJoinNode[T]) Run(ctx context.Context, in Envelope[T]) (Envelope[T], error) {
return f.run(ctx, in)
}
func (f *forkJoinNode[T]) run(ctx context.Context, in Envelope[T]) (Envelope[T], error) {
results := make([]Envelope[T], len(f.nodes))
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
var once sync.Once
var firstErr error
for i, n := range f.nodes {
wg.Add(1)
go func() {
defer wg.Done()
out, err := Run(ctx, n, in)
if err != nil {
once.Do(func() { firstErr = err; cancel() })
return
}
results[i] = out
}()
}
wg.Wait()
if firstErr != nil {
return Envelope[T]{}, firstErr
}
return f.merge(results), nil
}