-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrency.go
112 lines (106 loc) · 2.29 KB
/
concurrency.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
package utils
// Bridge gets all values coming from a stream of channels and streams them on a single channel
func Bridge[T any, V ~chan T](done chan T, chanStream chan V) chan T {
valStream := make(chan T)
go func() {
defer close(valStream)
for {
var stream chan T
select {
case maybeStream, ok := <-chanStream:
if ok == false {
return
}
stream = maybeStream
case <-done:
return
}
for val := range OrDone[T](done, stream) {
select {
case valStream <- val:
case <-done:
}
}
}
}()
return valStream
}
// OrDone gets either reads from the input channel or returns if a
// value is fed to the done channel
func OrDone[T any](done, c chan T) chan T {
valStream := make(chan T)
go func() {
defer close(valStream)
for {
select {
case <-done:
return
case v, ok := <-c:
if ok == false {
return
}
select {
case valStream <- v:
case <-done:
}
}
}
}()
return valStream
}
// Tee de-multiplexes a channel into multiple receivers
func Tee[T any](in chan T, receivers ...chan T) {
go func() {
defer func() {
for i := range receivers {
close(receivers[i])
}
}()
for elem := range in {
for ii := 0; ii < len(receivers); ii++ {
receivers[ii] <- elem
}
}
}()
}
// TeeValue de-multiplexes a value into multiple receivers
func TeeValue[T any](in T, receivers ...chan T) {
for ii := 0; ii < len(receivers); ii++ {
receivers[ii] <- in
}
}
// Take is a generator utility and best used in combination with Repeat
// It reads a given number of values from a valueStream, but returns
// early if a value is placed in the done channel.
func Take[S, T any](done chan S, valueStream chan T, num int) chan T {
takeStream := make(chan T)
go func() {
defer close(takeStream)
for i := 0; i < num; i++ {
select {
case <-done:
return
case in := <-valueStream:
takeStream <- in
}
}
}()
return takeStream
}
// Repeat will repeat the values you pass to it infinitely until a value is placed in the done channel.
func Repeat[T any](done <-chan interface{}, v []T) <-chan T {
valueStream := make(chan T)
go func() {
defer close(valueStream)
for {
for _, val := range v {
select {
case <-done:
return
case valueStream <- val:
}
}
}
}()
return valueStream
}