-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
122 lines (97 loc) · 2 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"runtime"
"sync"
"time"
)
const (
workerSize = 3
jobSize = 20
)
func worker(ctx context.Context, id int, job <-chan int, wg *sync.WaitGroup) <-chan int {
result := make(chan int)
go func() {
defer wg.Done()
defer close(result)
for {
select {
case <-ctx.Done():
return
case j, ok := <-job:
if !ok {
return
}
log.Printf("worker id: %d, start to deal with job: %d", id, j)
time.Sleep(1 * time.Second) // simulate the hard work
select {
// make sure if result channel is not writable during cancel(), we still could exit in this goroutine
case <-ctx.Done():
return
case result <- j * 2:
}
log.Printf("worker id: %d, end to deal with job: %d", id, j)
}
}
}()
return result
}
func genJobs(ctx context.Context) <-chan int {
jobs := make(chan int)
go func() {
defer close(jobs)
for i := 0; i < jobSize; i++ {
select {
case <-ctx.Done():
return
case jobs <- i:
}
}
}()
return jobs
}
func fanIn(ctx context.Context, sources ...<-chan int) <-chan int {
target := make(chan int)
go func() {
defer close(target)
wg := sync.WaitGroup{}
for _, source := range sources {
wg.Add(1)
go func(source <-chan int, target chan int) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case val, ok := <-source:
if !ok {
return
}
target <- val
}
}
}(source, target)
}
wg.Wait()
}()
return target
}
func main() {
wg := &sync.WaitGroup{} // for detecting worker all closed
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
jobs := genJobs(ctx)
results := make([]<-chan int, workerSize)
for i := 0; i < workerSize; i++ {
wg.Add(1)
results[i] = worker(ctx, i, jobs, wg)
}
flow := fanIn(ctx, results...)
for data := range flow {
log.Printf("Result is :%d", data)
}
wg.Wait()
fmt.Printf("expected 1 goroutine, got goroutine: %d\n", runtime.NumGoroutine())
}