-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
42 lines (34 loc) · 988 Bytes
/
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
package sqsdr
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/sqs"
)
// Pipeline is a simple struct to manage the interaction between a source, a chooser, and sinks.
// Pipeline satisfies the Handler interface and can be passed to a Poller.
type Pipeline struct {
Chooser Chooser
LeftSink Sinker
RightSink Sinker
}
// Handle is the entry point into the pipeline
func (p *Pipeline) Handle(ctx context.Context, msgs []*sqs.Message) ([]*sqs.Message, error) {
leftMsgs, rightMsgs := p.Chooser.Choose(msgs)
var leftError error
if len(leftMsgs) > 0 {
leftError = p.LeftSink.Sink(ctx, leftMsgs)
}
var rightError error
if len(rightMsgs) > 0 {
rightError = p.RightSink.Sink(ctx, rightMsgs)
}
var err error
if rightError != nil && leftError != nil {
err = fmt.Errorf("rightSink error: %v\nleftSink error: %v", rightError, leftError)
} else if rightError != nil {
err = rightError
} else if leftError != nil {
err = leftError
}
return msgs, err
}