-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathevent-driven-01.go
210 lines (167 loc) · 3.82 KB
/
event-driven-01.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
// This program demonstrates the event-driven processing concepts found in the
// book Designing Distributed System
package main
import (
"context"
"log"
"sync"
)
func main() {
generator := func(ctx context.Context, limit int) <-chan interface{} {
outStream := make(chan interface{})
go func() {
for i := 0; i < limit; i++ {
select {
case <-ctx.Done():
return
case outStream <- i:
}
}
// Preferable over `defer close(outStream)` due to the overhead
// of calling defer
close(outStream)
}()
return outStream
}
copier := func(ctx context.Context, inStream <-chan interface{}) (<-chan interface{}, <-chan interface{}) {
outStream1 := make(chan interface{})
outStream2 := make(chan interface{})
go func() {
for i := range inStream {
select {
case <-ctx.Done():
return
case outStream1 <- i:
}
select {
case <-ctx.Done():
return
case outStream2 <- i:
}
}
close(outStream1)
close(outStream2)
}()
return outStream1, outStream2
}
filter := func(ctx context.Context, inStream <-chan interface{}, fn func(interface{}) bool) <-chan interface{} {
outStream := make(chan interface{})
go func() {
for i := range inStream {
if !fn(i) {
continue
}
select {
case <-ctx.Done():
return
case outStream <- i:
}
}
close(outStream)
}()
return outStream
}
splitter := func(ctx context.Context, inStream <-chan interface{}, fn func(interface{}) bool) (<-chan interface{}, <-chan interface{}) {
outStream1 := make(chan interface{})
outStream2 := make(chan interface{})
go func() {
for i := range inStream {
if fn(i) {
select {
case <-ctx.Done():
return
case outStream1 <- i:
}
} else {
select {
case <-ctx.Done():
return
case outStream2 <- i:
}
}
}
close(outStream1)
close(outStream2)
}()
return outStream1, outStream2
}
sharder := func(ctx context.Context, inStream <-chan interface{}, fn func(interface{}) interface{}, numWorkers int) <-chan interface{} {
outStream := make(chan interface{})
var wg sync.WaitGroup
wg.Add(numWorkers)
worker := func(index int, in <-chan interface{}) {
for i := range in {
select {
case <-ctx.Done():
return
case outStream <- fn(i):
// log.Println("worker ", index, i)
}
}
wg.Done()
}
for i := 0; i < numWorkers; i++ {
go worker(i, inStream)
}
go func() {
wg.Wait()
close(outStream)
}()
return outStream
}
merger := func(ctx context.Context, streams ...<-chan interface{}) <-chan interface{} {
outStream := make(chan interface{})
var wg sync.WaitGroup
wg.Add(len(streams))
worker := func(in <-chan interface{}) {
for i := range in {
select {
case <-ctx.Done():
return
case outStream <- i:
}
}
wg.Done()
}
for _, i := range streams {
go worker(i)
}
go func() {
wg.Wait()
close(outStream)
}()
return outStream
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
numItems := 100
outStream1, outStream2 := copier(ctx, generator(ctx, numItems))
evenStream := filter(ctx, outStream1, even)
oddStream := filter(ctx, outStream2, odd)
greaterThanNumStream, lessThanNumStream := splitter(ctx, evenStream, greaterThan(numItems/2))
doubleOddStream := sharder(ctx, oddStream, double, 4)
out := merger(ctx, greaterThanNumStream, lessThanNumStream, doubleOddStream)
for o := range out {
log.Println(o)
}
log.Println("done")
}
func even(i interface{}) bool {
v := i.(int)
return v%2 == 0
}
func odd(i interface{}) bool {
v := i.(int)
return v%2 != 0
}
func double(i interface{}) interface{} {
v := i.(int)
return v * 2
}
func greaterThan(n int) func(interface{}) bool {
return func(i interface{}) bool {
v := i.(int)
return v > n
}
}