-
Notifications
You must be signed in to change notification settings - Fork 0
/
pulsar.go
218 lines (193 loc) · 6.1 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
package pulsar
import (
"errors"
"runtime"
"runtime/debug"
"sync"
"time"
"github.com/skirrund/gcloud/logger"
"github.com/skirrund/gcloud/mq"
"github.com/skirrund/gcloud/utils"
baseConsumer "github.com/skirrund/gcloud/mq/consumer"
"github.com/apache/pulsar-client-go/pulsar"
)
type Message struct {
Msg pulsar.Message
}
type PulsarClient struct {
client pulsar.Client
appName string
mt sync.Mutex
}
const (
SERVER_URL_KEY = "pulsar.service-url"
CONNECTION_TIMEOUT_KEY = "pulsar.connectionTimeout"
OPERATION_TIMEOUT_KEY = "pulsar.operationTimeout"
defaultConnectionTimeout = 5 * time.Second
defaultOperationTimeout = 30 * time.Second
)
var consumers sync.Map
const (
MAX_RETRY_TIMES = 50
)
var pulsarClient *PulsarClient
var once sync.Once
func NewClient(url string, connectionTimeoutSecond int64, operationTimeoutSecond int64, appName string) mq.IClient {
if pulsarClient != nil {
return pulsarClient
}
once.Do(func() {
pulsarClient = &PulsarClient{
client: createClient(url, connectionTimeoutSecond, operationTimeoutSecond),
appName: appName,
}
})
return pulsarClient
}
func createClient(url string, connectionTimeoutSecond int64, operationTimeoutSecond int64) pulsar.Client {
var cts time.Duration
var ots time.Duration
if connectionTimeoutSecond > 0 {
cts = time.Duration(connectionTimeoutSecond) * time.Second
} else {
cts = defaultConnectionTimeout
}
if operationTimeoutSecond > 0 {
ots = time.Duration(operationTimeoutSecond) * time.Second
} else {
ots = defaultOperationTimeout
}
logger.Infof("[pulsar]start init pulsar-client:" + url)
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: url,
ConnectionTimeout: cts,
OperationTimeout: ots,
MaxConnectionsPerBroker: runtime.NumCPU(),
})
if err != nil {
panic(err)
}
logger.Infof("[pulsar]finished init pulsar-client")
return client
}
func getAppName(appName string) string {
name := utils.Uuid()
if len(appName) == 0 {
return name
} else {
return appName + "-" + name
}
}
func (pc *PulsarClient) Send(topic string, msg string) error {
return pc.doSend(topic, msg, 0)
}
func (pc *PulsarClient) SendDelay(topic string, msg string, delay time.Duration) error {
return pc.doSend(topic, msg, delay)
}
func (pc *PulsarClient) SendDelayAt(topic string, msg string, delayAt time.Time) error {
return pc.doSendDelayAt(topic, msg, delayAt)
}
func (pc *PulsarClient) SendAsync(topic string, msg string) error {
return pc.doSendAsync(topic, msg, 0)
}
func (pc *PulsarClient) SendDelayAsync(topic string, msg string, delay time.Duration) error {
return pc.doSendAsync(topic, msg, delay)
}
func (pc *PulsarClient) SendDelayAtAsync(topic string, msg string, delayAt time.Time) error {
return pc.doSendDelayAtAsync(topic, msg, delayAt)
}
func (pc *PulsarClient) Subscribe(opts mq.ConsumerOptions) {
go pc.doSubscribe(opts)
}
func (pc *PulsarClient) Subscribes(opts ...mq.ConsumerOptions) {
for _, opt := range opts {
pc.Subscribe(opt)
}
}
func (pc *PulsarClient) doSubscribe(opts mq.ConsumerOptions) error {
subscriptionName := opts.SubscriptionName
topic := opts.Topic
logger.Infof("[pulsar]ConsumerOptions:%v", opts)
options := pulsar.ConsumerOptions{
Topic: topic,
SubscriptionName: subscriptionName,
Type: pulsar.SubscriptionType(opts.SubscriptionType),
Name: getAppName(pc.appName),
NackRedeliveryDelay: 15 * time.Second,
//Schema: pulsar.NewStringSchema(nil),
}
if opts.RetryTimes == 0 {
opts.RetryTimes = MAX_RETRY_TIMES
}
schema := pulsar.NewJSONSchema(`"string"`, nil)
channelSize := opts.MaxMessageChannelSize
if channelSize == 0 {
channelSize = 200
}
channel := make(chan pulsar.ConsumerMessage, channelSize)
options.MessageChannel = channel
consumer, err := pc.client.Subscribe(options)
if err != nil {
logger.Error(errors.New("[pulsar] Subscribe error:" + err.Error()))
panic("[pulsar] Subscribe error:" + err.Error())
//return err
}
consumers.Store(topic+":"+subscriptionName, opts)
logger.Infof("[pulsar]store consumerOptions:"+topic+":"+subscriptionName, ",", opts)
for cm := range channel {
go consume(cm, consumer, schema, opts)
}
return nil
}
func consume(cm pulsar.ConsumerMessage, consumer pulsar.Consumer, schema pulsar.Schema, opts mq.ConsumerOptions) {
defer func() {
if err := recover(); err != nil {
logger.Error("[pulsar] consumer panic recover :", err, "\n", string(debug.Stack()))
}
}()
msg := cm.Message
var msgStr string
logger.Infof("[pulsar] consumer info=>subName:%s,msgId:%v,reDeliveryCount:%d,publishTime:%v,producerName:%s", cm.Subscription(), msg.ID(), msg.RedeliveryCount(), msg.PublishTime(), msg.ProducerName())
err := schema.Decode(msg.Payload(), &msgStr)
if err != nil {
logger.Info("[pulsar] consumer msg:", err.Error())
} else {
logger.Info("[pulsar] consumer msg:", msgStr)
}
err = opts.MessageListener.OnMessage(baseConsumer.Message{
Value: msgStr,
RedeliveryCount: msg.RedeliveryCount(),
})
if err == nil {
consumer.Ack(msg)
} else {
logger.Error("[pulsar] consumer error:" + err.Error())
copts, ok := consumers.Load(opts.Topic + ":" + cm.Subscription())
retryTimes := uint32(0)
ACKMode := uint32(0)
if ok {
if opt, o := copts.(mq.ConsumerOptions); o {
retryTimes = opt.RetryTimes
if retryTimes > MAX_RETRY_TIMES {
retryTimes = MAX_RETRY_TIMES
}
ACKMode = uint32(opt.ACKMode)
} else {
logger.Errorf("[pulsar] consumerOptions type error=> options:%v", copts)
}
} else {
logger.Error("[pulsar] can not find ConsumerOptions=>subName:" + opts.Topic + ":" + cm.Subscription())
}
rt := msg.RedeliveryCount()
if ACKMode == 1 && rt < retryTimes {
logger.Infof("[pulsar]consummer error and retry=> subscriptionName:"+cm.Subscription()+",initRetryTimes:%d,retryTimes:%d,ack:%d", retryTimes, rt, ACKMode)
consumer.Nack(msg)
} else {
logger.Infof("[pulsar]consummer error and can not retry=> subscriptionName:"+cm.Subscription()+",initRetryTimes:%d,retryTimes:%d,ack:%d", retryTimes, rt, ACKMode)
consumer.Ack(msg)
}
}
}
func (pc *PulsarClient) Close() {
pc.client.Close()
}