-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuilder.go
357 lines (304 loc) · 9.4 KB
/
builder.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Package machine - Copyright © 2020 Jonathan Whitaker <github@whitaker.io>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package machine
import (
"context"
"fmt"
)
// Machine is the interface provided for creating a data processing stream.
type Machine[T any] interface {
// Name returns the name of the Machine path. Useful for debugging or reasoning about the path.
Name() string
// Then apply a mutation to each individual element of the payload.
Then(a ...Monad[T]) Machine[T]
// Recurse applies a recursive function to the payload through a Y Combinator.
// f is a function used by the Y Combinator to perform a recursion
// on the payload.
// Example:
//
// func(f Monad[int]) Monad[int] {
// return func(x int) int {
// if x <= 0 {
// return 1
// } else {
// return x * f(x-1)
// }
// }
// }
Recurse(x Monad[Monad[T]]) Machine[T]
// Memoize applies a recursive function to the payload through a Y Combinator
// and memoizes the results based on the index func.
// f is a function used by the Y Combinator to perform a recursion
// on the payload.
// Example:
//
// func(f Monad[int]) Monad[int] {
// return func(x int) int {
// if x <= 0 {
// return 1
// } else {
// return x * f(x-1)
// }
// }
// }
Memoize(x Monad[Monad[T]], index func(T) string) Machine[T]
// Or runs all of the functions until one succeeds or sends the payload to the right branch
Or(x ...Filter[T]) (Machine[T], Machine[T])
// And runs all of the functions and if one doesnt succeed sends the payload to the right branch
And(x ...Filter[T]) (Machine[T], Machine[T])
// Filter splits the data into multiple stream branches
If(f Filter[T]) (Machine[T], Machine[T])
// Select applies a series of Filters to the payload and returns a list of Builders
// the last one being for any unmatched payloads.
Select(fns ...Filter[T]) []Machine[T]
// Tee duplicates the data into multiple stream branches.
Tee(func(T) (a, b T)) (Machine[T], Machine[T])
// While creates a loop in the stream based on the filter
While(x Filter[T]) (loop, out Machine[T])
// Drop terminates the data from further processing without passing it on
Drop()
// Distribute is a function used for fanout
Distribute(Edge[T]) Machine[T]
// Output provided channel
Output() chan T
component(typeName string, fn func(output chan T) vertex[T]) Machine[T]
filterComponent(typeName string, fn filterComponent[T], loop bool) (Machine[T], Machine[T])
setup(ctx context.Context)
next(name string) *builder[T]
}
type builder[T any] struct {
name string
option *config
output chan T
start func(ctx context.Context, channel chan T)
loop *builder[T]
}
// New is a function for creating a new Machine.
//
// name string
// input chan T
// option *Option[T]
//
// Call the startFn returned by New to start the Machine once built.
func New[T any](name string, input chan T, options ...Option) (startFn func(context.Context), x Machine[T]) {
c := &config{}
for _, o := range options {
o.apply(c)
}
b := &builder[T]{
name: name,
loop: nil,
option: c,
output: input,
}
return func(ctx context.Context) {
b.start(ctx, input)
}, b
}
// Transform is a function for converting the type of the Machine. Cannot be used inside a loop
// until I figure out how to do it without some kind of run time error or overly complex
// tracking method that isn't type safe. I really wish method level generics were a thing.
func Transform[T, U any](m Machine[T], fn func(d T) U) (Machine[U], error) {
x := m.(*builder[T])
if x.loop != nil {
return nil, fmt.Errorf("transform cannot be used in a loop")
}
this := &builder[U]{
name: x.name + ":" + "transform",
loop: nil,
option: x.option,
output: make(chan U, x.option.bufferSize),
}
x.start = func(ctx context.Context, channel chan T) {
this.setup(ctx)
vertex[T](func(_ context.Context, payload T) {
this.output <- fn(payload)
}).run(ctx, this.name, channel, x.option)
}
return this, nil
}
// Name returns the name of the Machine path. Useful for debugging or reasoning about the path.
func (x *builder[T]) Name() string {
return x.name
}
// Then apply a mutation to each individual element of the payload.
func (x *builder[T]) Then(fn ...Monad[T]) Machine[T] {
return x.component("then", monadList[T](fn).combine().component)
}
// Select applies a series of Filters to the payload and returns a list of Builders
// the last one being for any unmatched payloads.
func (x *builder[T]) Select(fns ...Filter[T]) []Machine[T] {
out := []Machine[T]{}
var last = x
for i, fn := range fns {
o, l := last.filterComponent(fmt.Sprintf("select-%d", i), fn.component, false)
out = append(out, o)
last = l.(*builder[T])
}
out = append(out, last)
return out
}
// Recurse applies a recursive function to the payload through a Y Combinator.
func (x *builder[T]) Recurse(fn Monad[Monad[T]]) Machine[T] {
g := func(h recursiveBaseFn[T]) Monad[T] {
return func(payload T) T {
return fn(h(h))(payload)
}
}
p := g(g)
return x.component("recurse", p.component)
}
// Memoize applies a recursive function to the payload through a Y Combinator
// and memoizes the results based on the index func.
func (x *builder[T]) Memoize(fn Monad[Monad[T]], index func(T) string) Machine[T] {
g := func(h memoizedBaseFn[T], m map[string]T) Monad[T] {
return func(payload T) T {
id := index(payload)
if v, ok := m[id]; ok {
return v
}
m[id] = fn(h(h, m))(payload)
return m[id]
}
}
p := Monad[T](func(payload T) T {
m := map[string]T{}
return g(g, m)(payload)
})
return x.component("memoize", p.component)
}
// Drop terminates the data from further processing without passing it on
func (x *builder[T]) Drop() {
x.start = func(ctx context.Context, input chan T) {
go transfer(ctx, input, func(_ context.Context, _ T) {}, "", &config{})
}
}
// Or runs all of the functions until one succeeds or sends the payload to the right branch
func (x *builder[T]) Or(list ...Filter[T]) (left, right Machine[T]) {
return x.filterComponent("or", filterList[T](list).or().component, false)
}
// And runs all of the functions and if one doesnt succeed sends the payload to the right branch
func (x *builder[T]) And(list ...Filter[T]) (left, right Machine[T]) {
return x.filterComponent("and", filterList[T](list).and().component, false)
}
// If splits the data into multiple stream branches
func (x *builder[T]) If(fn Filter[T]) (left, right Machine[T]) {
return x.filterComponent("if", fn.component, false)
}
// Tee duplicates the data into multiple stream branches. The payload/vertexes are
// responsible for concurrent read/write controls
func (x *builder[T]) Tee(fn func(T) (a, b T)) (left, right Machine[T]) {
return x.filterComponent("tee",
func(left, right chan T) vertex[T] {
return func(_ context.Context, payload T) {
a, b := fn(payload)
left <- a
right <- b
}
},
false,
)
}
// While creates a loop in the stream based on the filter
func (x *builder[T]) While(fn Filter[T]) (loop, out Machine[T]) {
return x.filterComponent("while", fn.component, true)
}
// Distribute is a function used for fanout
func (x *builder[T]) Distribute(edge Edge[T]) Machine[T] {
this := x.next("distribute")
this.output = edge.Output()
x.start = func(ctx context.Context, channel chan T) {
this.setup(ctx)
vertex[T](edge.Send).run(ctx, this.name, channel, x.option)
}
return this
}
// Output return output channel
func (x *builder[T]) Output() chan T {
return x.output
}
func (x *builder[T]) component(typeName string, fn func(output chan T) vertex[T]) Machine[T] {
this := x.next(typeName)
x.start = func(ctx context.Context, channel chan T) {
this.setup(ctx)
fn(this.output).run(ctx, this.name, channel, x.option)
}
return this
}
func (x *builder[T]) filterComponent(typeName string, fn filterComponent[T], loop bool) (Machine[T], Machine[T]) {
name := x.name + ":" + typeName
l := x.loop
if loop {
l = x
}
left := &builder[T]{
name: name + ":left",
loop: l,
option: x.option,
output: make(chan T, x.option.bufferSize),
}
right := x.next("right")
alreadySetup := false
x.start = func(ctx context.Context, channel chan T) {
if alreadySetup {
if typeName == "while" {
go transfer(ctx, channel,
func(_ context.Context, data T) {
x.output <- data
},
name,
x.option,
)
}
return
}
alreadySetup = true
left.setup(ctx)
right.setup(ctx)
fn(left.output, right.output).run(ctx, name, channel, x.option)
}
return left, right
}
func (x *builder[T]) setup(ctx context.Context) {
if x.start == nil && x.loop != nil {
x.start = x.loop.start
}
if x.start != nil {
x.start(ctx, x.output)
}
}
func (x *builder[T]) next(name string) *builder[T] {
return &builder[T]{
name: x.name + ":" + name,
loop: x.loop,
option: x.option,
output: make(chan T, x.option.bufferSize),
}
}
func transfer[T any](ctx context.Context, input chan T, fn vertex[T], vertexName string, option *config) {
for {
select {
case <-ctx.Done():
if option.flushFN != nil && option.gracePeriod > 0 {
flush(vertexName, input, option)
}
return
case data := <-input:
fn(ctx, data)
}
}
}
func flush[T any](vertexName string, input chan T, option *config) {
c, cancel := context.WithTimeout(context.Background(), option.gracePeriod)
defer cancel()
for {
select {
case <-c.Done():
return
case data := <-input:
option.flushFN(vertexName, data)
}
}
}