Skip to content

Commit

Permalink
Add param which like consumeThreadMin of java sdk to control consumpt…
Browse files Browse the repository at this point in the history
…ion rate (#883)

* add param which like consumeThreadMin of java sdk to control consumption rate

* make some configs can be set for push_consumer

Co-authored-by: dengzhiwen1 <[email protected]>
  • Loading branch information
cserwen and dengzhiwen1 authored Aug 7, 2022
1 parent 43edd17 commit e1ddb88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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

0 comments on commit e1ddb88

Please sign in to comment.