-
Notifications
You must be signed in to change notification settings - Fork 786
/
output.go
217 lines (181 loc) · 5.09 KB
/
output.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
package pulsar
import (
"context"
"sync"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/public/service"
)
func init() {
err := service.RegisterOutput(
"pulsar",
outputConfigSpec(),
func(conf *service.ParsedConfig, mgr *service.Resources) (service.Output, int, error) {
w, err := newPulsarWriterFromParsed(conf, mgr.Logger())
if err != nil {
return nil, 0, err
}
n, err := conf.FieldInt("max_in_flight")
if err != nil {
return nil, 0, err
}
return w, n, err
})
if err != nil {
panic(err)
}
}
func outputConfigSpec() *service.ConfigSpec {
return service.NewConfigSpec().
Version("3.43.0").
Categories("Services").
Summary("Write messages to an Apache Pulsar server.").
Field(service.NewURLField("url").
Description("A URL to connect to.").
Example("pulsar://localhost:6650").
Example("pulsar://pulsar.us-west.example.com:6650").
Example("pulsar+ssl://pulsar.us-west.example.com:6651")).
Field(service.NewStringField("topic").
Description("The topic to publish to.")).
Field(service.NewObjectField("tls",
service.NewStringField("root_cas_file").
Description("An optional path of a root certificate authority file to use. This is a file, often with a .pem extension, containing a certificate chain from the parent trusted root certificate, to possible intermediate signing certificates, to the host certificate.").
Default("").
Example("./root_cas.pem")).
Description("Specify the path to a custom CA certificate to trust broker TLS service.")).
Field(service.NewInterpolatedStringField("key").
Description("The key to publish messages with.").
Default("")).
Field(service.NewInterpolatedStringField("ordering_key").
Description("The ordering key to publish messages with.").
Default("")).
Field(service.NewIntField("max_in_flight").
Description("The maximum number of messages to have in flight at a given time. Increase this to improve throughput.").
Default(64)).
Field(authField())
}
//------------------------------------------------------------------------------
type pulsarWriter struct {
client pulsar.Client
producer pulsar.Producer
m sync.RWMutex
log *service.Logger
authConf authConfig
url string
topic string
rootCasFile string
key *service.InterpolatedString
orderingKey *service.InterpolatedString
}
func newPulsarWriterFromParsed(conf *service.ParsedConfig, log *service.Logger) (p *pulsarWriter, err error) {
p = &pulsarWriter{
log: log,
}
if p.authConf, err = authFromParsed(conf); err != nil {
return
}
if p.url, err = conf.FieldString("url"); err != nil {
return
}
if p.topic, err = conf.FieldString("topic"); err != nil {
return
}
if p.rootCasFile, err = conf.FieldString("tls", "root_cas_file"); err != nil {
return
}
if p.key, err = conf.FieldInterpolatedString("key"); err != nil {
return
}
if p.orderingKey, err = conf.FieldInterpolatedString("ordering_key"); err != nil {
return
}
return
}
//------------------------------------------------------------------------------
func (p *pulsarWriter) Connect(ctx context.Context) error {
p.m.Lock()
defer p.m.Unlock()
if p.client != nil {
return nil
}
var (
client pulsar.Client
producer pulsar.Producer
err error
)
opts := pulsar.ClientOptions{
Logger: createDefaultLogger(p.log),
ConnectionTimeout: time.Second * 3,
URL: p.url,
TLSTrustCertsFilePath: p.rootCasFile,
}
if p.authConf.OAuth2.Enabled {
opts.Authentication = pulsar.NewAuthenticationOAuth2(p.authConf.OAuth2.ToMap())
} else if p.authConf.Token.Enabled {
opts.Authentication = pulsar.NewAuthenticationToken(p.authConf.Token.Token)
}
if client, err = pulsar.NewClient(opts); err != nil {
return err
}
if producer, err = client.CreateProducer(pulsar.ProducerOptions{
Topic: p.topic,
}); err != nil {
client.Close()
return err
}
p.client = client
p.producer = producer
return nil
}
// disconnect safely closes a connection to an Pulsar server.
func (p *pulsarWriter) disconnect(ctx context.Context) error {
p.m.Lock()
defer p.m.Unlock()
if p.client == nil {
return nil
}
p.producer.Close()
p.client.Close()
p.producer = nil
p.client = nil
return nil
}
//------------------------------------------------------------------------------
func (p *pulsarWriter) Write(ctx context.Context, msg *service.Message) error {
var r pulsar.Producer
p.m.RLock()
if p.producer != nil {
r = p.producer
}
p.m.RUnlock()
if r == nil {
return component.ErrNotConnected
}
b, err := msg.AsBytes()
if err != nil {
return err
}
m := &pulsar.ProducerMessage{
Payload: b,
}
key, err := p.key.TryBytes(msg)
if err != nil {
return err
}
if len(key) > 0 {
m.Key = string(key)
}
orderingKey, err := p.orderingKey.TryBytes(msg)
if err != nil {
return err
}
if len(orderingKey) > 0 {
m.OrderingKey = string(orderingKey)
}
_, err = r.Send(context.Background(), m)
return err
}
func (p *pulsarWriter) Close(ctx context.Context) error {
return p.disconnect(ctx)
}