fix: change consumer group receive messages avoid kafka consumer group always rebalanced. (#1942)

pull/1963/head
OpenIM-Gordon 8 months ago committed by GitHub
parent 6c4334b730
commit 8e6c1d74c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -20,8 +20,6 @@ import (
"fmt"
"github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/log"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
"net/http"
@ -118,11 +116,8 @@ func (m *MsgTransfer) Start(prometheusPort int) error {
netErr error
)
onError := func(ctx context.Context, err error, errInfo string) {
log.ZWarn(ctx, errInfo, err)
}
go m.historyCH.historyConsumerGroup.RegisterHandleAndConsumer(m.ctx, m.historyCH, onError)
go m.historyMongoCH.historyConsumerGroup.RegisterHandleAndConsumer(m.ctx, m.historyMongoCH, onError)
go m.historyCH.historyConsumerGroup.RegisterHandleAndConsumer(m.ctx, m.historyCH)
go m.historyMongoCH.historyConsumerGroup.RegisterHandleAndConsumer(m.ctx, m.historyMongoCH)
if config.Config.Prometheus.Enable {
go func() {

@ -438,6 +438,7 @@ func (och *OnlineHistoryRedisConsumerHandler) ConsumeClaim(
wg = sync.WaitGroup{}
running = new(atomic.Bool)
)
running.Store(true)
wg.Add(1)
go func() {

@ -16,8 +16,6 @@ package push
import (
"context"
"github.com/OpenIMSDK/tools/log"
)
type Consumer struct {
@ -36,9 +34,6 @@ func NewConsumer(pusher *Pusher) (*Consumer, error) {
}
func (c *Consumer) Start() {
onError := func(ctx context.Context, err error, errInfo string) {
log.ZWarn(ctx, errInfo, err)
}
go c.pushCh.pushConsumerGroup.RegisterHandleAndConsumer(context.Background(), &c.pushCh, onError)
go c.pushCh.pushConsumerGroup.RegisterHandleAndConsumer(context.Background(), &c.pushCh)
}

@ -17,8 +17,6 @@ package kafka
import (
"context"
"errors"
"fmt"
"github.com/IBM/sarama"
"github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/log"
@ -67,18 +65,19 @@ func (mc *MConsumerGroup) GetContextFromMsg(cMsg *sarama.ConsumerMessage) contex
return GetContextWithMQHeader(cMsg.Headers)
}
func (mc *MConsumerGroup) RegisterHandleAndConsumer(ctx context.Context, handler sarama.ConsumerGroupHandler, onError func(context.Context, error, string)) {
func (mc *MConsumerGroup) RegisterHandleAndConsumer(ctx context.Context, handler sarama.ConsumerGroupHandler) {
log.ZDebug(ctx, "register consumer group", "groupID", mc.groupID)
for {
err := mc.ConsumerGroup.Consume(ctx, mc.topics, handler)
if errors.Is(err, sarama.ErrClosedConsumerGroup) || errors.Is(err, context.Canceled) {
if errors.Is(err, sarama.ErrClosedConsumerGroup) {
return
}
if err != nil {
errInfo := fmt.Sprintf("consume err: %v, topic: %v, groupID: %s", err, strings.Join(mc.topics, ", "), mc.groupID)
onError(ctx, err, errInfo) // 调用回调函数处理错误
if errors.Is(err, context.Canceled) {
return
}
if err != nil {
log.ZWarn(ctx, "consume err", err, "topic", mc.topics, "groupID", mc.groupID)
}
}
}

Loading…
Cancel
Save