-
Notifications
You must be signed in to change notification settings - Fork 44
/
util.go
114 lines (96 loc) · 2.62 KB
/
util.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
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
package flow
import (
"sync"
"github.com/superproj/onex/pkg/streams"
)
// DoStream streams data from the outlet to inlet.
func DoStream(outlet streams.Outlet, inlet streams.Inlet) {
go func() {
for element := range outlet.Out() {
inlet.In() <- element
}
close(inlet.In())
}()
}
// Split splits the stream into two flows according to the given boolean predicate.
func Split(outlet streams.Outlet, predicate func(any) bool) [2]streams.Flow {
condTrue := NewPassThrough()
condFalse := NewPassThrough()
go func() {
for element := range outlet.Out() {
if predicate(element) {
condTrue.In() <- element
} else {
condFalse.In() <- element
}
}
close(condTrue.In())
close(condFalse.In())
}()
return [...]streams.Flow{condTrue, condFalse}
}
// FanOut creates a number of identical flows from the single outlet.
// This can be useful when writing to multiple sinks is required.
func FanOut(outlet streams.Outlet, magnitude int) []streams.Flow {
var out []streams.Flow
for i := 0; i < magnitude; i++ {
out = append(out, NewPassThrough())
}
go func() {
for element := range outlet.Out() {
for _, socket := range out {
socket.In() <- element
}
}
for i := 0; i < magnitude; i++ {
close(out[i].In())
}
}()
return out
}
// RoundRobin creates a balanced number of flows from the single outlet.
// This can be useful when work can be parallelized across multiple cores.
func RoundRobin(outlet streams.Outlet, magnitude int) []streams.Flow {
out := make([]streams.Flow, magnitude)
for i := 0; i < magnitude; i++ {
out[i] = NewPassThrough()
go func(o streams.Flow) {
defer close(o.In())
for element := range outlet.Out() {
o.In() <- element
}
}(out[i])
}
return out
}
// Merge merges multiple flows into a single flow.
func Merge(outlets ...streams.Flow) streams.Flow {
merged := NewPassThrough()
var wg sync.WaitGroup
wg.Add(len(outlets))
for _, out := range outlets {
go func(outlet streams.Outlet) {
for element := range outlet.Out() {
merged.In() <- element
}
wg.Done()
}(out)
}
// close the in channel on the last outlet close.
go func(wg *sync.WaitGroup) {
wg.Wait()
close(merged.In())
}(&wg)
return merged
}
// Flatten creates a Flow to flatten the stream of slices.
func Flatten(parallelism uint) streams.Flow {
return NewFlatMap(func(element []any) []any {
return element
}, parallelism)
}