Skip to content

add ReqUnRegisterClient, graceful Exit #1198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 41 additions & 2 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ func GetOrNewRocketMQClient(option ClientOptions, callbackCh chan interface{}) R
}

func (c *rmqClient) Start() {
//ctx, cancel := context.WithCancel(context.Background())
//c.cancel = cancel
// ctx, cancel := context.WithCancel(context.Background())
// c.cancel = cancel
atomic.AddInt32(&c.instanceCount, 1)
c.once.Do(func() {
if !c.option.Credentials.IsEmpty() {
Expand Down Expand Up @@ -687,6 +687,43 @@ func (c *rmqClient) SendHeartbeatToAllBrokerWithLock() {
})
}

func (c *rmqClient) unRegisterClientWithLock(producerGroup string, consumerGroup string) {
c.hbMutex.Lock()
defer c.hbMutex.Unlock()
c.GetNameSrv().(*namesrvs).brokerAddressesMap.Range(func(key, value interface{}) bool {
brokerName := key.(string)
data := value.(*BrokerData)
header := &UnregisterClientRequestHeader{
clientID: c.ClientID(),
producerGroup: producerGroup,
consumerGroup: consumerGroup,
}
for id, addr := range data.BrokerAddresses {
cmd := remote.NewRemotingCommand(ReqUnRegisterClient, header, nil)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
response, err := c.remoteClient.InvokeSync(ctx, addr, cmd)
if err != nil {
cancel()
rlog.Warning("send unRegister client to broker error", map[string]interface{}{
rlog.LogKeyUnderlayError: err,
})
return true
}
cancel()
if response.Code != ResSuccess {
rlog.Warning("send unRegister client to broker failed", map[string]interface{}{
"brokerName": brokerName,
"brokerId": id,
"brokerAddr": addr,
"responseCode": response.Code,
"remark": response.Remark,
})
}
}
return true
})
}

func (c *rmqClient) UpdateTopicRouteInfo() {
allTopics := make(map[string]bool, 0)
publishTopicSet := make(map[string]bool, 0)
Expand Down Expand Up @@ -843,6 +880,7 @@ func (c *rmqClient) RegisterConsumer(group string, consumer InnerConsumer) error

func (c *rmqClient) UnregisterConsumer(group string) {
c.consumerMap.Delete(group)
c.unRegisterClientWithLock("", group)
}

func (c *rmqClient) RegisterProducer(group string, producer InnerProducer) error {
Expand All @@ -859,6 +897,7 @@ func (c *rmqClient) RegisterProducer(group string, producer InnerProducer) error

func (c *rmqClient) UnregisterProducer(group string) {
c.producerMap.Delete(group)
c.unRegisterClientWithLock(group, "")
}

func (c *rmqClient) RebalanceImmediately() {
Expand Down
15 changes: 15 additions & 0 deletions internal/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
ReqGetMinOffset = int16(31)
ReqViewMessageByID = int16(33)
ReqHeartBeat = int16(34)
ReqUnRegisterClient = int16(35)
ReqConsumerSendMsgBack = int16(36)
ReqENDTransaction = int16(37)
ReqGetConsumerListByGroup = int16(38)
Expand Down Expand Up @@ -634,3 +635,17 @@ func (request *ReplyMessageRequestHeader) Decode(properties map[string]string) {
request.storeTimestamp, _ = strconv.ParseInt(v, 10, 0)
}
}

type UnregisterClientRequestHeader struct {
clientID string
producerGroup string
consumerGroup string
}

func (request *UnregisterClientRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["clientId"] = request.clientID
maps["producerGroup"] = request.producerGroup
maps["consumerGroup"] = request.consumerGroup
return maps
}