-
Notifications
You must be signed in to change notification settings - Fork 0
/
destination.go
188 lines (155 loc) · 4.4 KB
/
destination.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
package benthos
import (
"context"
"fmt"
"github.com/benthosdev/benthos/v4/public/service"
sdk "github.com/conduitio/conduit-connector-sdk"
"sync"
)
type Destination struct {
sdk.UnimplementedDestination
config DestinationConfig
records chan sdk.Record
benthosStream *service.Stream
cancelBenthos context.CancelFunc
errC chan batchError
mu sync.Mutex
inFlight int
done chan struct{}
}
type batchError struct {
err error
written int
}
func NewDestination() sdk.Destination {
return &Destination{
records: make(chan sdk.Record),
done: make(chan struct{}),
}
}
func (d *Destination) Connect(ctx context.Context) error {
return nil
}
func (d *Destination) Read(ctx context.Context) (*service.Message, service.AckFunc, error) {
sdk.Logger(ctx).Debug().Msgf("reading record...")
rec := <-d.records
sdk.Logger(ctx).Debug().Msgf("got a record to read")
return d.toMessage(rec),
func(ctx context.Context, err error) error {
sdk.Logger(ctx).Debug().Msgf("service.AckFunc called")
d.mu.Lock()
if err != nil {
if d.errC == nil {
sdk.Logger(ctx).Debug().Msg("Read: initializing error channel")
d.errC = make(chan batchError)
}
d.errC <- batchError{
err: err,
written: d.inFlight,
}
}
d.inFlight--
if d.inFlight == 0 {
d.done <- struct{}{}
}
d.mu.Unlock()
return nil
},
nil
}
func (d *Destination) Close(ctx context.Context) error {
return nil
}
func (d *Destination) Parameters() map[string]sdk.Parameter {
return map[string]sdk.Parameter{
BenthosYaml: {
Default: "localhost:10000",
Required: true,
Description: "The URL of the server.",
},
}
}
func (d *Destination) Configure(ctx context.Context, cfg map[string]string) error {
sdk.Logger(ctx).Debug().Msg("Configuring a Destination connector...")
config, err := ParseDestinationConfig(cfg)
if err != nil {
return err
}
d.config = config
return nil
}
func (d *Destination) Open(ctx context.Context) error {
sdk.Logger(ctx).Debug().Msgf("opening destination...")
builder := service.NewStreamBuilder()
builder.DisableLinting()
// Register our new output, which doesn't require a config schema.
err := service.RegisterInput(
"conduit_destination_input",
service.NewConfigSpec(),
func(conf *service.ParsedConfig, mgr *service.Resources) (service.Input, error) {
return service.AutoRetryNacks(d), nil
},
)
if err != nil {
return fmt.Errorf("failed registering Benthos output: %w", err)
}
err = builder.SetYAML(d.config.benthosYaml)
if err != nil {
return fmt.Errorf("failed parsing Benthos YAML config: %w", err)
}
builder.AddInputYAML(`
label: "label_conduit_destination_input"
conduit_destination_input: {}
`)
// Build a stream with our configured components.
stream, err := builder.Build()
if err != nil {
return fmt.Errorf("failed building Benthos stream: %w", err)
}
d.benthosStream = stream
// And run it, blocking until it gracefully terminates once the generate
// input has generated a message, and it has flushed through the stream.
benthosCtx, cancelBenthos := context.WithCancel(context.Background())
d.cancelBenthos = cancelBenthos
go func() {
sdk.Logger(ctx).Debug().Msgf("running stream...")
err = stream.Run(benthosCtx)
sdk.Logger(ctx).Err(err).Msg("benthos: stream done running")
d.errC <- batchError{err: err}
}()
return nil
}
func (d *Destination) Write(ctx context.Context, records []sdk.Record) (int, error) {
sdk.Logger(ctx).Debug().Msgf("writing %v records...", len(records))
d.inFlight = len(records)
for _, r := range records {
sdk.Logger(ctx).Debug().Msg("Write: putting record into channel")
d.records <- r
}
if d.errC == nil {
sdk.Logger(ctx).Debug().Msg("Write: initializing error channel")
d.errC = make(chan batchError)
}
sdk.Logger(ctx).Debug().Msg("Write: select")
select {
case <-d.done:
sdk.Logger(ctx).Debug().Msgf("done writing records")
return len(records), nil
case err := <-d.errC:
sdk.Logger(ctx).Err(err.err).
Int("inFlight", d.inFlight).
Msgf("error while writing records")
return len(records) - d.inFlight, err.err
}
}
func (d *Destination) Teardown(ctx context.Context) error {
sdk.Logger(ctx).Debug().Msg("teardown started...")
if d.cancelBenthos != nil {
d.cancelBenthos()
}
sdk.Logger(ctx).Debug().Msg("teardown done!")
return nil
}
func (d *Destination) toMessage(rec sdk.Record) *service.Message {
return service.NewMessage(rec.Bytes())
}