@@ -23,14 +23,17 @@ import (
23
23
24
24
type Pipeline struct {
25
25
inputs []plugin.Plugin
26
+ filters []plugin.Plugin
26
27
outputs []plugin.Plugin
28
+ filterChan chan event.Event
27
29
outputChan chan event.Event
28
30
errorChan chan error
29
31
doneChan chan bool
30
32
}
31
33
32
34
func New () * Pipeline {
33
35
p := & Pipeline {
36
+ filterChan : make (chan event.Event ),
34
37
outputChan : make (chan event.Event ),
35
38
errorChan : make (chan error ),
36
39
doneChan : make (chan bool ),
@@ -42,6 +45,10 @@ func (p *Pipeline) AddInput(input plugin.Plugin) {
42
45
p .inputs = append (p .inputs , input )
43
46
}
44
47
48
+ func (p * Pipeline ) AddFilter (filter plugin.Plugin ) {
49
+ p .filters = append (p .filters , filter )
50
+ }
51
+
45
52
func (p * Pipeline ) AddOutput (output plugin.Plugin ) {
46
53
p .outputs = append (p .outputs , output )
47
54
}
@@ -57,11 +64,35 @@ func (p *Pipeline) Start() error {
57
64
if err := input .Start (); err != nil {
58
65
return fmt .Errorf ("failed to start input: %s" , err )
59
66
}
60
- // Start background process to send input events to combined output channel
61
- go p .chanCopyLoop (input .OutputChan (), p .outputChan )
67
+ // Start background process to send input events to combined filter channel
68
+ go p .chanCopyLoop (input .OutputChan (), p .filterChan )
62
69
// Start background error listener
63
70
go p .errorChanWait (input .ErrorChan ())
64
71
}
72
+ // Start filters
73
+ for idx , filter := range p .filters {
74
+ if err := filter .Start (); err != nil {
75
+ return fmt .Errorf ("failed to start input: %s" , err )
76
+ }
77
+ if idx == 0 {
78
+ // Start background process to send events from combined filter channel to first filter plugin
79
+ go p .chanCopyLoop (p .filterChan , filter .InputChan ())
80
+ } else {
81
+ // Start background process to send events from previous filter plugin to current filter plugin
82
+ go p .chanCopyLoop (p .filters [idx - 1 ].OutputChan (), filter .InputChan ())
83
+ }
84
+ if idx == len (p .filters )- 1 {
85
+ // Start background process to send events from last filter to combined output channel
86
+ go p .chanCopyLoop (filter .OutputChan (), p .outputChan )
87
+ }
88
+ // Start background error listener
89
+ go p .errorChanWait (filter .ErrorChan ())
90
+ }
91
+ if len (p .filters ) == 0 {
92
+ // Start background process to send events from combined filter channel to combined output channel if
93
+ // there are no filter plugins
94
+ go p .chanCopyLoop (p .filterChan , p .outputChan )
95
+ }
65
96
// Start outputs
66
97
for _ , output := range p .outputs {
67
98
if err := output .Start (); err != nil {
@@ -78,6 +109,7 @@ func (p *Pipeline) Start() error {
78
109
func (p * Pipeline ) Stop () error {
79
110
close (p .doneChan )
80
111
close (p .errorChan )
112
+ close (p .filterChan )
81
113
close (p .outputChan )
82
114
// Stop inputs
83
115
for _ , input := range p .inputs {
0 commit comments