-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
193 lines (159 loc) · 4.02 KB
/
builder.go
File metadata and controls
193 lines (159 loc) · 4.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package pocket
import (
"context"
"fmt"
"sync"
"golang.org/x/sync/errgroup"
)
// Builder provides a fluent API for constructing graphs.
type Builder struct {
nodes map[string]Node
start Node
store Store
opts []GraphOption
}
// NewBuilder creates a new graph builder.
func NewBuilder(store Store) *Builder {
return &Builder{
nodes: make(map[string]Node),
store: store,
}
}
// Add registers a node in the graph.
func (b *Builder) Add(node Node) *Builder {
b.nodes[node.Name()] = node
if b.start == nil {
b.start = node
}
return b
}
// Start sets the starting node.
func (b *Builder) Start(name string) *Builder {
if node, ok := b.nodes[name]; ok {
b.start = node
}
return b
}
// Connect creates a connection between nodes.
func (b *Builder) Connect(from, action, to string) *Builder {
fromNode, ok := b.nodes[from]
if !ok {
return b
}
toNode, ok := b.nodes[to]
if !ok {
return b
}
fromNode.Connect(action, toNode)
return b
}
// WithOptions adds graph options.
func (b *Builder) WithOptions(opts ...GraphOption) *Builder {
b.opts = append(b.opts, opts...)
return b
}
// Build creates the graph.
func (b *Builder) Build() (*Graph, error) {
if b.start == nil {
return nil, ErrNoStartNode
}
return NewGraph(b.start, b.store, b.opts...), nil
}
// RunConcurrent executes multiple nodes concurrently.
func RunConcurrent(ctx context.Context, nodes []Node, store Store, inputs []any) ([]any, error) {
if len(nodes) == 0 {
return nil, nil
}
// If inputs is nil or empty, create nil inputs for each node
if len(inputs) == 0 {
inputs = make([]any, len(nodes))
}
if len(inputs) != len(nodes) {
return nil, fmt.Errorf("input count (%d) must match node count (%d)", len(inputs), len(nodes))
}
g, ctx := errgroup.WithContext(ctx)
results := make([]any, len(nodes))
mu := &sync.Mutex{}
for i, node := range nodes {
i, node := i, node // capture loop variables
input := inputs[i]
g.Go(func() error {
// Each concurrent execution gets its own scoped store
scopedStore := store.Scope(fmt.Sprintf("concurrent-%d", i))
graph := NewGraph(node, scopedStore)
result, err := graph.Run(ctx, input)
if err != nil {
return fmt.Errorf("node %s: %w", node.Name(), err)
}
mu.Lock()
results[i] = result
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
}
// Pipeline executes nodes sequentially, passing output to input.
func Pipeline(ctx context.Context, nodes []Node, store Store, input any) (any, error) {
current := input
for _, node := range nodes {
graph := NewGraph(node, store)
output, err := graph.Run(ctx, current)
if err != nil {
return nil, fmt.Errorf("pipeline failed at %s: %w", node.Name(), err)
}
current = output
}
return current, nil
}
// FanOut executes a node for each input item concurrently.
func FanOut[T any](ctx context.Context, node Node, store Store, items []T) ([]any, error) {
g, ctx := errgroup.WithContext(ctx)
results := make([]any, len(items))
mu := &sync.Mutex{}
for i, item := range items {
i, item := i, item
g.Go(func() error {
// Each item gets its own scoped store
scopedStore := store.Scope(fmt.Sprintf("item-%d", i))
graph := NewGraph(node, scopedStore)
result, err := graph.Run(ctx, item)
if err != nil {
return err
}
mu.Lock()
results[i] = result
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
}
// FanIn collects results from multiple sources.
type FanIn struct {
sources []Node
combine func([]any) (any, error)
}
// NewFanIn creates a fan-in pattern.
func NewFanIn(combine func([]any) (any, error), sources ...Node) *FanIn {
return &FanIn{
sources: sources,
combine: combine,
}
}
// Run executes the fan-in pattern.
func (f *FanIn) Run(ctx context.Context, store Store) (any, error) {
// Create nil inputs for all sources
inputs := make([]any, len(f.sources))
results, err := RunConcurrent(ctx, f.sources, store, inputs)
if err != nil {
return nil, err
}
return f.combine(results)
}