Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions consumer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type consumerOptions struct {
RebalanceLockInterval time.Duration

Resolver primitive.NsResolver

ConsumeGoroutineNums int
}

func defaultPushConsumerOptions() consumerOptions {
Expand All @@ -120,6 +122,7 @@ func defaultPushConsumerOptions() consumerOptions {
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
Expand Down Expand Up @@ -178,6 +181,30 @@ func WithConsumeConcurrentlyMaxSpan(consumeConcurrentlyMaxSpan int) Option {
}
}

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.
Expand Down Expand Up @@ -300,3 +327,9 @@ func WithConsumeTimeout(timeout time.Duration) Option {
opts.ConsumeTimeout = timeout
}
}

func WithConsumeGoroutineNums(nums int) Option {
return func(opts *consumerOptions) {
opts.ConsumeGoroutineNums = nums
}
}
6 changes: 6 additions & 0 deletions consumer/push_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type pushConsumer struct {
queueLock *QueueLock
done chan struct{}
closeOnce sync.Once
crCh chan struct{}
}

func NewPushConsumer(opts ...Option) (*pushConsumer, error) {
Expand Down Expand Up @@ -115,6 +116,7 @@ func NewPushConsumer(opts ...Option) (*pushConsumer, error) {
queueLock: newQueueLock(),
done: make(chan struct{}, 1),
consumeFunc: utils.NewSet(),
crCh: make(chan struct{}, defaultOpts.ConsumeGoroutineNums),
}
dc.mqChanged = p.messageQueueChanged
if p.consumeOrderly {
Expand Down Expand Up @@ -1023,13 +1025,16 @@ func (pc *pushConsumer) consumeMessageCurrently(pq *processQueue, mq *primitive.
subMsgs = msgs[count:next]
count = next - 1
}

pc.crCh <- struct{}{}
go primitive.WithRecover(func() {
RETRY:
if pq.IsDroppd() {
rlog.Info("the message queue not be able to consume, because it was dropped", map[string]interface{}{
rlog.LogKeyMessageQueue: mq.String(),
rlog.LogKeyConsumerGroup: pc.consumerGroup,
})
<-pc.crCh
return
}

Expand Down Expand Up @@ -1109,6 +1114,7 @@ func (pc *pushConsumer) consumeMessageCurrently(pq *processQueue, mq *primitive.
"message": subMsgs,
})
}
<-pc.crCh
})
}
}
Expand Down