-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallelizer.go
103 lines (91 loc) · 2.75 KB
/
parallelizer.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
package queue
import (
"fmt"
"sync"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
)
type WorkFunc func(piece interface{})
// ParallelizerSimple parallelizes the processing of "pieces" by the WorkFunc
// It's simple in that it assumes the number of pieces are known before hand
// and will run untill all of them have been processed.
func ParallelizerSimple(workers int, pieces []interface{}, wf WorkFunc) {
// we write all the pieces into a channel that is exactly the length
// of the slice containing the pieces
toProcessChan := make(chan interface{}, len(pieces))
for _, p := range pieces {
toProcessChan <- p
}
close(toProcessChan)
var wg sync.WaitGroup
wg.Add(workers)
// we create the desired number of workers and have each worker
// read from the above chan the piece and call the desired WorkFunc
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
defer utilruntime.HandleCrash()
// Read from channel until it is closed
for p := range toProcessChan {
wf(p)
}
}()
}
wg.Wait()
}
// ParallelizerWithShutDown parallelizes the processing of "pieces" by the
// WorkFunc. It assumes the number of pieces are known before hand
// and will run untill all of them have been processed or it receives a
// shutdown request on the shutdown chan.
func ParallelizerWithShutDown(shutdown <-chan struct{}, workers int, pieces []interface{}, wf WorkFunc) {
// we write all the pieces into a channel that is exactly the length
// of the slice containing the pieces
toProcessChan := make(chan interface{}, len(pieces))
for _, p := range pieces {
toProcessChan <- p
}
close(toProcessChan)
var wg sync.WaitGroup
wg.Add(workers)
// we create the desired number of workers and have each worker
// read from the above chan the piece and call the desired WorkFunc
for i := 0; i < workers; i++ {
go func(i int) {
defer wg.Done()
defer utilruntime.HandleCrash()
// Read from channel until it is closed
for p := range toProcessChan {
select {
case <-shutdown:
fmt.Printf("Worker %v: Shutdown recieved\n", i)
return
default:
wf(p)
}
}
}(i)
}
wg.Wait()
}
// ParallelizerWithInputChan parallelizes the processing of "pieces" by the
// WorkFunc. It continues to read from the peices from the input chan, until
// the chan is closed or shutdown is called.
func ParallelizerWithInputChan(shutdown <-chan struct{}, workers int, inChan <-chan interface{}, wf WorkFunc) {
wg := sync.WaitGroup{}
wg.Add(workers)
for i := 0; i < workers; i++ {
go func(i int) {
defer wg.Done()
defer utilruntime.HandleCrash()
for { //p := range inChan {
select {
case <-shutdown:
fmt.Printf("Worker %v: Shutdown recieved\n", i)
return
case p := <-inChan:
wf(p)
}
}
}(i)
}
wg.Wait()
}