-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpipelines.go
596 lines (546 loc) · 17.4 KB
/
pipelines.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Package pipelines provides helper functions for constructing concurrent processing pipelines.
// Each pipeline stage represents a single stage in a parallel computation, with an input channel and an output channel.
// Generally, pipeline stages have signatures starting with a context and input channel as their first arguments, and
// returning a channel, as below:
//
// Stage[S,T any](ctx context.Context, in <-chan S, ...) <-chan T
//
// The return value from a pipeline stage is referred to as the stage's 'output' channel. Each stage is a non-blocking
// call which starts one or more goroutines which listen on the input channel and send results to the output channel.
// Goroutines started by each stage respond to context cancellation or closure of the input channel by closing its
// output channel and cleaning up all goroutines started. Many pipeline stages take a function as an argument, which
// transform the input and output in some way.
//
// By default, each pipeline starts the minimum number of threads required for its operation, and returns an unbuffered channel.
// These defaults can be modified by passing the result of WithBuffer or WithPool as optional arguments.
package pipelines
import (
"context"
"fmt"
"sync"
)
// Chan converts a slice to a channel. The channel returned is a closed, buffered channel containing exactly the same
// values. Unlike other funcs in this package, Chan does not start any new goroutines.
func Chan[T any](in []T) <-chan T {
result := make(chan T, len(in))
defer close(result) // non-empty buffered channels can be drained even when closed.
for _, t := range in {
result <- t
}
return result
}
// Flatten starts a pipeline stage which converts a "chan []T" to a "chan T". It provides a pipeline stage which converts a channel of slices to a
// channel of scalar values. Each value contained in slices received from the input channel is sent to the output channel.
func Flatten[T any](ctx context.Context, in <-chan []T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doFlatten(ctx, in, out[0])
}, configure(opts)))
}
func doFlatten[T any](ctx context.Context, in <-chan []T, result chan<- T) {
for {
select {
case <-ctx.Done():
return
case t, ok := <-in:
if !ok {
return
}
sendAll(ctx, t, result)
}
}
}
// Map starts a pipeline stage which converts a "chan S" to a "chan T", by converting each S to exactly one T. It
// applies f to every value received from the input channel and sends the result to the output channel. The output
// channel is closed whenever the input channel is closed or the provided context is cancelled.
func Map[S, T any](ctx context.Context, in <-chan S, f func(S) T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doMap(ctx, in, f, out[0])
}, configure(opts)))
}
func doMap[S, T any](ctx context.Context, in <-chan S, f func(S) T, result chan<- T) {
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
select {
case <-ctx.Done():
return
case result <- f(s):
}
}
}
}
// MapCtx starts a pipeline stage which converts a "chan S" to "chan T", by converting each S to exactly one T.
// Unlike Map, the function which performs the conversion also accepts a context.Context.
// Map applies f to every value received from its input channel and sends the result to its output channel.
// The context passed to MapCtx is passed as an argument to f, unchanged.
func MapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S) T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doMapCtx(ctx, in, f, out[0])
}, configure(opts)))
}
func doMapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S) T, result chan<- T) {
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
select {
case <-ctx.Done():
return
case result <- f(ctx, s):
}
}
}
}
// FlatMap starts a pipeline stage which converts a "chan S" to a "chan T", by converting each S to zero or more Ts.
// It applies f to every value received from its input channel and sends all values found in the
// slice returned from f to its output channel.
func FlatMap[S, T any](ctx context.Context, in <-chan S, f func(S) []T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doFlatMap(ctx, in, f, out[0])
}, configure(opts)))
}
func doFlatMap[S, T any](ctx context.Context, in <-chan S, f func(S) []T, out chan<- T) {
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
sendAll(ctx, f(s), out)
}
}
}
// FlatMapCtx starts a pipeline stage which converts a "chan S" to a "chan T", by converting each S to zero or more Ts.
// It applies f to every value received from its input channel and sends all values found in the slice returned from
// f to its output channel.
//
// The context passed to FlatMapCtx is passed as an argument to f, unchanged.
func FlatMapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S) []T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doFlatMapCtx(ctx, in, f, out[0])
}, configure(opts)))
}
func doFlatMapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S) []T, out chan<- T) {
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
sendAll(ctx, f(ctx, s), out)
}
}
}
// Combine converts two "chan T" into a single "chan T". It sends all values received from both of its input
// channels to its output channel.
func Combine[T any](ctx context.Context, t1 <-chan T, t2 <-chan T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doCombine(ctx, t1, t2, out[0])
}, configure(opts)))
}
func doCombine[T any](ctx context.Context, t1 <-chan T, t2 <-chan T, out chan<- T) {
closedCount := 0
for closedCount < 2 {
select {
case <-ctx.Done():
return
case v, ok := <-t1:
if !ok {
t1 = nil
closedCount++
continue
}
out <- v
case v, ok := <-t2:
if !ok {
t2 = nil
closedCount++
continue
}
out <- v
}
}
}
// Tee converts a "chan T" into two "chan T", each of which receive exactly the same values, possibly in different
// orders.
// Both output channels must be drained concurrently to avoid blocking this pipeline stage.
func Tee[T any](ctx context.Context, ch <-chan T, opts ...Option) (<-chan T, <-chan T) {
conf := configure(opts)
conf.outputs = 2
return return2(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doTee(ctx, ch, out[0], out[1])
}, conf))
}
func doTee[T any](ctx context.Context, ch <-chan T, chan1, chan2 chan<- T) {
for {
select {
case t, ok := <-ch:
if !ok {
return
}
select {
case chan1 <- t:
select {
case chan2 <- t:
case <-ctx.Done():
return
}
case chan2 <- t:
select {
case chan1 <- t:
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}
// OptionMap converts a "chan S" to a "chan T" by converting each "S" to zero or one "T"s.
// It applies f to every value received from in and sends any non-nil results to its output channel.
func OptionMap[S, T any](ctx context.Context, in <-chan S, f func(S) *T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doOptionMap(ctx, in, out[0], f)
}, configure(opts)))
}
func doOptionMap[S, T any](ctx context.Context, in <-chan S, out chan<- T, f func(S) *T) {
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
t := f(s)
if t == nil {
continue
}
select {
case <-ctx.Done():
return
case out <- *t:
}
}
}
}
// OptionMapCtx converts a "chan S" to a "chan T" by converting each "S" to zero or one "T"s. It applies f to every
// value received from in and sends all non-nil results to its output channel. The same context passed to OptionMapCtx
// is passed as an argument to f.
func OptionMapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S) *T, opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doOptionMapCtx(ctx, in, out[0], f)
}, configure(opts)))
}
func doOptionMapCtx[S, T any](ctx context.Context, in <-chan S, out chan<- T, f func(context.Context, S) *T) {
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
t := f(ctx, s)
if t == nil {
continue
}
select {
case <-ctx.Done():
return
case out <- *t:
}
}
}
}
// sendAll sends all values in a slice to the provided channel. It blocks until the channel is closed or the provided
// context is cancelled.
func sendAll[T any](ctx context.Context, ts []T, ch chan<- T) {
for _, t := range ts {
select {
case <-ctx.Done():
return
case ch <- t:
}
}
}
// ForkMapCtx converts a "chan S" into a "chan T" by converting each S into zero or more Ts. Unlike FlatMap or FlatMapCtx,
// this func starts a new goroutine for converting each value of S.
// Each goroutine is responsible for sending its output to this channel.
// To avoid resource leaks, f must respect context cancellation when sending to its output channel.
// The same context passed to ForkMapCtx is passed to f.
//
// ForkMapCtx should be used with caution, as it introduces potentially unbounded parallelism to a pipeline computation.
//
// Variants of ForkMapCtx are intentionally omitted from this package.
// ForkMap is omitted because the caller cannot listen for context cancellation in some cases.
// ForkFlatMap is omitted because it is more efficient for the caller range over the slice and send individual values themselves.
func ForkMapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S, chan<- T), opts ...Option) <-chan T {
return return1(doWithConf(ctx, func(ctx context.Context, out ...chan T) {
doForkMapCtx(ctx, in, f, out[0])
}, configure(opts)))
}
func doForkMapCtx[S, T any](ctx context.Context, in <-chan S, f func(context.Context, S, chan<- T), out chan<- T) {
var wg sync.WaitGroup
defer wg.Wait()
for {
select {
case <-ctx.Done():
return
case s, ok := <-in:
if !ok {
return
}
wg.Add(1)
go func(s S) {
defer wg.Done()
f(ctx, s, out)
}(s)
}
}
}
// An Option is passed to optionally configure a pipeline stage.
type Option func(*config)
// WithBuffer configures a pipeline to return a buffered output channel with a buffer of the provided size.s
func WithBuffer(size int) Option {
return func(conf *config) {
conf.bufferSize = size
}
}
// WithPool configures a pipeline to run the provided stage on a parallel worker pool of the given size. All workers are
// kept alive until the input channel is closed or the provided context is cancelled.
func WithPool(numWorkers int) Option {
return func(conf *config) {
conf.workers = numWorkers
}
}
// WithDone configures a pipeline stage to cancel the returned context when all goroutines started by the stage
// have been stopped.
// This is appropriate for termination detection for ANY stages in a pipeline.
// To await termination of ALL stages in a pipeline, use WithWaitGroup.
func WithDone(ctx context.Context) (Option, context.Context) {
ctx, cancel := context.WithCancel(ctx)
return func(conf *config) {
conf.doneCancel = cancel
}, ctx
}
// WithWaitGroup configures a pipeline stage to add a value to the provided WaitGroup for each goroutine started by the
// stage, and signal Done when each goroutine has completed. This option is appropriate for termination detection of
// ALL stages in a pipeline. To detect termination of ANY stage in a pipeline, use WithDone.
func WithWaitGroup(wg *sync.WaitGroup) Option {
return func(conf *config) {
conf.wg = wg
}
}
type config struct {
bufferSize int
workers int
outputs int
doneCancel context.CancelFunc
wg *sync.WaitGroup
}
func makeOutputChannels[T any](c config) []chan T {
var result []chan T
for i := 0; i < c.outputs; i++ {
result = append(result, make(chan T, c.bufferSize))
}
return result
}
// add1 calls Add(1) on the WaitGroup, if one has been configured.
func (c *config) add1() {
if c.wg != nil {
c.wg.Add(1)
}
}
// done calls Done() on the waitgroup, if one has been configured
func (c *config) done() {
if c.wg != nil {
c.wg.Done()
}
}
// cancel calls doneCancel, if it has been configured
func (c config) cancel() {
if c.doneCancel != nil {
c.doneCancel()
}
}
func configure(opts []Option) config {
result := config{
bufferSize: 0,
workers: 1,
outputs: 1,
}
for _, opt := range opts {
opt(&result)
}
return result
}
// doWithConf runs the implementation provided via doIt on goroutines according to the provided options.
func doWithConf[T any](ctx context.Context, doIt func(context.Context, ...chan T), conf config) []chan T {
outs := makeOutputChannels[T](conf)
if conf.workers == 1 {
conf.add1()
// run without a worker pool to avoid overhead from the WaitGroup
go func() {
defer func() {
for _, ch := range outs {
close(ch)
}
conf.cancel()
conf.done()
}()
doIt(ctx, outs...)
}()
} else {
// run on worker pool.
var poolStopped sync.WaitGroup
for i := 0; i < conf.workers; i++ {
poolStopped.Add(1)
conf.add1()
go func(id int) {
defer func() {
poolStopped.Done()
if id == 0 { // first thread closes the output channel.
poolStopped.Wait()
defer func() {
for _, ch := range outs {
close(ch)
}
}()
conf.cancel()
}
conf.done()
}()
doIt(ctx, outs...)
}(i)
}
}
return outs
}
// Drain receives all values from the provided channel and returns them in a slice.
// Drain blocks the caller until the input channel is closed or the provided context is cancelled.
// An error is returned if and only if the provided context was cancelled before the input channel was closed.
func Drain[T any](ctx context.Context, in <-chan T) ([]T, error) {
var result []T
for {
select {
case <-ctx.Done():
return result, ctx.Err()
case repo, ok := <-in:
if !ok {
return result, nil
}
result = append(result, repo)
}
}
}
// Reduce runs a reducer function on every input received from the in chan and returns the output. Reduce blocks the
// caller until the input channel is closed or the provided context is cancelled.
// An error is returned if and only if the provided context was cancelled before the input channel was closed.
func Reduce[S, T any](ctx context.Context, in <-chan S, f func(T, S) T) (T, error) {
var result T
for {
select {
case <-ctx.Done():
fmt.Println("reduce closed")
return result, ctx.Err()
case s, ok := <-in:
if !ok {
return result, nil
}
result = f(result, s)
}
}
}
// ErrorSink provides an error-handling solution for pipelines created by this package. It manages a
// pipeline stage which can receive fatal and non-fatal errors that may occur during the course of a pipeline.
type ErrorSink struct {
errors chan errWrapper
cancel context.CancelFunc
errs []error
needLock bool
lock *sync.Mutex
wg *sync.WaitGroup
}
// NewErrorSink returns a new ErrorSink, along with a context which is cancelled when a fatal error is sent to the
// ErrorSink. Starts a new, configurable pipeline stage which catches any errors reported.
func NewErrorSink(ctx context.Context, opts ...Option) (context.Context, *ErrorSink) {
ctx, cancel := context.WithCancel(ctx)
result := &ErrorSink{cancel: cancel, wg: &sync.WaitGroup{}}
config := configure(opts)
if config.workers > 1 {
result.needLock = true
result.lock = &sync.Mutex{}
}
outs := doWithConf(ctx, func(ctx context.Context, in ...chan errWrapper) {
result.doErrSink(ctx, in[0])
}, config)
result.errors = outs[0]
return ctx, result
}
func (s *ErrorSink) doErrSink(ctx context.Context, errors chan errWrapper) {
for {
select {
case <-ctx.Done():
return
case werr := <-errors:
s.appendErr(werr.err)
s.wg.Done()
if werr.isFatal {
s.cancel()
}
}
}
}
func (s *ErrorSink) appendErr(err error) {
if s.needLock { // a lock is only needed in case the ErrorSink was started with
s.lock.Lock()
defer s.lock.Unlock()
}
s.errs = append(s.errs, err)
}
// Fatal sends a fatal error to this ErrorSink, cancelling the child context which was created by NewErrorSink,
// as well as reporting this error.
func (s *ErrorSink) Fatal(err error) {
s.wg.Add(1)
s.errors <- errWrapper{isFatal: true, err: err}
}
// Error sends a non-fatal error to this ErrorSink, which is reported and included along with All()
func (s *ErrorSink) Error(err error) {
s.wg.Add(1)
s.errors <- errWrapper{isFatal: false, err: err}
}
// All returns all errors which have been received by this ErrorSink so far. Subsequent calls to All can return strictly
// more errors, but will never return fewer errors. The only way to be certain that all errors from a pipeline have been
// reported is to pass WithWaitGroup to every pipeline stage which sends an error to this ErrorSink and wait for all
// stages to terminate before calling All().
func (s *ErrorSink) All() []error {
s.wg.Wait()
return s.errs
}
type errWrapper struct {
isFatal bool
err error
}
func return1[T any](chans []chan T) <-chan T {
return chans[0]
}
func return2[T any](chans []chan T) (<-chan T, <-chan T) {
return chans[0], chans[1]
}