-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe.go
1614 lines (1365 loc) · 41 KB
/
pipe.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny
// Copyright 2017 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
package pipe
import (
"container/ring"
"sync"
"time"
"github.com/cheekybits/genny/generic"
)
// Any is the generic type flowing thru the pipe network.
type Any generic.Type
// ===========================================================================
// Beg of AnyMake creators
// AnyMakeChan returns a new open channel
// (simply a 'chan Any' that is).
//
// Note: No 'Any-producer' is launched here yet! (as is in all the other functions).
// This is useful to easily create corresponding variables such as:
//
// var myAnyPipelineStartsHere := AnyMakeChan()
// // ... lot's of code to design and build Your favourite "myAnyWorkflowPipeline"
// // ...
// // ... *before* You start pouring data into it, e.g. simply via:
// for drop := range water {
// myAnyPipelineStartsHere <- drop
// }
// close(myAnyPipelineStartsHere)
//
// Hint: especially helpful, if Your piping library operates on some hidden (non-exported) type
// (or on a type imported from elsewhere - and You don't want/need or should(!) have to care.)
//
// Note: as always (except for AnyPipeBuffer) the channel is unbuffered.
//
func AnyMakeChan() (out chan Any) {
return make(chan Any)
}
// End of AnyMake creators
// ===========================================================================
// ===========================================================================
// Beg of AnyChan producers
// AnyChan returns a channel to receive
// all inputs
// before close.
func AnyChan(inp ...Any) (out <-chan Any) {
cha := make(chan Any)
go chanAny(cha, inp...)
return cha
}
func chanAny(out chan<- Any, inp ...Any) {
defer close(out)
for i := range inp {
out <- inp[i]
}
}
// AnyChanSlice returns a channel to receive
// all inputs
// before close.
func AnyChanSlice(inp ...[]Any) (out <-chan Any) {
cha := make(chan Any)
go chanAnySlice(cha, inp...)
return cha
}
func chanAnySlice(out chan<- Any, inp ...[]Any) {
defer close(out)
for i := range inp {
for j := range inp[i] {
out <- inp[i][j]
}
}
}
// AnyChanFuncNok returns a channel to receive
// all results of generator `gen`
// until `!ok`
// before close.
func AnyChanFuncNok(gen func() (Any, bool)) (out <-chan Any) {
cha := make(chan Any)
go chanAnyFuncNok(cha, gen)
return cha
}
func chanAnyFuncNok(out chan<- Any, gen func() (Any, bool)) {
defer close(out)
for {
res, ok := gen() // generate
if !ok {
return
}
out <- res
}
}
// AnyChanFuncErr returns a channel to receive
// all results of generator `gen`
// until `err != nil`
// before close.
func AnyChanFuncErr(gen func() (Any, error)) (out <-chan Any) {
cha := make(chan Any)
go chanAnyFuncErr(cha, gen)
return cha
}
func chanAnyFuncErr(out chan<- Any, gen func() (Any, error)) {
defer close(out)
for {
res, err := gen() // generate
if err != nil {
return
}
out <- res
}
}
// End of AnyChan producers
// ===========================================================================
// ===========================================================================
// Beg of AnyPipe functions
// AnyPipe
// will apply every `op` to every `inp` and
// returns a channel to receive
// each `inp`
// before close.
//
// Note: For functional people,
// this 'could' be named `AnyMap`.
// Just: 'map' has a very different meaning in go lang.
func AnyPipe(inp <-chan Any, ops ...func(a Any)) (out <-chan Any) {
cha := make(chan Any)
go pipeAny(cha, inp, ops...)
return cha
}
func pipeAny(out chan<- Any, inp <-chan Any, ops ...func(a Any)) {
defer close(out)
for i := range inp {
for _, op := range ops {
if op != nil {
op(i) // chain action
}
}
out <- i // send it
}
}
// AnyPipeFunc
// will chain every `act` to every `inp` and
// returns a channel to receive
// each result
// before close.
func AnyPipeFunc(inp <-chan Any, acts ...func(a Any) Any) (out <-chan Any) {
cha := make(chan Any)
go pipeAnyFunc(cha, inp, acts...)
return cha
}
func pipeAnyFunc(out chan<- Any, inp <-chan Any, acts ...func(a Any) Any) {
defer close(out)
for i := range inp {
for _, act := range acts {
if act != nil {
i = act(i) // chain action
}
}
out <- i // send result
}
}
// End of AnyPipe functions
// ===========================================================================
// ===========================================================================
// Beg of AnyTube closures around AnyPipe
// AnyTube returns a closure around PipeAny (_, ops...).
func AnyTube(ops ...func(a Any)) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipe(inp, ops...)
}
}
// AnyTubeFunc returns a closure around PipeAnyFunc (_, acts...).
func AnyTubeFunc(acts ...func(a Any) Any) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeFunc(inp, acts...)
}
}
// End of AnyTube closures around AnyPipe
// ===========================================================================
// ===========================================================================
// Beg of AnyDone terminators
// AnyDone
// will apply every `op` to every `inp` and
// returns a channel to receive
// one signal
// upon close.
func AnyDone(inp <-chan Any, ops ...func(a Any)) (done <-chan struct{}) {
sig := make(chan struct{})
go doneAny(sig, inp, ops...)
return sig
}
func doneAny(done chan<- struct{}, inp <-chan Any, ops ...func(a Any)) {
defer close(done)
for i := range inp {
for _, op := range ops {
if op != nil {
op(i) // apply operation
}
}
}
done <- struct{}{}
}
// AnyDoneFunc
// will chain every `act` to every `inp` and
// returns a channel to receive
// one signal
// upon close.
func AnyDoneFunc(inp <-chan Any, acts ...func(a Any) Any) (done <-chan struct{}) {
sig := make(chan struct{})
go doneAnyFunc(sig, inp, acts...)
return sig
}
func doneAnyFunc(done chan<- struct{}, inp <-chan Any, acts ...func(a Any) Any) {
defer close(done)
for i := range inp {
for _, act := range acts {
if act != nil {
i = act(i) // chain action
}
}
}
done <- struct{}{}
}
// AnyDoneSlice returns a channel to receive
// a slice with every Any received on `inp`
// upon close.
//
// Note: Unlike AnyDone, AnyDoneSlice sends the fully accumulated slice, not just an event, once upon close of inp.
func AnyDoneSlice(inp <-chan Any) (done <-chan []Any) {
sig := make(chan []Any)
go doneAnySlice(sig, inp)
return sig
}
func doneAnySlice(done chan<- []Any, inp <-chan Any) {
defer close(done)
slice := []Any{}
for i := range inp {
slice = append(slice, i)
}
done <- slice
}
// End of AnyDone terminators
// ===========================================================================
// ===========================================================================
// Beg of AnyFini closures
// AnyFini returns a closure around `AnyDone(_, ops...)`.
func AnyFini(ops ...func(a Any)) func(inp <-chan Any) (done <-chan struct{}) {
return func(inp <-chan Any) (done <-chan struct{}) {
return AnyDone(inp, ops...)
}
}
// AnyFiniFunc returns a closure around `AnyDoneFunc(_, acts...)`.
func AnyFiniFunc(acts ...func(a Any) Any) func(inp <-chan Any) (done <-chan struct{}) {
return func(inp <-chan Any) (done <-chan struct{}) {
return AnyDoneFunc(inp, acts...)
}
}
// AnyFiniSlice returns a closure around `AnyDoneSlice(_)`.
func AnyFiniSlice() func(inp <-chan Any) (done <-chan []Any) {
return func(inp <-chan Any) (done <-chan []Any) {
return AnyDoneSlice(inp)
}
}
// End of AnyFini closures
// ===========================================================================
// ===========================================================================
// Beg of AnyPair functions
// AnyPair returns a pair of channels to receive every result of inp before close.
// Note: Yes, it is a VERY simple fanout - but sometimes all You need.
func AnyPair(inp <-chan Any) (out1, out2 <-chan Any) {
cha1 := make(chan Any)
cha2 := make(chan Any)
go pairAny(cha1, cha2, inp)
return cha1, cha2
}
/* not used - kept for reference only.
func pairAny ( out1 , out2 chan <- Any , inp <- chan Any ) {
defer close(out1)
defer close(out2)
for i := range inp {
out1 <- i
out2 <- i
}
} */
func pairAny(out1, out2 chan<- Any, inp <-chan Any) {
defer close(out1)
defer close(out2)
for i := range inp {
select { // send first to whomever is ready to receive
case out1 <- i:
out2 <- i
case out2 <- i:
out1 <- i
}
}
}
// End of AnyPair functions
// ===========================================================================
// ===========================================================================
// Beg of AnyFork functions
// AnyFork returns two channels
// either of which is to receive
// every result of inp
// before close.
func AnyFork(inp <-chan Any) (out1, out2 <-chan Any) {
cha1 := make(chan Any)
cha2 := make(chan Any)
go forkAny(cha1, cha2, inp)
return cha1, cha2
}
/* not used - kept for reference only.
func forkAny ( out1 , out2 chan <- Any , inp <- chan Any ) {
defer close(out1)
defer close(out2)
for i := range inp {
out1 <- i
out2 <- i
}
} */
func forkAny(out1, out2 chan<- Any, inp <-chan Any) {
defer close(out1)
defer close(out2)
for i := range inp {
select { // send first to whomever is ready to receive
case out1 <- i:
out2 <- i
case out2 <- i:
out1 <- i
}
}
}
// End of AnyFork functions
// ===========================================================================
// ===========================================================================
// Beg of AnyFanIn2 simple binary Fan-In
// AnyFanIn2 returns a channel to receive
// all from both `inp` and `inp2`
// before close.
func AnyFanIn2(inp, inp2 <-chan Any) (out <-chan Any) {
cha := make(chan Any)
go fanIn2Any(cha, inp, inp2)
return cha
}
/* not used - kept for reference only.
// fanin2Any as seen in Go Concurrency Patterns
func fanin2Any ( out chan <- Any , inp , inp2 <- chan Any ) {
for {
select {
case e := <-inp:
out <- e
case e := <-inp2:
out <- e
}
}
} */
func fanIn2Any(out chan<- Any, inp, inp2 <-chan Any) {
defer close(out)
var (
closed bool // we found a chan closed
ok bool // did we read successfully?
e Any // what we've read
)
for !closed {
select {
case e, ok = <-inp:
if ok {
out <- e
} else {
inp = inp2 // swap inp2 into inp
closed = true // break out of the loop
}
case e, ok = <-inp2:
if ok {
out <- e
} else {
closed = true // break out of the loop }
}
}
}
// inp might not be closed yet. Drain it.
for e = range inp {
out <- e
}
}
// End of AnyFanIn2 simple binary Fan-In
// ===========================================================================
// ===========================================================================
// Beg of AnyPipeBuffered - a buffered channel with capacity `cap` to receive
// AnyPipeBuffered returns a buffered channel with capacity `cap` to receive
// all `inp`
// before close.
func AnyPipeBuffered(inp <-chan Any, cap int) (out <-chan Any) {
cha := make(chan Any, cap)
go pipeAnyBuffered(cha, inp)
return cha
}
func pipeAnyBuffered(out chan<- Any, inp <-chan Any) {
defer close(out)
for i := range inp {
out <- i
}
}
// AnyTubeBuffered returns a closure around PipeAnyBuffer (_, cap).
func AnyTubeBuffered(cap int) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeBuffered(inp, cap)
}
}
// End of AnyPipeBuffered - a buffered channel with capacity `cap` to receive
// ===========================================================================
// ===========================================================================
// Beg of AnyPipeEnter/Leave - Flapdoors observed by a Waiter
// AnyWaiter - as implemented by `*sync.WaitGroup` -
// attends Flapdoors and keeps counting
// who enters and who leaves.
//
// Use AnyDoneWait to learn about
// when the facilities are closed.
//
// Note: You may also use Your provided `*sync.WaitGroup.Wait()`
// to know when to close the facilities.
// Just: AnyDoneWait is more convenient
// as it also closes the primary channel for You.
//
// Just make sure to have _all_ entrances and exits attended,
// and `Wait()` only *after* You've started flooding the facilities.
type AnyWaiter interface {
Add(delta int)
Done()
Wait()
}
// Note: The name is intentionally generic in order to avoid eventual multiple-declaration clashes.
// AnyPipeEnter returns a channel to receive
// all `inp`
// and registers throughput
// as arrival
// on the given `sync.WaitGroup`
// until close.
func AnyPipeEnter(inp <-chan Any, wg AnyWaiter) (out <-chan Any) {
cha := make(chan Any)
go pipeAnyEnter(cha, wg, inp)
return cha
}
// AnyPipeLeave returns a channel to receive
// all `inp`
// and registers throughput
// as departure
// on the given `sync.WaitGroup`
// until close.
func AnyPipeLeave(inp <-chan Any, wg AnyWaiter) (out <-chan Any) {
cha := make(chan Any)
go pipeAnyLeave(cha, wg, inp)
return cha
}
// AnyDoneLeave returns a channel to receive
// one signal after
// all throughput on `inp`
// has been registered
// as departure
// on the given `sync.WaitGroup`
// before close.
func AnyDoneLeave(inp <-chan Any, wg AnyWaiter) (done <-chan struct{}) {
sig := make(chan struct{})
go doneAnyLeave(sig, wg, inp)
return sig
}
func pipeAnyEnter(out chan<- Any, wg AnyWaiter, inp <-chan Any) {
defer close(out)
for i := range inp {
wg.Add(1)
out <- i
}
}
func pipeAnyLeave(out chan<- Any, wg AnyWaiter, inp <-chan Any) {
defer close(out)
for i := range inp {
out <- i
wg.Done()
}
}
func doneAnyLeave(done chan<- struct{}, wg AnyWaiter, inp <-chan Any) {
defer close(done)
for i := range inp {
_ = i // discard
wg.Done()
}
done <- struct{}{}
}
// AnyTubeEnter returns a closure around AnyPipeEnter (_, wg)
// registering throughput
// as arrival
// on the given `sync.WaitGroup`.
func AnyTubeEnter(wg AnyWaiter) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeEnter(inp, wg)
}
}
// AnyTubeLeave returns a closure around AnyPipeLeave (_, wg)
// registering throughput
// as departure
// on the given `sync.WaitGroup`.
func AnyTubeLeave(wg AnyWaiter) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeLeave(inp, wg)
}
}
// AnyFiniLeave returns a closure around `AnyDoneLeave(_, wg)`
// registering throughput
// as departure
// on the given `sync.WaitGroup`.
func AnyFiniLeave(wg AnyWaiter) func(inp <-chan Any) (done <-chan struct{}) {
return func(inp <-chan Any) (done <-chan struct{}) {
return AnyDoneLeave(inp, wg)
}
}
// AnyDoneWait returns a channel to receive
// one signal
// after wg.Wait() has returned and out has been closed
// before close.
//
// Note: Use only *after* You've started flooding the facilities.
func AnyDoneWait(out chan<- Any, wg AnyWaiter) (done <-chan struct{}) {
cha := make(chan struct{})
go doneAnyWait(cha, out, wg)
return cha
}
func doneAnyWait(done chan<- struct{}, out chan<- Any, wg AnyWaiter) {
defer close(done)
wg.Wait()
close(out)
done <- struct{}{} // not really needed - but looks better
}
// AnyFiniWait returns a closure around `AnyDoneWait(_, wg)`.
func AnyFiniWait(wg AnyWaiter) func(out chan<- Any) (done <-chan struct{}) {
return func(out chan<- Any) (done <-chan struct{}) {
return AnyDoneWait(out, wg)
}
}
// End of AnyPipeEnter/Leave - Flapdoors observed by a Waiter
// ===========================================================================
// ===========================================================================
// Beg of AnyDoneFreq - receive a frequency histogram
// AnyDoneFreq returns a channel to receive
// a frequency histogram (as a `map[Any]int64`)
// upon close.
func AnyDoneFreq(inp <-chan Any) (freq <-chan map[Any]int64) {
cha := make(chan map[Any]int64)
go doneAnyFreq(cha, inp)
return cha
}
// AnyDoneFreqAttr returns a channel to receive
// a frequency histogram (as a `map[interface{}]int64`)
// upon close.
//
// `attr` provides the key to the frequency map.
// If `nil` is passed as `attr` then Any is used as key.
func AnyDoneFreqAttr(inp <-chan Any, attr func(a Any) interface{}) (freq <-chan map[interface{}]int64) {
cha := make(chan map[interface{}]int64)
go doneAnyFreqAttr(cha, inp, attr)
return cha
}
func doneAnyFreq(out chan<- map[Any]int64, inp <-chan Any) {
defer close(out)
freq := make(map[Any]int64)
for i := range inp {
freq[i]++
}
out <- freq
}
func doneAnyFreqAttr(out chan<- map[interface{}]int64, inp <-chan Any, attr func(a Any) interface{}) {
defer close(out)
freq := make(map[interface{}]int64)
if attr == nil { // Make `nil` value useful
attr = func(a Any) interface{} { return a }
}
for i := range inp {
freq[attr(i)]++
}
out <- freq
}
// End of AnyDoneFreq - receive a frequency histogram
// ===========================================================================
// ===========================================================================
// Beg of AnyPipeDone
// AnyPipeDone returns a channel to receive every `inp` before close and a channel to signal this closing.
func AnyPipeDone(inp <-chan Any) (out <-chan Any, done <-chan struct{}) {
cha := make(chan Any)
doit := make(chan struct{})
go pipeAnyDone(cha, doit, inp)
return cha, doit
}
func pipeAnyDone(out chan<- Any, done chan<- struct{}, inp <-chan Any) {
defer close(out)
defer close(done)
for i := range inp {
out <- i
}
done <- struct{}{}
}
// End of AnyPipeDone
// ===========================================================================
// ===========================================================================
// Beg of AnyPlug - graceful terminator
// AnyPlug returns a channel to receive every `inp` before close and a channel to signal this closing.
// Upon receipt of a stop signal,
// output is immediately closed,
// and for graceful termination
// any remaining input is drained before done is signalled.
func AnyPlug(inp <-chan Any, stop <-chan struct{}) (out <-chan Any, done <-chan struct{}) {
cha := make(chan Any)
doit := make(chan struct{})
go plugAny(cha, doit, inp, stop)
return cha, doit
}
func plugAny(out chan<- Any, done chan<- struct{}, inp <-chan Any, stop <-chan struct{}) {
defer close(done)
var end bool // shall we end?
var ok bool // did we read successfully?
var e Any // what we've read
for !end {
select {
case e, ok = <-inp:
if ok {
out <- e
} else {
end = true
}
case <-stop:
end = true
}
}
close(out)
for range inp {
// drain inp
}
done <- struct{}{}
}
// End of AnyPlug - graceful terminator
// ===========================================================================
// ===========================================================================
// Beg of AnyPlugAfter - graceful terminator
// AnyPlugAfter returns a channel to receive every `inp` before close and a channel to signal this closing.
// Upon receipt of a time signal
// (e.g. from `time.After(...)`),
// output is immediately closed,
// and for graceful termination
// any remaining input is drained before done is signalled.
func AnyPlugAfter(inp <-chan Any, after <-chan time.Time) (out <-chan Any, done <-chan struct{}) {
cha := make(chan Any)
doit := make(chan struct{})
go plugAnyAfter(cha, doit, inp, after)
return cha, doit
}
func plugAnyAfter(out chan<- Any, done chan<- struct{}, inp <-chan Any, after <-chan time.Time) {
defer close(done)
var end bool // shall we end?
var ok bool // did we read successfully?
var e Any // what we've read
for !end {
select {
case e, ok = <-inp:
if ok {
out <- e
} else {
end = true
}
case <-after:
end = true
}
}
close(out)
for range inp {
// drain inp
}
done <- struct{}{}
}
// End of AnyPlugAfter - graceful terminator
// ===========================================================================
// ===========================================================================
// Beg of AnySema - limited parallel execution
// AnyPipeFuncMany returns a channel to receive
// every result of action `act` applied to `inp`
// by `many` parallel processing goroutines
// before close.
//
// ref: database/sql/sql_test.go
// ref: cmd/compile/internal/gc/noder.go
//
func AnyPipeFuncMany(inp <-chan Any, act func(a Any) Any, many int) (out <-chan Any) {
cha := make(chan Any)
if act == nil { // Make `nil` value useful
act = func(a Any) Any { return a }
}
if many < 1 {
many = 1
}
go pipeAnyFuncMany(cha, inp, act, many)
return cha
}
func pipeAnyFuncMany(out chan<- Any, inp <-chan Any, act func(a Any) Any, many int) {
defer close(out)
sem := make(chan struct{}, many)
var wg sync.WaitGroup
for i := range inp {
sem <- struct{}{}
wg.Add(1)
go func(i Any) {
defer func() {
<-sem
wg.Done()
}()
out <- act(i) // apply action
}(i)
}
wg.Wait()
}
// End of AnySema - limited parallel execution
// ===========================================================================
// Note: pipeAnyAdjust imports "container/ring" for the expanding buffer.
// ===========================================================================
// Beg of AnyPipeAdjust
// AnyPipeAdjust returns a channel to receive
// all `inp`
// buffered by a AnySendProxy process
// before close.
func AnyPipeAdjust(inp <-chan Any, sizes ...int) (out <-chan Any) {
cap, que := sendAnyProxySizes(sizes...)
cha := make(chan Any, cap)
go pipeAnyAdjust(cha, inp, que)
return cha
}
// AnyTubeAdjust returns a closure around AnyPipeAdjust (_, sizes ...int).
func AnyTubeAdjust(sizes ...int) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeAdjust(inp, sizes...)
}
}
// End of AnyPipeAdjust
// ===========================================================================
// ===========================================================================
// Beg of sendAnyProxy
func sendAnyProxySizes(sizes ...int) (cap, que int) {
// CAP is the minimum capacity of the buffered proxy channel in `AnySendProxy`
const CAP = 10
// QUE is the minimum initially allocated size of the circular queue in `AnySendProxy`
const QUE = 16
cap = CAP
que = QUE
if len(sizes) > 0 && sizes[0] > CAP {
que = sizes[0]
}
if len(sizes) > 1 && sizes[1] > QUE {
que = sizes[1]
}
if len(sizes) > 2 {
panic("AnySendProxy: too many sizes")
}
return
}
// AnySendProxy returns a channel to serve as a sending proxy to 'out'.
// Uses a goroutine to receive values from 'out' and store them
// in an expanding buffer, so that sending to 'out' never blocks.
// Note: the expanding buffer is implemented via "container/ring"
//
// Note: AnySendProxy is kept for the Sieve example
// and other dynamic use to be discovered
// even so it does not fit the pipe tube pattern as AnyPipeAdjust does.
func AnySendProxy(out chan<- Any, sizes ...int) chan<- Any {
cap, que := sendAnyProxySizes(sizes...)
cha := make(chan Any, cap)
go pipeAnyAdjust(out, cha, que)
return cha
}
// pipeAnyAdjust uses an adjusting buffer to receive from 'inp'
// even so 'out' is not ready to receive yet. The buffer may grow
// until 'inp' is closed and then will shrink by every send to 'out'.
// Note: the adjusting buffer is implemented via "container/ring"
func pipeAnyAdjust(out chan<- Any, inp <-chan Any, QUE int) {
defer close(out)
n := QUE // the allocated size of the circular queue
first := ring.New(n)
last := first
var c chan<- Any
var e Any
ok := true
for ok {
c = out
if first == last {
c = nil // buffer empty: disable output
} else {
e = first.Value.(Any)
}
select {
case e, ok = <-inp:
if ok {
last.Value = e
if last.Next() == first {
last.Link(ring.New(n)) // buffer full: expand it
n *= 2
}
last = last.Next()
}
case c <- e:
first = first.Next()
}
}
for first != last {
out <- first.Value.(Any)
first = first.Unlink(1) // first.Next()
}
}
// End of sendAnyProxy
// ===========================================================================
// ===========================================================================
// Beg of AnyFanOut
// AnyFanOut returns a slice (of size = size) of channels
// each of which shall receive any inp before close.
func AnyFanOut(inp <-chan Any, size int) (outS [](<-chan Any)) {
chaS := make([]chan Any, size)
for i := 0; i < size; i++ {
chaS[i] = make(chan Any)
}
go fanAnyOut(inp, chaS...)
outS = make([]<-chan Any, size)
for i := 0; i < size; i++ {
outS[i] = (<-chan Any)(chaS[i]) // convert `chan` to `<-chan`
}
return outS
}
// c fanAnyOut(inp <-chan Any, outs ...chan<- Any) {
func fanAnyOut(inp <-chan Any, outs ...chan Any) {
for i := range inp {
for o := range outs {
outs[o] <- i
}
}
for o := range outs {
close(outs[o])
}