-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
209 lines (193 loc) · 6.43 KB
/
producer.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package benthosx
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/Jeffail/benthos/lib/config"
"github.com/Jeffail/benthos/lib/log"
"github.com/Jeffail/benthos/lib/manager"
"github.com/Jeffail/benthos/lib/message"
"github.com/Jeffail/benthos/lib/metrics"
"github.com/Jeffail/benthos/lib/output"
"github.com/Jeffail/benthos/lib/pipeline"
"github.com/Jeffail/benthos/lib/types"
)
// NewProducer uses the given Benthos configuration to create a Producer
// instance that may be used as either a client in other code or as input
// for constructing a Lambda function.
func NewProducer(conf *config.Type) (*BenthosProducer, error) {
// Logging and stats aggregation.
logger := log.New(os.Stdout, conf.Logger)
// Create our metrics type.
stats, err := metrics.New(conf.Metrics, metrics.OptSetLogger(logger))
for err != nil {
logger.Errorf("Failed to connect metrics aggregator: %v\n", err)
stats = metrics.Noop()
}
// piplineLayer represents the processing pipeline that a message will
// pass through.
var pipelineLayer pipeline.Type
// outputLayer represents the composite of the various destination/outputs
// defined by the configuration.
var outputLayer output.Type
// pipelineInput is the entry point to the pipeline for all events
// received by the Lambda.
var pipelineInput = make(chan types.Transaction, 1)
// manager manages statefull resources like caches, rate limits, and system
// conditions that are shared across the entire runtime.
var mgr *manager.Type
// For compatibility we map the default output option to serverless.
if conf.Output.Type == output.TypeSTDOUT {
conf.Output.Type = TypeServerless
}
mgr, err = manager.New(conf.Manager, types.NoopMgr(), logger, stats)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %s", err.Error())
}
pipelineLayer, err = pipeline.New(
conf.Pipeline, mgr,
logger.NewModule(".pipeline"), metrics.Namespaced(stats, "pipeline"),
)
if err != nil {
return nil, fmt.Errorf("failed to create pipeline: %s", err.Error())
}
outputLayer, err = output.New(
conf.Output, mgr,
logger.NewModule(".output"), metrics.Namespaced(stats, "output"),
)
if err != nil {
return nil, fmt.Errorf("failed to create output: %s", err.Error())
}
if err := pipelineLayer.Consume(pipelineInput); err != nil {
return nil, fmt.Errorf("failed to connect pipeline: %s", err.Error())
}
if err := outputLayer.Consume(pipelineLayer.TransactionChan()); err != nil {
return nil, fmt.Errorf("failed to connect output: %s", err.Error())
}
closeFn := func() error {
exitTimeout := time.Second * 30
timesOut := time.Now().Add(exitTimeout)
pipelineLayer.CloseAsync()
outputLayer.CloseAsync()
_ = outputLayer.WaitForClose(exitTimeout)
_ = pipelineLayer.WaitForClose(time.Until(timesOut))
mgr.CloseAsync()
_ = mgr.WaitForClose(time.Until(timesOut))
return nil
}
return &BenthosProducer{
PipelineInput: pipelineInput,
CloseFn: closeFn,
}, nil
}
// BenthosProducer uses a set of Benthos transaction channels to coordinate
// processing and outputting an event.
type BenthosProducer struct {
// PipelineInput will be sent the raw message received from the call to
// Produce().
PipelineInput chan<- types.Transaction
// CloseFn will be called when the producer is closed. This is used to bind
// shutdown behavior for any long lived resources used to power the producer.
CloseFn func() error
}
// Close the producer. It is not valid to call Produce after calling Close.
func (p *BenthosProducer) Close() error {
return p.CloseFn()
}
// Produce an event to one or more outputs. The return is the final version
// of the event produces after being processed or an error if something
// went wrong. The input may be any type that can be marshaled to JSON.
func (p *BenthosProducer) Produce(ctx context.Context, in interface{}) (interface{}, error) {
// Convert the raw input into a Benthos message.
var msg = message.New(nil)
part := message.NewPart(nil)
if err := part.SetJSON(in); err != nil {
return nil, err
}
msgPart := message.WithContext(NewResponseContext(ctx), part)
msg.Append(msgPart)
resChan := make(chan types.Response, 1)
select {
case p.PipelineInput <- types.NewTransaction(msg, resChan):
case <-ctx.Done():
return nil, errors.New("request cancelled")
}
select {
case res := <-resChan:
if res.Error() != nil {
return nil, res.Error()
}
case <-ctx.Done():
return nil, errors.New("request cancelled")
}
r := ResponseFromContext(message.GetContext(msgPart))
if r.Len() == 0 {
// nothing set == no serverless outputs. use a canned response.
return map[string]interface{}{
"message": "request successful",
}, nil
}
if r.Len() == 1 {
var responseMsgs []types.Message
r.Range(func(key string, response []types.Message) bool {
responseMsgs = response
return false
})
if len(responseMsgs) == 1 {
if responseMsgs[0].Len() == 1 {
// one key, one msg, one part == {}
return responseMsgs[0].Get(0).JSON()
}
if responseMsgs[0].Len() > 1 {
// one key, one msg, multi part == [{},...]
results := make([]interface{}, 0, responseMsgs[0].Len())
err := responseMsgs[0].Iter(func(_ int, p types.Part) error {
jResult, err := p.JSON()
results = append(results, jResult)
return err
})
return results, err
}
// one key, one msg, zero parts == ???
// TODO: Define this case.
return nil, nil
}
// one key, multi message, any part == [[],[]]
results := make([][]interface{}, 0, len(responseMsgs))
for _, responseMsg := range responseMsgs {
msgParts := make([]interface{}, 0, responseMsg.Len())
if err := responseMsg.Iter(func(_ int, p types.Part) error {
jResult, err := p.JSON()
msgParts = append(msgParts, jResult)
return err
}); err != nil {
return nil, err
}
results = append(results, msgParts)
}
return results, nil
}
// multi-key, any msg, any part == {"k": [[]]}
results := make(map[string][][]interface{})
var err error // captures any JSON errors in the range function
r.Range(func(k string, msgs []types.Message) bool {
kResults := make([][]interface{}, 0, len(msgs))
for _, responseMsg := range msgs {
msgParts := make([]interface{}, 0, responseMsg.Len())
err = responseMsg.Iter(func(_ int, p types.Part) error {
jResult, er := p.JSON()
msgParts = append(msgParts, jResult)
return er
})
if err != nil {
return false
}
kResults = append(kResults, msgParts)
}
results[k] = kResults
return true
})
return results, err
}