You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/push/push_handler.go

74 lines
2.3 KiB

4 years ago
/*
** description("").
2 years ago
** copyright('OpenIM,www.OpenIM.io').
4 years ago
** author("fg,Gordon@tuoyun.net").
** time(2021/5/13 10:33).
*/
2 years ago
package push
4 years ago
import (
2 years ago
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
kfk "OpenIM/pkg/common/kafka"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/tracelog"
pbChat "OpenIM/pkg/proto/msg"
pbPush "OpenIM/pkg/proto/push"
"OpenIM/pkg/utils"
2 years ago
"context"
4 years ago
"github.com/Shopify/sarama"
"github.com/golang/protobuf/proto"
)
2 years ago
type ConsumerHandler struct {
4 years ago
pushConsumerGroup *kfk.MConsumerGroup
2 years ago
pusher Pusher
4 years ago
}
2 years ago
func (c *ConsumerHandler) Init() {
c.pushConsumerGroup = kfk.NewMConsumerGroup(&kfk.MConsumerGroupConfig{KafkaVersion: sarama.V2_0_0_0,
4 years ago
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.Ms2pschat.Topic}, config.Config.Kafka.Ms2pschat.Addr,
config.Config.Kafka.ConsumerGroupID.MsgToPush)
}
2 years ago
2 years ago
func (c *ConsumerHandler) handleMs2PsChat(msg []byte) {
3 years ago
log.NewDebug("", "msg come from kafka And push!!!", "msg", string(msg))
msgFromMQ := pbChat.PushMsgDataToMQ{}
if err := proto.Unmarshal(msg, &msgFromMQ); err != nil {
3 years ago
log.Error("", "push Unmarshal msg err", "msg", string(msg), "err", err.Error())
4 years ago
return
}
2 years ago
pbData := &pbPush.PushMsgReq{
2 years ago
MsgData: msgFromMQ.MsgData,
SourceID: msgFromMQ.PushToUserID,
2 years ago
}
2 years ago
sec := msgFromMQ.MsgData.SendTime / 1000
nowSec := utils.GetCurrentTimestampBySecond()
if nowSec-sec > 10 {
return
}
2 years ago
ctx := context.Background()
tracelog.SetOperationID(ctx, "")
var err error
2 years ago
switch msgFromMQ.MsgData.SessionType {
case constant.SuperGroupChatType:
2 years ago
err = c.pusher.MsgToSuperGroupUser(ctx, pbData.SourceID, pbData.MsgData)
2 years ago
default:
2 years ago
err = c.pusher.MsgToUser(ctx, pbData.SourceID, pbData.MsgData)
}
if err != nil {
log.NewError("", "push failed", *pbData)
2 years ago
}
4 years ago
}
2 years ago
func (ConsumerHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
func (ConsumerHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
func (c *ConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession,
4 years ago
claim sarama.ConsumerGroupClaim) error {
for msg := range claim.Messages() {
3 years ago
log.NewDebug("", "kafka get info to mysql", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value))
2 years ago
c.handleMs2PsChat(msg.Value)
2 years ago
sess.MarkMessage(msg, "")
4 years ago
}
return nil
}