@@ -25,10 +25,10 @@ import (
2525 "strconv"
2626 "strings"
2727 "sync"
28- "sync/atomic"
2928 "time"
3029
3130 errors2 "github.com/apache/rocketmq-client-go/v2/errors"
31+ "go.uber.org/atomic"
3232
3333 "github.com/pkg/errors"
3434
@@ -97,7 +97,7 @@ func NewPushConsumer(opts ...Option) (*pushConsumer, error) {
9797 client : internal .GetOrNewRocketMQClient (defaultOpts .ClientOptions , nil ),
9898 consumerGroup : defaultOpts .GroupName ,
9999 cType : _PushConsume ,
100- state : int32 (internal .StateCreateJust ),
100+ state : atomic . NewInt32 ( int32 (internal .StateCreateJust ) ),
101101 prCh : make (chan PullRequest , 4 ),
102102 model : defaultOpts .ConsumerModel ,
103103 consumeOrderly : defaultOpts .ConsumeOrderly ,
@@ -138,7 +138,7 @@ func (pc *pushConsumer) Start() error {
138138 "messageModel" : pc .model ,
139139 "unitMode" : pc .unitMode ,
140140 })
141- atomic . StoreInt32 ( & pc .state , int32 (internal .StateStartFailed ))
141+ pc .state . Store ( int32 (internal .StateStartFailed ))
142142 err = pc .validate ()
143143 if err != nil {
144144 rlog .Error ("the consumer group option validate fail" , map [string ]interface {}{
@@ -289,8 +289,8 @@ func (pc *pushConsumer) Shutdown() error {
289289
290290func (pc * pushConsumer ) Subscribe (topic string , selector MessageSelector ,
291291 f func (context.Context , ... * primitive.MessageExt ) (ConsumeResult , error )) error {
292- if atomic . LoadInt32 ( & pc .state ) == int32 (internal .StateStartFailed ) ||
293- atomic . LoadInt32 ( & pc .state ) == int32 (internal .StateShutdown ) {
292+ if pc .state . Load ( ) == int32 (internal .StateStartFailed ) ||
293+ pc .state . Load ( ) == int32 (internal .StateShutdown ) {
294294 return errors2 .ErrStartTopic
295295 }
296296
@@ -685,7 +685,7 @@ func (pc *pushConsumer) pullMessage(request *PullRequest) {
685685 goto NEXT
686686 }
687687
688- if pc .pause {
688+ if pc .pause . Load () {
689689 rlog .Debug (fmt .Sprintf ("consumer [%s] of [%s] was paused, execute pull request [%s] later" ,
690690 pc .option .InstanceName , pc .consumerGroup , request .String ()), nil )
691691 sleepTime = _PullDelayTimeWhenSuspend
@@ -945,12 +945,12 @@ func (pc *pushConsumer) buildSendBackRequest(msg *primitive.MessageExt, delayLev
945945}
946946
947947func (pc * pushConsumer ) suspend () {
948- pc .pause = true
948+ pc .pause . Store ( true )
949949 rlog .Info (fmt .Sprintf ("suspend consumer: %s" , pc .consumerGroup ), nil )
950950}
951951
952952func (pc * pushConsumer ) resume () {
953- pc .pause = false
953+ pc .pause . Store ( false )
954954 pc .doBalance ()
955955 rlog .Info (fmt .Sprintf ("resume consumer: %s" , pc .consumerGroup ), nil )
956956}
0 commit comments