-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
257 lines (223 loc) · 6.26 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
package alirmq
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
"github.com/sirupsen/logrus"
"github.com/xiaojiaoyu100/rocketmq-client-go"
"github.com/xiaojiaoyu100/rocketmq-client-go/consumer"
"github.com/xiaojiaoyu100/rocketmq-client-go/primitive"
"github.com/xiaojiaoyu100/aliyun-rocket-mq/log"
)
type Handler func(*M) error
type ErrorCallback func(err error, msg *primitive.MessageExt)
type Consumer struct {
consumer rocketmq.PushConsumer
option *ConsumerOption
handleFuncMap map[string]Handler
signal chan os.Signal
callback ErrorCallback
subscribeTopics []string
topicTagsMap map[string][]string
mutex sync.Mutex
onsClient *OnsClient
}
func NewConsumer(cred *RocketMQCredentials, setter ...ConsumerSetter) (*Consumer, error) {
os.Setenv("ROCKETMQ_GO_LOG_LEVEL", "warn")
opts := &ConsumerOption{
cred: cred,
Broadcasting: false,
ConsumeOrderly: false,
}
for _, apply := range setter {
apply(opts)
}
if opts.MaxReconsumeTimes == 0 {
opts.MaxReconsumeTimes = 16
}
if opts.MaxTopicCount == 0 {
opts.MaxTopicCount = 16
}
if opts.InstanceName == "" {
opts.InstanceName = strconv.Itoa(os.Getpid())
}
consumerModel := consumer.Clustering
if opts.Broadcasting {
consumerModel = consumer.BroadCasting
}
consumeOrder := opts.ConsumeOrderly
if !strings.HasPrefix(opts.cred.GroupName, "GID_") {
opts.cred.GroupName = "GID_" + opts.cred.GroupName
}
nameServers := strings.Split(opts.cred.NameServer, ",")
c, err := rocketmq.NewPushConsumer(
consumer.WithNameServer(nameServers),
consumer.WithNamespace(opts.cred.NameSpace),
consumer.WithGroupName(opts.cred.GroupName),
consumer.WithInstance(opts.InstanceName),
consumer.WithCredentials(primitive.Credentials{
AccessKey: opts.cred.AccessKey,
SecretKey: opts.cred.SecretKey,
}),
consumer.WithMaxTopicCount(opts.MaxTopicCount+1),
consumer.WithMaxReconsumeTimes(opts.MaxReconsumeTimes),
consumer.WithConsumerModel(consumerModel),
consumer.WithConsumerOrder(consumeOrder),
)
if err != nil {
return nil, fmt.Errorf("new rocketMQ consumer error = %v", err)
}
var onsClient *OnsClient
if opts.cred.Region != "" {
onsClient, err = NewOnsClient(
opts.cred.Region, opts.cred.AccessKey, opts.cred.SecretKey, opts.cred.NameSpace)
if err != nil {
return nil, fmt.Errorf("new onsClient error = %w", err)
}
}
inst := &Consumer{
consumer: c,
option: opts,
handleFuncMap: make(map[string]Handler, 0),
signal: make(chan os.Signal, 1),
subscribeTopics: make([]string, 0, opts.MaxTopicCount),
topicTagsMap: make(map[string][]string, 0),
onsClient: onsClient,
}
return inst, nil
}
func (c *Consumer) autoCreateConsumerGroup() bool {
return c.onsClient != nil
}
func (c *Consumer) createConsumerGroupNotExists(group string) error {
groups, err := c.onsClient.ListGroup()
if err != nil {
return fmt.Errorf("list consumer group error = %w", err)
}
groupMap := make(map[string]struct{})
for _, g := range groups {
groupMap[g.GroupName] = struct{}{}
}
// 消费者组不存在,自动创建
if _, ok := groupMap[group]; !ok {
err = c.onsClient.CreateGroup(group, group)
if err != nil {
err = fmt.Errorf("create consumer group error = %w", err)
}
}
return err
}
func (c *Consumer) SetErrorCallback(fn func(err error, msg *primitive.MessageExt)) {
c.callback = fn
}
func (c *Consumer) uniqueTags(tags []string) []string {
m := make(map[string]struct{})
for _, t := range tags {
if t == _TagAll {
continue
}
m[t] = struct{}{}
}
res := make([]string, 0, len(m))
for k := range m {
res = append(res, k)
}
return res
}
func (c *Consumer) Subscribe(topic, tag string, handler Handler) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if len(c.subscribeTopics) >= c.option.MaxTopicCount {
return fmt.Errorf("consumer subscribe topic large than config")
}
if tag == "" {
tag = _TagAll
}
tags := strings.Split(tag, "||")
for _, t := range tags {
c.handleFuncMap[c.generateKey(topic, t)] = handler
}
// 支持在同一个topic上重复订阅
if subscribeTags, ok := c.topicTagsMap[topic]; ok {
subscribeTags = append(subscribeTags, tags...)
c.topicTagsMap[topic] = c.uniqueTags(subscribeTags)
} else {
c.topicTagsMap[topic] = c.uniqueTags(tags)
}
subscribeTags := c.topicTagsMap[topic]
if len(subscribeTags) == 0 {
tag = _TagAll
} else {
tag = strings.Join(subscribeTags, "||")
}
err := c.consumer.Subscribe(
topic,
consumer.MessageSelector{Type: consumer.TAG, Expression: tag},
c.processMessage)
if err != nil {
return fmt.Errorf("subscribe topic error = %v", err)
}
return nil
}
func (c *Consumer) generateKey(topic, tag string) string {
return fmt.Sprintf("%s#%s", topic, tag)
}
func (c *Consumer) processMessage(ctx context.Context, messages ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
for _, msg := range messages {
topic, tag := msg.Topic, msg.GetTags()
if strings.HasPrefix(topic, _RetryPrefix) {
topic = msg.GetProperty(_RetryOriginTopic)
if topic == "" {
log.Inst().Errorf("get message - %v without topic - %v", msg, topic)
continue
}
}
if strings.HasPrefix(topic, c.option.cred.NameSpace) {
topic = strings.ReplaceAll(topic, c.option.cred.NameSpace+"%", "")
}
key := c.generateKey(topic, tag)
m := &M{
Topic: topic,
Tag: tag,
MsgId: msg.MsgId,
Key: msg.GetKeys(),
Body: msg.Body,
}
if fn, ok := c.handleFuncMap[key]; ok {
err := fn(m)
if err != nil {
return consumer.ConsumeRetryLater, nil
}
}
if fn, ok := c.handleFuncMap[c.generateKey(topic, "*")]; ok {
err := fn(m)
if err != nil {
return consumer.ConsumeRetryLater, nil
}
}
}
return consumer.ConsumeSuccess, nil
}
func (c *Consumer) Start() error {
if c.autoCreateConsumerGroup() {
err := c.createConsumerGroupNotExists(c.option.cred.GroupName)
if err != nil {
return err
}
}
return c.consumer.Start()
}
func (c *Consumer) Stop() error {
return c.consumer.Shutdown()
}
func DefaultErrorCallback(err error, msg *primitive.MessageExt) {
log.Inst().WithFields(logrus.Fields{
"topic": msg.Topic,
"tag": msg.GetTags(),
"messageid": msg.MsgId,
"reconsumetimes": msg.ReconsumeTimes,
}).WithError(err).Warnf("handle rocket mq msg error")
}