-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
92 lines (74 loc) · 1.86 KB
/
pipeline.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
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
doneChan := make(chan bool)
params := make(chan int, 5)
params <- 1
params <- 2
params <- 3
params <- 4
params <- 5
// close channel, there is no new value send to this channel.
// NOTE : Even when channel is closed, go routine can still receive msg, but block for sent msg
// When using `range` to get msg from channel, we need to close channel to indicate that there is no value.
// If not closed, then `range` will continue to listen to the open channel
close(params)
pipeline(params, doneChan)
select {
case done := <-doneChan:
elapsed := time.Since(start)
fmt.Println("DONE", done, elapsed)
}
}
func pipeline(params <-chan int, doneChan chan<- bool) {
go func() {
for _ = range step3(step2(step1(params))) {
//fmt.Println(result)
}
doneChan <- true // mark as done, so it could trigger `select case done`
close(doneChan) // close done channel only after it's receive value true
}()
}
func step1(params <-chan int) <-chan string {
ch := make(chan string, len(params))
go func() {
for param := range params {
time.Sleep(1 * time.Second)
result := fmt.Sprintf("step1(%v)", param)
fmt.Println(result)
ch <- result
}
close(ch) // close channel
}()
return ch
}
func step2(params <-chan string) <-chan string {
ch := make(chan string, len(params))
go func() {
for param := range params {
time.Sleep(1 * time.Second)
result := fmt.Sprintf("step2(%v)", param)
fmt.Println(result)
ch <- result
}
close(ch) // close channel
}()
return ch
}
func step3(params <-chan string) <-chan string {
ch := make(chan string, len(params))
go func() {
for param := range params {
time.Sleep(1 * time.Second)
result := fmt.Sprintf("step3(%v)", param)
fmt.Println(result)
ch <- result
}
close(ch) // close channel
}()
return ch
}