forked from cornelk/pulsar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
476 lines (402 loc) · 12.1 KB
/
consumer.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package pulsar
import (
"context"
"errors"
"fmt"
"io"
"math"
"regexp"
"sync"
pb "github.com/cornelk/pulsar-go/proto"
"github.com/golang/protobuf/proto"
)
// SubscriptionType ...
type SubscriptionType pb.CommandSubscribe_SubType
// Subscription type options.
const (
ExclusiveSubscription = SubscriptionType(pb.CommandSubscribe_Exclusive)
SharedSubscription = SubscriptionType(pb.CommandSubscribe_Shared)
FailoverSubscription = SubscriptionType(pb.CommandSubscribe_Failover)
KeySharedSubscription = SubscriptionType(pb.CommandSubscribe_Key_Shared)
)
// InitialPosition ...
type InitialPosition pb.CommandSubscribe_InitialPosition
// Subscription initial position options.
const (
// Start reading from the end topic, only getting messages published after the
// reader was created.
LatestPosition = InitialPosition(pb.CommandSubscribe_Latest)
// Start reading from the earliest message available in the topic.
EarliestPosition = InitialPosition(pb.CommandSubscribe_Earliest)
)
// ConsumerConfig is a configuration object used to create new instances
// of Consumer.
type ConsumerConfig struct {
// The topic name to read messages from.
Topic string
// A regular expression for topics to read messages from.
TopicPattern string
// Interval in ms in which the client checks for topic changes
// that match the set topic pattern and updates the subscriptions.
// Default is 30000
TopicPatternDiscoveryInterval int
// A unique name for the subscription. If not specified, a random name
// will be used.
Subscription string
// A unique name for the Consumer. If not specified, a random name
// will be used.
Name string
// Select the subscription type to be used when subscribing to the topic.
// Default is `Subscribe_Exclusive`
Type SubscriptionType
// Signal whether the subscription will initialize on latest
// or earliest position.
InitialPosition InitialPosition
// If specified, the subscription will position the cursor
// on the particular message id and will send messages from
// that point.
StartMessageID []byte
// Include the message StartMessageID in the read messages.
// If StartMessageID is not set but InitialPosition is set
// to LatestPosition, the latest message ID of the topic
// will be sent.
StartMessageIDInclusive bool
// Signal whether the subscription should be backed by a
// durable cursor or not. For Readers, set to false, for
// Consumers set Durable to true and specify a Subscription.
// If Durable is true, StartMessageID will be ignored, as it
// will be determined by the broker.
Durable bool
// If true, the subscribe operation will cause a topic to be
// created if it does not exist already (and if topic auto-creation
// is allowed by broker.
// If false, the subscribe operation will fail if the topic
// does not exist.
ForceTopicCreation bool
// MessageChannel sets a channel that receives all messages that the
// consumer receives. If not set, a default channel for 1000 messages
// will be created.
MessageChannel chan *Message
}
// Consumer provides a high-level API for consuming messages from Pulsar.
type Consumer interface {
// Close closes the subscription and unregisters from the Client.
Close() error
AckMessage(*Message) error
// ReadMessage reads and return the next message from the Pulsar.
ReadMessage(context.Context) (*Message, error)
SeekMessage(*Message) error
// HasNext returns whether there is a message available to read
HasNext() bool
// LastMessageID returns the last message ID of the topic.
// If the topic is empty, EntryId will be math.MaxUint64
LastMessageID() (*MessageID, error)
}
type consumerCloser interface {
CloseConsumer(consumerID uint64) error
}
type consumerState int
const (
consumerInit consumerState = iota
consumerReady
consumerSubscribed
consumerClosing
consumerClosed
)
type consumer struct {
ctx context.Context
conn clientConn
req *requests
// subscribe options
topic string
subscription *string
name *string
subType pb.CommandSubscribe_SubType
initialPosition pb.CommandSubscribe_InitialPosition
startMessageID *pb.MessageIdData
startMessageIDInclusive bool
startMessageIDSeekDone bool
forceTopicCreation *bool
durable *bool
incomingMessages chan *Message
consumerID uint64
connected chan error
state consumerState
stateMu sync.RWMutex
closer consumerCloser
multi *multiTopicConsumer // set if managed by a multi topic consumer
messagePermits uint32
usedPermits uint32
permitsMu sync.Mutex
}
// Validate method validates the config properties.
func (config *ConsumerConfig) Validate() error {
if config.Topic == "" && config.TopicPattern == "" {
return errors.New("topic is not set")
}
if config.Topic != "" && config.TopicPattern != "" {
return errors.New("topic and topic pattern are exclusive")
}
if config.StartMessageID != nil {
id := &pb.MessageIdData{}
if err := proto.Unmarshal(config.StartMessageID, id); err != nil {
return fmt.Errorf("start message id unmarshalling: %w", err)
}
}
if config.TopicPattern != "" {
if _, err := regexp.Compile(config.TopicPattern); err != nil {
return fmt.Errorf("topic pattern regular expression compiling: %w", err)
}
if config.TopicPatternDiscoveryInterval < 0 {
return errors.New("invalid topic pattern discovery interval set")
}
}
return nil
}
// newConsumer creates and returns a new Consumer configured with config.
func newConsumer(closer consumerCloser, conn brokerConnection, config ConsumerConfig, consumerID uint64) (*consumer, error) {
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("validating config: %w", err)
}
c := &consumer{
ctx: conn.ctx,
conn: conn.conn,
req: conn.req,
topic: config.Topic,
subType: pb.CommandSubscribe_SubType(config.Type),
initialPosition: pb.CommandSubscribe_InitialPosition(config.InitialPosition),
startMessageIDInclusive: config.StartMessageIDInclusive,
forceTopicCreation: proto.Bool(config.ForceTopicCreation),
durable: proto.Bool(config.Durable),
consumerID: consumerID,
connected: make(chan error, 1),
state: consumerInit,
closer: closer,
}
if config.MessageChannel == nil {
c.incomingMessages = make(chan *Message, 1000)
} else {
c.incomingMessages = config.MessageChannel
}
if cap(c.incomingMessages) > math.MaxUint32 {
c.messagePermits = math.MaxUint32
} else {
c.messagePermits = uint32(cap(c.incomingMessages))
}
if !config.Durable {
if config.StartMessageID != nil {
id := &pb.MessageIdData{}
if err := proto.Unmarshal(config.StartMessageID, id); err != nil {
return nil, err
}
c.startMessageID = id
} else {
if config.InitialPosition == EarliestPosition {
c.startMessageID = earliestMessageID
} else {
c.startMessageID = latestMessageID
}
}
}
if config.Name == "" {
c.name = proto.String(randomConsumerName())
} else {
c.name = proto.String(config.Name)
}
if config.Subscription == "" {
c.subscription = proto.String(randomSubscriptionName())
} else {
c.subscription = proto.String(config.Subscription)
}
return c, nil
}
func (c *consumer) topicLookupFinished(cmd *command) error {
if cmd.err != nil {
return cmd.err
}
id := c.req.newID()
if c.startMessageIDInclusive && c.startMessageID == latestMessageID && !c.startMessageIDSeekDone {
c.req.addCallback(id, c.getLastMessageID)
} else {
c.req.addCallback(id, c.subscribedSendFlowCommand)
}
c.stateMu.Lock()
c.state = consumerReady
c.stateMu.Unlock()
return c.sendSubscribeCommand(cmd.custom, id)
}
func (c *consumer) getLastMessageID(base *command) error {
if base.err != nil {
c.connected <- base.err
return base.err
}
reqID := c.req.newID()
cmd := newGetLastMessageIDCommand(c.consumerID, reqID)
c.req.addCallback(reqID, c.seekToLastMessageID)
return c.conn.WriteCommand(cmd, nil)
}
func (c *consumer) seekToLastMessageID(cmd *command) error {
c.startMessageIDSeekDone = true
if cmd.err != nil {
return cmd.err
}
if *cmd.GetLastMessageIdResponse.LastMessageId.EntryId == math.MaxUint64 {
return c.subscribedSendFlowCommand(cmd) // empty topic
}
reqID := c.req.newID()
return c.sendSeek(reqID, cmd.GetLastMessageIdResponse.LastMessageId)
}
func (c *consumer) Close() error {
return c.closer.CloseConsumer(c.consumerID)
}
func (c *consumer) reset(newConsumerID uint64) {
c.consumerID = newConsumerID
}
func (c *consumer) ReadMessage(ctx context.Context) (*Message, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-c.ctx.Done():
return nil, c.ctx.Err()
case m, ok := <-c.incomingMessages:
if !ok {
return nil, io.EOF
}
err := c.useMessagePermit()
return m, err
}
}
func (c *consumer) useMessagePermit() error {
c.permitsMu.Lock()
c.usedPermits++
hasPermits := c.usedPermits < c.messagePermits
c.permitsMu.Unlock()
if hasPermits {
return nil
}
return c.sendFlowCommand()
}
func (c *consumer) HasNext() bool {
return len(c.incomingMessages) > 0
}
func (c *consumer) SeekMessage(msg *Message) error {
id := &pb.MessageIdData{
LedgerId: msg.ID.LedgerId,
EntryId: msg.ID.EntryId,
}
reqID := c.req.newID()
ch := make(chan error, 1)
c.req.addCallback(reqID, func(cmd *command) error {
ch <- cmd.err
return cmd.err
})
c.startMessageID = id
if err := c.sendSeek(reqID, id); err != nil {
return err
}
err := <-ch
// the server issues a close consumer command, wait for the
// automatic reconnection
e := <-c.connected
if err == nil {
err = e
}
return err
}
func (c *consumer) sendSeek(reqID uint64, id *pb.MessageIdData) error {
base := &pb.BaseCommand{
Type: pb.BaseCommand_SEEK.Enum(),
Seek: &pb.CommandSeek{
ConsumerId: proto.Uint64(c.consumerID),
RequestId: proto.Uint64(reqID),
MessageId: id,
},
}
return c.conn.WriteCommand(base, nil)
}
func (c *consumer) AckMessage(msg *Message) error {
return c.sendAck(msg.ID)
}
func (c *consumer) sendAck(id *MessageID) error {
base := &pb.BaseCommand{
Type: pb.BaseCommand_ACK.Enum(),
Ack: &pb.CommandAck{
ConsumerId: proto.Uint64(c.consumerID),
AckType: pb.CommandAck_Individual.Enum(),
MessageId: []*pb.MessageIdData{(*pb.MessageIdData)(id)},
},
}
return c.conn.WriteCommand(base, nil)
}
func (c *consumer) sendSubscribeCommand(topic string, reqID uint64) error {
base := &pb.BaseCommand{
Type: pb.BaseCommand_SUBSCRIBE.Enum(),
Subscribe: &pb.CommandSubscribe{
Topic: &topic,
Subscription: c.subscription,
SubType: &c.subType,
ConsumerId: proto.Uint64(c.consumerID),
RequestId: proto.Uint64(reqID),
ConsumerName: c.name,
Durable: c.durable,
StartMessageId: c.startMessageID,
ReadCompacted: proto.Bool(false),
InitialPosition: &c.initialPosition,
ForceTopicCreation: c.forceTopicCreation,
},
}
return c.conn.WriteCommand(base, nil)
}
func (c *consumer) subscribedSendFlowCommand(cmd *command) error {
defer func() {
c.connected <- cmd.err
}()
if cmd.err != nil {
return cmd.err
}
c.stateMu.Lock()
c.state = consumerSubscribed
c.stateMu.Unlock()
return c.sendFlowCommand()
}
func (c *consumer) sendFlowCommand() error {
c.permitsMu.Lock()
c.usedPermits = 0
c.permitsMu.Unlock()
base := &pb.BaseCommand{
Type: pb.BaseCommand_FLOW.Enum(),
Flow: &pb.CommandFlow{
ConsumerId: proto.Uint64(c.consumerID),
MessagePermits: proto.Uint32(c.messagePermits),
},
}
return c.conn.WriteCommand(base, nil)
}
func (c *consumer) clearReceiverQueue() {
for {
select {
case _, ok := <-c.incomingMessages:
if !ok {
return
}
default:
return
}
}
}
func (c *consumer) LastMessageID() (*MessageID, error) {
reqID := c.req.newID()
cmd := newGetLastMessageIDCommand(c.consumerID, reqID)
var msgID *MessageID
respHandler := func(resp *command) error {
if resp.err == nil && resp.GetLastMessageIdResponse != nil {
msgID = (*MessageID)(resp.GetLastMessageIdResponse.LastMessageId)
}
return resp.err
}
err := c.conn.SendCallbackCommand(c.req, reqID, cmd, respHandler)
if err != nil {
return nil, err
}
return msgID, nil
}