-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipeline.go
97 lines (86 loc) · 1.83 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
93
94
95
96
97
package gpool
import (
"sync"
)
// Proc ...
type Proc func(<-chan interface{}, chan<- interface{})
// Stage ...
type Stage struct {
proc Proc
concurrent int
}
// NewStage ...
func NewStage(concurrent int, proc Proc) *Stage {
// For safety
if concurrent < 1 {
concurrent = 1
}
return &Stage{
proc: proc,
concurrent: concurrent,
}
}
// Pipeline ...
type Pipeline struct {
stages []*Stage
}
// New ...
func NewPipeline(stages ...*Stage) (p *Pipeline) {
p = &Pipeline{make([]*Stage, 0, len(stages))}
for _, s := range stages {
p.Add(s)
}
return
}
// Add ...
func (p *Pipeline) Add(s *Stage) {
p.stages = append(p.stages, s)
}
// Start ...
func (p *Pipeline) Start() (chan<- interface{}, <-chan interface{}) {
input := make(chan interface{})
output := input
for _, s := range p.stages {
wg := &sync.WaitGroup{}
prev := output
output = make(chan interface{})
for i := 0; i < s.concurrent; i++ {
wg.Add(1)
go func(s *Stage, in <-chan interface{}, out chan<- interface{}) {
s.proc(in, out)
wg.Done()
}(s, prev, output)
}
go func(s *Stage, out chan<- interface{}) {
wg.Wait()
close(out)
}(s, output)
}
return input, output
}
type BranchProc func(<-chan interface{}, []chan<- interface{})
func BranchRun(proc BranchProc, concurrent int, in <-chan interface{}, outs []chan<- interface{}) {
var wg sync.WaitGroup
for i := 0; i < concurrent; i++ {
wg.Add(1)
go func(in <-chan interface{}, outs []chan<- interface{}) {
proc(in, outs)
wg.Done()
}(in, outs)
}
go func(outs []chan<- interface{}) {
wg.Wait()
for _, out := range outs {
close(out)
}
}(outs)
}
func Branch(in <-chan interface{}, outs []chan<- interface{}) {
BranchRun(func(in <-chan interface{}, outs []chan<- interface{}) {
for v := range in {
for _, out := range outs {
out <- v
}
}
}, 1, in, outs)
}