forked from cornelk/pulsar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_multitopic.go
96 lines (76 loc) · 2.05 KB
/
consumer_multitopic.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
package pulsar
import (
"context"
"errors"
"io"
"regexp"
)
type multiTopicConsumer struct {
ctx context.Context
topicPattern *regexp.Regexp
incomingMessages chan *Message
consumers *consumerRegistry
closer consumerCloser
}
func newMultiTopicConsumer(closer consumerCloser, conn brokerConnection, config ConsumerConfig) (*multiTopicConsumer, error) {
c := &multiTopicConsumer{
ctx: conn.ctx,
consumers: newConsumerRegistry(),
closer: closer,
}
if config.MessageChannel == nil {
c.incomingMessages = make(chan *Message, 1000)
} else {
c.incomingMessages = config.MessageChannel
}
var err error
c.topicPattern, err = regexp.Compile(config.TopicPattern)
if err != nil {
return nil, err
}
return c, nil
}
func (c *multiTopicConsumer) addConsumer(id uint64, consumer *consumer) {
c.consumers.add(id, consumer)
}
func (c *multiTopicConsumer) changeConsumerID(consumer *consumer, oldID, newID uint64) {
c.consumers.add(newID, consumer)
c.consumers.delete(oldID)
}
func (c *multiTopicConsumer) Close() error {
c.consumers.mu.RLock()
defer c.consumers.mu.RLock()
for _, con := range c.consumers.consumers {
_ = c.closer.CloseConsumer(con.consumerID)
}
return nil
}
func (c *multiTopicConsumer) 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
}
return m, nil
}
}
func (c *multiTopicConsumer) SeekMessage(_ *Message) error {
return errors.New("seek not supported in multi topic consumer")
}
func (c *multiTopicConsumer) AckMessage(msg *Message) error {
consumer, ok := c.consumers.get(msg.consumerID)
if !ok {
return errors.New("consumer of message not found")
}
return consumer.AckMessage(msg)
}
func (c *multiTopicConsumer) HasNext() bool {
return len(c.incomingMessages) > 0
}
func (c *multiTopicConsumer) LastMessageID() (*MessageID, error) {
return nil, errors.New("last message id not supported in multi topic consumer")
}