-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfifo.go
64 lines (57 loc) · 1.2 KB
/
fifo.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
package pipeline
import (
"context"
"fmt"
)
type fifo struct {
id string
task Task
}
// FIFO returns a Stage that processes incoming data in a first-in first-out
// fashion. Each input is passed to the specified Task and its output
// is emitted to the next Stage.
func FIFO(id string, task Task) Stage {
return &fifo{
id: id,
task: task,
}
}
// ID implements Stage.
func (r *fifo) ID() string {
return r.id
}
// Run implements Stage.
func (r *fifo) Run(ctx context.Context, sp StageParams) {
for {
if !processStageData(ctx, sp, r.executeTask) {
break
}
}
}
func (r *fifo) executeTask(ctx context.Context, data Data, sp StageParams) (Data, error) {
select {
case <-ctx.Done():
return nil, nil
default:
}
dataOut, err := r.task.Process(ctx, data, &taskParams{
pipeline: sp.Pipeline(),
registry: sp.Registry(),
})
if err != nil {
e := fmt.Errorf("pipeline stage %d: %v", sp.Position(), err)
sp.Error().Append(e)
return dataOut, e
}
// If the task did not output data for the
// next stage there is nothing we need to do
if dataOut == nil {
return nil, nil
}
// Output processed data
select {
case <-ctx.Done():
case sp.Output() <- dataOut:
}
return dataOut, nil
}