Files
open-im-server/internal/msgtransfer/persistent_msg_handler.go
T

90 lines
3.1 KiB
Go
Raw Normal View History

2021-05-26 19:24:25 +08:00
/*
** description("").
** copyright('tuoyun,www.tuoyun.net').
** author("fg,Gordon@tuoyun.net").
** time(2021/5/11 15:37).
*/
2023-02-09 20:36:34 +08:00
package msgtransfer
2021-05-26 19:24:25 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/controller"
kfk "OpenIM/pkg/common/kafka"
"OpenIM/pkg/common/log"
2023-03-03 17:42:26 +08:00
"OpenIM/pkg/common/tracelog"
2023-02-23 19:15:30 +08:00
pbMsg "OpenIM/pkg/proto/msg"
"OpenIM/pkg/utils"
2023-03-03 17:42:26 +08:00
"context"
2022-09-09 01:10:06 +08:00
2021-05-26 19:24:25 +08:00
"github.com/Shopify/sarama"
"github.com/golang/protobuf/proto"
)
type PersistentConsumerHandler struct {
persistentConsumerGroup *kfk.MConsumerGroup
2023-03-03 17:42:26 +08:00
chatLogDatabase controller.ChatLogDatabase
2021-05-26 19:24:25 +08:00
}
2023-03-03 17:42:26 +08:00
func NewPersistentConsumerHandler(database controller.ChatLogDatabase) *PersistentConsumerHandler {
return &PersistentConsumerHandler{
persistentConsumerGroup: kfk.NewMConsumerGroup(&kfk.MConsumerGroupConfig{KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.Ws2mschat.Topic},
config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMySql),
chatLogDatabase: database,
}
2022-09-12 19:32:24 +08:00
}
2023-03-03 17:42:26 +08:00
func (pc *PersistentConsumerHandler) handleChatWs2Mysql(ctx context.Context, cMsg *sarama.ConsumerMessage, msgKey string, _ sarama.ConsumerGroupSession) {
2022-05-19 14:21:38 +08:00
msg := cMsg.Value
2023-03-03 17:42:26 +08:00
operationID := tracelog.GetOperationID(ctx)
2022-06-08 16:53:58 +08:00
log.NewInfo("msg come here mysql!!!", "", "msg", string(msg), msgKey)
2022-03-30 18:23:05 +08:00
var tag bool
2021-12-23 17:34:32 +08:00
msgFromMQ := pbMsg.MsgDataToMQ{}
err := proto.Unmarshal(msg, &msgFromMQ)
2021-05-26 19:24:25 +08:00
if err != nil {
2023-03-03 17:42:26 +08:00
log.NewError(operationID, "msg_transfer Unmarshal msg err", "msg", string(msg), "err", err.Error())
2021-05-26 19:24:25 +08:00
return
}
2023-03-03 17:42:26 +08:00
log.Debug(operationID, "proto.Unmarshal MsgDataToMQ", msgFromMQ.String())
2021-05-26 19:24:25 +08:00
//Control whether to store history messages (mysql)
2021-12-23 17:34:32 +08:00
isPersist := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsPersistent)
2021-05-26 19:24:25 +08:00
//Only process receiver data
2021-07-06 20:04:57 +08:00
if isPersist {
2022-03-30 18:23:05 +08:00
switch msgFromMQ.MsgData.SessionType {
case constant.SingleChatType, constant.NotificationChatType:
if msgKey == msgFromMQ.MsgData.RecvID {
tag = true
2021-07-06 20:04:57 +08:00
}
2022-03-30 18:23:05 +08:00
case constant.GroupChatType:
if msgKey == msgFromMQ.MsgData.SendID {
2022-03-30 18:23:05 +08:00
tag = true
}
2022-05-30 19:31:09 +08:00
case constant.SuperGroupChatType:
tag = true
2022-03-30 18:23:05 +08:00
}
if tag {
2023-03-03 17:42:26 +08:00
log.NewInfo(operationID, "msg_transfer msg persisting", string(msg))
2023-03-06 16:23:16 +08:00
if err = pc.chatLogDatabase.CreateChatLog(msgFromMQ); err != nil {
2023-03-03 17:42:26 +08:00
log.NewError(operationID, "Message insert failed", "err", err.Error(), "msg", msgFromMQ.String())
2021-07-06 20:04:57 +08:00
return
}
2021-05-26 19:24:25 +08:00
}
2021-07-06 20:04:57 +08:00
}
2021-05-26 19:24:25 +08:00
}
func (PersistentConsumerHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
func (PersistentConsumerHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
2023-03-03 17:42:26 +08:00
func (pc *PersistentConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
2021-05-26 19:24:25 +08:00
for msg := range claim.Messages() {
2022-06-08 16:53:58 +08:00
log.NewDebug("", "kafka get info to mysql", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value), "key", string(msg.Key))
2022-06-08 17:11:17 +08:00
if len(msg.Value) != 0 {
2023-03-03 17:42:26 +08:00
ctx := pc.persistentConsumerGroup.GetContextFromMsg(msg)
pc.handleChatWs2Mysql(ctx, msg, string(msg.Key), sess)
2022-06-08 17:19:37 +08:00
} else {
log.Error("", "msg get from kafka but is nil", msg.Key)
2022-06-08 17:11:17 +08:00
}
2021-09-13 15:48:19 +08:00
sess.MarkMessage(msg, "")
2021-05-26 19:24:25 +08:00
}
return nil
}