-
Notifications
You must be signed in to change notification settings - Fork 2
/
stage.go
63 lines (50 loc) · 995 Bytes
/
stage.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
package pipeline
import (
"log"
"sync"
)
type StageWorker struct {
wg *sync.WaitGroup
input chan Message
output chan Message
concurrency int
pipe Stage
}
func (w *StageWorker) Start() error {
for i := 0; i < w.concurrency; i++ {
w.wg.Add(1)
go func() {
defer w.wg.Done()
for i := range w.Input() {
result, err := w.pipe.Process(i)
if err != nil {
log.Println(err)
continue
}
for _, r := range result {
w.Output() <- r
}
}
}()
}
return nil
}
func (w *StageWorker) WaitStop() error {
w.wg.Wait()
return nil
}
func (w *StageWorker) Input() chan Message {
return w.input
}
func (w *StageWorker) Output() chan Message {
return w.output
}
func NewWorkerGroup(concurrency int, pipe Stage, input chan Message, output chan Message) StageWorker {
return StageWorker{
wg: &sync.WaitGroup{},
input: input,
output: output,
concurrency: concurrency,
pipe: pipe,
}
}