forked from apache/rocketmq-client-go
/
option.go
389 lines (322 loc) · 11.1 KB
/
option.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package consumer
import (
"strings"
"time"
"go.uber.org/atomic"
"github.com/apache/rocketmq-client-go/v2/hooks"
"github.com/apache/rocketmq-client-go/v2/internal"
"github.com/apache/rocketmq-client-go/v2/primitive"
)
type consumerOptions struct {
internal.ClientOptions
/**
* Backtracking consumption time with second precision. Time format is
* 20131223171201<br>
* Implying Seventeen twelve and 01 seconds on December 23, 2013 year<br>
* Default backtracking consumption time Half an hour ago.
*/
ConsumeTimestamp string
// The socket timeout in milliseconds
ConsumerPullTimeout time.Duration
// Concurrently max span offset.it has no effect on sequential consumption
ConsumeConcurrentlyMaxSpan int
// Flow control threshold on queue level, each message queue will cache at most 1000 messages by default,
// Consider the {PullBatchSize}, the instantaneous value may exceed the limit
PullThresholdForQueue int64
// Limit the cached message size on queue level, each message queue will cache at most 100 MiB messages by default,
// Consider the {@code pullBatchSize}, the instantaneous value may exceed the limit
//
// The size of a message only measured by message body, so it's not accurate
PullThresholdSizeForQueue int
// Flow control threshold on topic level, default value is -1(Unlimited)
//
// The value of {@code pullThresholdForQueue} will be overwritten and calculated based on
// {@code pullThresholdForTopic} if it isn't unlimited
//
// For example, if the value of pullThresholdForTopic is 1000 and 10 message queues are assigned to this consumer,
// then pullThresholdForQueue will be set to 100
PullThresholdForTopic int
// Limit the cached message size on topic level, default value is -1 MiB(Unlimited)
//
// The value of {@code pullThresholdSizeForQueue} will be overwritten and calculated based on
// {@code pullThresholdSizeForTopic} if it isn't unlimited
//
// For example, if the value of pullThresholdSizeForTopic is 1000 MiB and 10 message queues are
// assigned to this consumer, then pullThresholdSizeForQueue will be set to 100 MiB
PullThresholdSizeForTopic int
// Message pull Interval
PullInterval atomic.Duration
// Message consumer tps
ConsumeTPS atomic.Int32
// Batch consumption size
ConsumeMessageBatchMaxSize int
// Batch pull size
PullBatchSize atomic.Int32
// Whether update subscription relationship when every pull
PostSubscriptionWhenPull bool
// Max re-consume times. -1 means 16 times.
//
// If messages are re-consumed more than {@link #maxReconsumeTimes} before Success, it's be directed to a deletion
// queue waiting.
MaxReconsumeTimes int32
// Suspending pulling time for cases requiring slow pulling like flow-control scenario.
SuspendCurrentQueueTimeMillis time.Duration
// Maximum amount of time a message may block the consuming thread.
ConsumeTimeout time.Duration
ConsumerModel MessageModel
Strategy AllocateStrategy
ConsumeOrderly bool
FromWhere ConsumeFromWhere
Interceptors []primitive.Interceptor
// TODO traceDispatcher
MaxTimeConsumeContinuously time.Duration
//
AutoCommit bool
RebalanceLockInterval time.Duration
Resolver primitive.NsResolver
ConsumeGoroutineNums int
filterMessageHooks []hooks.FilterMessageHook
Limiter Limiter
TraceDispatcher internal.TraceDispatcher
}
func defaultPushConsumerOptions() consumerOptions {
opts := consumerOptions{
ClientOptions: internal.DefaultClientOptions(),
Strategy: AllocateByAveragely,
MaxTimeConsumeContinuously: time.Duration(60 * time.Second),
RebalanceLockInterval: 20 * time.Second,
MaxReconsumeTimes: -1,
ConsumerModel: Clustering,
AutoCommit: true,
Resolver: primitive.NewHttpResolver("DEFAULT"),
ConsumeTimestamp: time.Now().Add(time.Minute * (-30)).Format("20060102150405"),
ConsumeTimeout: 15 * time.Minute,
ConsumeGoroutineNums: 20,
}
opts.ClientOptions.GroupName = "DEFAULT_CONSUMER"
return opts
}
type Option func(*consumerOptions)
func defaultPullConsumerOptions() consumerOptions {
opts := consumerOptions{
ClientOptions: internal.DefaultClientOptions(),
Resolver: primitive.NewHttpResolver("DEFAULT"),
ConsumerModel: Clustering,
Strategy: AllocateByAveragely,
}
opts.ClientOptions.GroupName = "DEFAULT_CONSUMER"
return opts
}
func WithConsumerModel(m MessageModel) Option {
return func(options *consumerOptions) {
options.ConsumerModel = m
}
}
func WithConsumeFromWhere(w ConsumeFromWhere) Option {
return func(options *consumerOptions) {
options.FromWhere = w
}
}
func WithConsumerOrder(order bool) Option {
return func(options *consumerOptions) {
options.ConsumeOrderly = order
}
}
func WithConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize int) Option {
return func(options *consumerOptions) {
options.ConsumeMessageBatchMaxSize = consumeMessageBatchMaxSize
}
}
func WithConsumeTimestamp(consumeTimestamp string) Option {
return func(options *consumerOptions) {
options.ConsumeTimestamp = consumeTimestamp
}
}
func WithConsumerPullTimeout(consumerPullTimeout time.Duration) Option {
return func(options *consumerOptions) {
options.ConsumerPullTimeout = consumerPullTimeout
}
}
func WithConsumeConcurrentlyMaxSpan(consumeConcurrentlyMaxSpan int) Option {
return func(options *consumerOptions) {
options.ConsumeConcurrentlyMaxSpan = consumeConcurrentlyMaxSpan
}
}
func WithPullThresholdForQueue(pullThresholdForQueue int64) Option {
return func(options *consumerOptions) {
options.PullThresholdForQueue = pullThresholdForQueue
}
}
func WithPullThresholdSizeForQueue(pullThresholdSizeForQueue int) Option {
return func(options *consumerOptions) {
options.PullThresholdSizeForQueue = pullThresholdSizeForQueue
}
}
func WithPullThresholdForTopic(pullThresholdForTopic int) Option {
return func(options *consumerOptions) {
options.PullThresholdForTopic = pullThresholdForTopic
}
}
func WithPullThresholdSizeForTopic(pullThresholdSizeForTopic int) Option {
return func(options *consumerOptions) {
options.PullThresholdSizeForTopic = pullThresholdSizeForTopic
}
}
// WithChainConsumerInterceptor returns a ConsumerOption that specifies the chained interceptor for consumer.
// The first interceptor will be the outer most, while the last interceptor will be the inner most wrapper
// around the real call.
func WithInterceptor(fs ...primitive.Interceptor) Option {
return func(options *consumerOptions) {
options.Interceptors = append(options.Interceptors, fs...)
}
}
// WithGroupName set group name address
func WithGroupName(group string) Option {
return func(opts *consumerOptions) {
if group == "" {
return
}
opts.GroupName = group
}
}
func WithInstance(name string) Option {
return func(options *consumerOptions) {
options.InstanceName = name
}
}
// WithNamespace set the namespace of consumer
func WithNamespace(namespace string) Option {
return func(opts *consumerOptions) {
opts.Namespace = namespace
}
}
func WithVIPChannel(enable bool) Option {
return func(opts *consumerOptions) {
opts.VIPChannelEnabled = enable
}
}
// WithRetry return a Option that specifies the retry times when send failed.
// TODO: use retry middleware instead
func WithRetry(retries int) Option {
return func(opts *consumerOptions) {
opts.RetryTimes = retries
}
}
func WithCredentials(c primitive.Credentials) Option {
return func(options *consumerOptions) {
options.ClientOptions.Credentials = c
}
}
// WithMaxReconsumeTimes set MaxReconsumeTimes of options, if message reconsume greater than MaxReconsumeTimes, it will
// be sent to retry or dlq topic. more info reference by examples/consumer/retry.
func WithMaxReconsumeTimes(times int32) Option {
return func(opts *consumerOptions) {
opts.MaxReconsumeTimes = times
}
}
func WithStrategy(strategy AllocateStrategy) Option {
return func(opts *consumerOptions) {
opts.Strategy = strategy
}
}
func WithPullBatchSize(batchSize int32) Option {
return func(options *consumerOptions) {
options.PullBatchSize.Store(batchSize)
}
}
func WithRebalanceLockInterval(interval time.Duration) Option {
return func(options *consumerOptions) {
options.RebalanceLockInterval = interval
}
}
func WithAutoCommit(auto bool) Option {
return func(options *consumerOptions) {
options.AutoCommit = auto
}
}
func WithSuspendCurrentQueueTimeMillis(suspendT time.Duration) Option {
return func(options *consumerOptions) {
options.SuspendCurrentQueueTimeMillis = suspendT
}
}
func WithPullInterval(interval time.Duration) Option {
return func(options *consumerOptions) {
options.PullInterval.Store(interval)
}
}
// WithConsumeTPS set single-machine consumption tps
func WithConsumeTPS(tps int32) Option {
return func(options *consumerOptions) {
options.ConsumeTPS.Store(tps)
}
}
// WithNsResolver set nameserver resolver to fetch nameserver addr
func WithNsResolver(resolver primitive.NsResolver) Option {
return func(options *consumerOptions) {
options.Resolver = resolver
}
}
// WithNameServer set NameServer address, only support one NameServer cluster in alpha2
func WithNameServer(nameServers primitive.NamesrvAddr) Option {
return func(options *consumerOptions) {
options.Resolver = primitive.NewPassthroughResolver(nameServers)
}
}
// WithNameServerDomain set NameServer domain
func WithNameServerDomain(nameServerUrl string) Option {
return func(opts *consumerOptions) {
h := primitive.NewHttpResolver("DEFAULT", nameServerUrl)
if opts.UnitName != "" {
h.DomainWithUnit(opts.UnitName)
}
opts.Resolver = h
}
}
// WithUnitName set the name of specified unit
func WithUnitName(unitName string) Option {
return func(opts *consumerOptions) {
opts.UnitName = strings.TrimSpace(unitName)
if ns, ok := opts.Resolver.(*primitive.HttpResolver); ok {
ns.DomainWithUnit(opts.UnitName)
}
}
}
func WithConsumeTimeout(timeout time.Duration) Option {
return func(opts *consumerOptions) {
opts.ConsumeTimeout = timeout
}
}
func WithConsumeGoroutineNums(nums int) Option {
return func(opts *consumerOptions) {
opts.ConsumeGoroutineNums = nums
}
}
func WithFilterMessageHook(hooks []hooks.FilterMessageHook) Option {
return func(opts *consumerOptions) {
opts.filterMessageHooks = hooks
}
}
func WithLimiter(limiter Limiter) Option {
return func(opts *consumerOptions) {
opts.Limiter = limiter
}
}
func WithTls(useTls bool) Option {
return func(opts *consumerOptions) {
opts.ClientOptions.RemotingClientConfig.UseTls = useTls
}
}