-
Notifications
You must be signed in to change notification settings - Fork 1
/
pulsar.go
303 lines (257 loc) · 6.59 KB
/
pulsar.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package gomb
import (
"context"
"sync"
"time"
"github.com/apache/pulsar-client-go/pulsar"
)
// *******************************************
// Pulsar Broker
// *******************************************
// pulsarBroker is a pulsar broker.
type pulsarBroker struct {
// options is the broker options.
// comma delimited list of pulsar brokers
// https://pulsar.apache.org/docs/2.11.x/client-libraries-go/#connection-urls
Brokers string
// operationTimeout is the timeout for operations.
operationTimeout time.Duration
// connectionTimeout is the timeout for connections.
connectionTimeout time.Duration
// options
options *BrokerOptions
// client
client pulsar.Client
// client options
clientOptions pulsar.ClientOptions
}
// newPulsarBroker creates a new pulsar broker.
func newPulsarBroker(opts BrokerOptions) (Broker, error) {
var err error
// check if broker type is set
// and if it is valid
t := opts.BrokerType
if t == "" {
return nil, ErrMissingBroker
}
// check auth is ok
if opts.Auth != nil {
// auth is not required
if err := opts.Auth.Check(); err != nil {
return nil, err
}
}
pulsarOpts := pulsar.ClientOptions{
URL: opts.Address,
}
// set auth
if opts.Auth != nil {
// TLS or OAuth2 are supported
if opts.Auth.oauth2Set() {
// https://pulsar.apache.org/docs/2.11.x/client-libraries-go/#oauth2-authentication
pulsarOpts.Authentication = pulsar.NewAuthenticationOAuth2(
opts.Auth.OAuth2.ToMap(),
)
} else if opts.Auth.tlsSet() {
// https://pulsar.apache.org/docs/2.11.x/client-libraries-go/#tls-authentication
pulsarOpts.Authentication = pulsar.NewAuthenticationTLS(
opts.Auth.TLS.CertFile,
opts.Auth.TLS.KeyFile,
)
pulsarOpts.TLSTrustCertsFilePath = opts.Auth.TLS.CertFile
} else {
// return an error as the auth is not supported
return nil, &ErrAuth{
Msg: "Unsupported auth type - pulsar supports TLS and OAuth2",
}
}
}
// create the client
client, err := pulsar.NewClient(pulsarOpts)
if err != nil {
return nil, err
}
// set defaults for timeouts
if opts.OperationTimeout == 0 {
opts.OperationTimeout = 30 * time.Second
}
if opts.ConnectionTimeout == 0 {
opts.ConnectionTimeout = 30 * time.Second
}
// create the broker
return &pulsarBroker{
Brokers: opts.Address,
operationTimeout: opts.OperationTimeout,
connectionTimeout: opts.ConnectionTimeout,
options: &opts,
client: client,
clientOptions: pulsarOpts,
}, nil
}
// Connect connects to the broker.
func (b *pulsarBroker) Connect(ctx context.Context) error {
if b.client == nil {
// set the client
client, err := pulsar.NewClient(b.clientOptions)
if err != nil {
return ErrConnection
}
b.client = client
}
return nil
}
// Close closes the broker.
func (b *pulsarBroker) Close() error {
if b.client != nil {
b.client.Close()
b.client = nil
}
return nil
}
// AckMessage acknowledges a message.
func (b *pulsarBroker) AckMessage(ctx context.Context, msg BrokerMessage) error {
// todo: implement
return nil
}
// Options returns the broker options.
func (b *pulsarBroker) Options() *BrokerOptions {
return b.options
}
// ************************************************
// * Publisher
// ************************************************
// Publish publishes a message to the broker.
func (b *pulsarBroker) Publish(ctx context.Context, msg BrokerMessage, topics ...string) error {
for _, topic := range topics {
// todo: create a producer per topic and reuse it
// create the producer
producer, err := b.client.CreateProducer(pulsar.ProducerOptions{
Topic: topic,
})
if err != nil {
return err
}
// publish the message
_, err = producer.Send(ctx, &pulsar.ProducerMessage{
Payload: msg.Body(),
Key: msg.Key(),
Properties: msg.Headers(),
EventTime: msg.Timestamp(),
})
if err != nil {
return err
}
// close the producer
producer.Close()
}
return nil
}
// ************************************************
// * Consumer
// ************************************************
// pulsarConsumer is a pulsar consumer.
type pulsarConsumer struct {
// Topics is the list of topics to consume.
topics []string
// ConsumerGroup is the consumer group to consume from.
consumerGroup string
// broker
broker *pulsarBroker
// consumer
consumer pulsar.Consumer
// paused
paused bool
mu sync.RWMutex
}
// NewConsumer creates a new consumer.
func (b *pulsarBroker) NewConsumer(opts ConsumerOptions) (Consumer, error) {
// create the consumer
return &pulsarConsumer{
topics: opts.Topics,
consumerGroup: opts.ConsumerGroup,
broker: b,
}, nil
}
// Consume consumes messages from the topics.
func (c *pulsarConsumer) Consume(ctx context.Context) (<-chan BrokerMessage, error) {
var err error
// create the channel
ch := make(chan BrokerMessage)
// create the consumer
c.consumer, err = c.broker.client.Subscribe(pulsar.ConsumerOptions{
Topics: c.topics,
SubscriptionName: c.consumerGroup,
Type: pulsar.Shared,
ReceiverQueueSize: 1000,
})
if err != nil {
return nil, err
}
// consume messages
go func() {
for {
c.loopUntilUnpaused()
select {
case <-ctx.Done():
return
default:
// consume the message
m, err := c.consumer.Receive(ctx)
if err != nil {
return
}
// create the broker message
msg := &Message{}
msg.SetBody(m.Payload())
msg.SetKey(m.Key())
msg.SetHeaders(m.Properties())
msg.SetTimestamp(m.EventTime())
msg.SetID(string(m.ID().Serialize()))
msg.SetGroup(c.consumerGroup)
msg.SetTopic(m.Topic())
msg.SetBroker(c.broker)
// send the message
ch <- msg
}
}
}()
return ch, nil
}
// Pause pauses the consumer.
func (c *pulsarConsumer) Pause() {
c.mu.Lock()
defer c.mu.Unlock()
c.paused = true
}
// Resume resumes the consumer.
func (c *pulsarConsumer) Resume() {
c.mu.Lock()
defer c.mu.Unlock()
c.paused = false
}
// Topics returns the list of topics.
func (c *pulsarConsumer) Topics() []string {
return c.topics
}
// Close closes the consumer.
func (c *pulsarConsumer) Close() error {
c.consumer.Close()
return nil
}
// Connect connects to the broker.
func (c *pulsarConsumer) Connect(ctx context.Context) error {
return c.broker.Connect(ctx)
}
// ConsumerGroup returns the consumer group.
func (c *pulsarConsumer) ConsumerGroup() string {
return c.consumerGroup
}
// loopUntilUnpaused is a helper function that will loop until the consumer is unpaused.
func (c *pulsarConsumer) loopUntilUnpaused() {
c.mu.RLock()
defer c.mu.RUnlock()
for c.paused {
// sleep for a bit?
time.Sleep(time.Millisecond * 100)
}
}