forked from benthosdev/benthos
/
input.go
289 lines (251 loc) · 9.38 KB
/
input.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package service
import (
"context"
"errors"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/input"
"github.com/benthosdev/benthos/v4/internal/component/input/batcher"
"github.com/benthosdev/benthos/v4/internal/message"
)
// AckFunc is a common function returned by inputs that must be called once for
// each message consumed. This function ensures that the source of the message
// receives either an acknowledgement (err is nil) or an error that can either
// be propagated upstream as a nack, or trigger a reattempt at delivering the
// same message.
//
// If your input implementation doesn't have a specific mechanism for dealing
// with a nack then you can wrap your input implementation with AutoRetryNacks
// to get automatic retries.
type AckFunc func(ctx context.Context, err error) error
// Input is an interface implemented by Benthos inputs. Calls to Read should
// block until either a message has been received, the connection is lost, or
// the provided context is cancelled.
type Input interface {
// Establish a connection to the upstream service. Connect will always be
// called first when a reader is instantiated, and will be continuously
// called with back off until a nil error is returned.
//
// The provided context remains open only for the duration of the connecting
// phase, and should not be used to establish the lifetime of the connection
// itself.
//
// Once Connect returns a nil error the Read method will be called until
// either ErrNotConnected is returned, or the reader is closed.
Connect(context.Context) error
// Read a single message from a source, along with a function to be called
// once the message can be either acked (successfully sent or intentionally
// filtered) or nacked (failed to be processed or dispatched to the output).
//
// The AckFunc will be called for every message at least once, but there are
// no guarantees as to when this will occur. If your input implementation
// doesn't have a specific mechanism for dealing with a nack then you can
// wrap your input implementation with AutoRetryNacks to get automatic
// retries.
//
// If this method returns ErrNotConnected then Read will not be called again
// until Connect has returned a nil error. If ErrEndOfInput is returned then
// Read will no longer be called and the pipeline will gracefully terminate.
Read(context.Context) (*Message, AckFunc, error)
Closer
}
//------------------------------------------------------------------------------
// BatchInput is an interface implemented by Benthos inputs that produce
// messages in batches, where there is a desire to process and send the batch as
// a logical group rather than as individual messages.
//
// Calls to ReadBatch should block until either a message batch is ready to
// process, the connection is lost, or the provided context is cancelled.
type BatchInput interface {
// Establish a connection to the upstream service. Connect will always be
// called first when a reader is instantiated, and will be continuously
// called with back off until a nil error is returned.
//
// The provided context remains open only for the duration of the connecting
// phase, and should not be used to establish the lifetime of the connection
// itself.
//
// Once Connect returns a nil error the Read method will be called until
// either ErrNotConnected is returned, or the reader is closed.
Connect(context.Context) error
// Read a message batch from a source, along with a function to be called
// once the entire batch can be either acked (successfully sent or
// intentionally filtered) or nacked (failed to be processed or dispatched
// to the output).
//
// The AckFunc will be called for every message batch at least once, but
// there are no guarantees as to when this will occur. If your input
// implementation doesn't have a specific mechanism for dealing with a nack
// then you can wrap your input implementation with AutoRetryNacksBatched to
// get automatic retries.
//
// If this method returns ErrNotConnected then ReadBatch will not be called
// again until Connect has returned a nil error. If ErrEndOfInput is
// returned then Read will no longer be called and the pipeline will
// gracefully terminate.
ReadBatch(context.Context) (MessageBatch, AckFunc, error)
Closer
}
//------------------------------------------------------------------------------
// Implements input.AsyncReader.
type airGapReader struct {
r Input
}
func newAirGapReader(r Input) input.Async {
return &airGapReader{r: r}
}
func (a *airGapReader) Connect(ctx context.Context) error {
err := a.r.Connect(ctx)
if err != nil && errors.Is(err, ErrEndOfInput) {
err = component.ErrTypeClosed
}
return err
}
func (a *airGapReader) ReadBatch(ctx context.Context) (message.Batch, input.AsyncAckFn, error) {
msg, ackFn, err := a.r.Read(ctx)
if err != nil {
if errors.Is(err, ErrNotConnected) {
err = component.ErrNotConnected
} else if errors.Is(err, ErrEndOfInput) {
err = component.ErrTypeClosed
}
return nil, nil, err
}
tMsg := message.Batch{msg.part}
return tMsg, func(c context.Context, r error) error {
return ackFn(c, r)
}, nil
}
func (a *airGapReader) Close(ctx context.Context) error {
return a.r.Close(ctx)
}
//------------------------------------------------------------------------------
// Implements input.AsyncReader.
type airGapBatchReader struct {
r BatchInput
}
func newAirGapBatchReader(r BatchInput) input.Async {
return &airGapBatchReader{r: r}
}
func (a *airGapBatchReader) Connect(ctx context.Context) error {
err := a.r.Connect(ctx)
if err != nil && errors.Is(err, ErrEndOfInput) {
err = component.ErrTypeClosed
}
return err
}
func (a *airGapBatchReader) ReadBatch(ctx context.Context) (message.Batch, input.AsyncAckFn, error) {
batch, ackFn, err := a.r.ReadBatch(ctx)
if err != nil {
if errors.Is(err, ErrNotConnected) {
err = component.ErrNotConnected
} else if errors.Is(err, ErrEndOfInput) {
err = component.ErrTypeClosed
}
return nil, nil, err
}
mBatch := make(message.Batch, len(batch))
for i, p := range batch {
mBatch[i] = p.part
}
return mBatch, func(c context.Context, r error) error {
r = toPublicBatchError(r)
return ackFn(c, r)
}, nil
}
func (a *airGapBatchReader) Close(ctx context.Context) error {
return a.r.Close(ctx)
}
//------------------------------------------------------------------------------
// ResourceInput provides access to an input resource.
type ResourceInput struct {
i input.Streamed
}
func newResourceInput(i input.Streamed) *ResourceInput {
return &ResourceInput{i: i}
}
// ReadBatch attempts to read a message batch from the input, along with a
// function to be called once the entire batch can be either acked (successfully
// sent or intentionally filtered) or nacked (failed to be processed or
// dispatched to the output).
//
// If this method returns ErrEndOfInput then that indicates that the input has
// finished and will no longer yield new messages.
func (r *ResourceInput) ReadBatch(ctx context.Context) (MessageBatch, AckFunc, error) {
var tran message.Transaction
var open bool
select {
case tran, open = <-r.i.TransactionChan():
case <-ctx.Done():
return nil, nil, ctx.Err()
}
if !open {
return nil, nil, ErrEndOfInput
}
var b MessageBatch
_ = tran.Payload.Iter(func(i int, part *message.Part) error {
b = append(b, NewInternalMessage(part))
return nil
})
return b, func(c context.Context, r error) error {
r = fromPublicBatchError(r)
return tran.Ack(c, r)
}, nil
}
//------------------------------------------------------------------------------
// OwnedInput provides direct ownership of an input extracted from a plugin
// config. Connectivity of the input is handled internally, and so the consumer
// of this type should only be concerned with reading messages and eventually
// calling Close to terminate the input.
type OwnedInput struct {
i input.Streamed
}
// BatchedWith returns a copy of the OwnedInput where messages will be batched
// according to the provided batcher.
func (o *OwnedInput) BatchedWith(b *Batcher) *OwnedInput {
return &OwnedInput{
i: batcher.New(b.p, o.i, b.mgr.Logger()),
}
}
// ReadBatch attempts to read a message batch from the input, along with a
// function to be called once the entire batch can be either acked (successfully
// sent or intentionally filtered) or nacked (failed to be processed or
// dispatched to the output).
//
// If this method returns ErrEndOfInput then that indicates that the input has
// finished and will no longer yield new messages.
func (o *OwnedInput) ReadBatch(ctx context.Context) (MessageBatch, AckFunc, error) {
var tran message.Transaction
var open bool
select {
case tran, open = <-o.i.TransactionChan():
case <-ctx.Done():
return nil, nil, ctx.Err()
}
if !open {
return nil, nil, ErrEndOfInput
}
var b MessageBatch
_ = tran.Payload.Iter(func(i int, part *message.Part) error {
b = append(b, NewInternalMessage(part))
return nil
})
return b, func(c context.Context, r error) error {
r = fromPublicBatchError(r)
return tran.Ack(c, r)
}, nil
}
// Close the input.
func (o *OwnedInput) Close(ctx context.Context) error {
o.i.TriggerStopConsuming()
return o.i.WaitForClose(ctx)
}
type inputUnwrapper struct {
i input.Streamed
}
func (w inputUnwrapper) Unwrap() input.Streamed {
return w.i
}
// XUnwrapper is for internal use only, do not use this.
func (o *OwnedInput) XUnwrapper() any {
return inputUnwrapper{i: o.i}
}