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.
175 lines
6.8 KiB
175 lines
6.8 KiB
4 years ago
|
package logic
|
||
|
|
||
|
import (
|
||
3 years ago
|
"Open_IM/pkg/common/config"
|
||
|
"Open_IM/pkg/common/constant"
|
||
|
kfk "Open_IM/pkg/common/kafka"
|
||
|
"Open_IM/pkg/common/log"
|
||
|
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||
3 years ago
|
pbMsg "Open_IM/pkg/proto/chat"
|
||
|
pbPush "Open_IM/pkg/proto/push"
|
||
3 years ago
|
"Open_IM/pkg/utils"
|
||
4 years ago
|
"context"
|
||
3 years ago
|
"errors"
|
||
4 years ago
|
"github.com/Shopify/sarama"
|
||
|
"github.com/golang/protobuf/proto"
|
||
|
"strings"
|
||
3 years ago
|
"time"
|
||
4 years ago
|
)
|
||
|
|
||
|
type fcb func(msg []byte, msgKey string)
|
||
3 years ago
|
type Cmd2Value struct {
|
||
|
Cmd string
|
||
|
Value interface{}
|
||
|
}
|
||
|
type OnlineHistoryConsumerHandler struct {
|
||
|
msgHandle map[string]fcb
|
||
|
historyConsumerGroup *kfk.MConsumerGroup
|
||
|
cmdCh chan Cmd2Value
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (och *OnlineHistoryConsumerHandler) Init(cmdCh chan Cmd2Value) {
|
||
|
och.msgHandle = make(map[string]fcb)
|
||
|
och.cmdCh = cmdCh
|
||
|
och.msgHandle[config.Config.Kafka.Ws2mschat.Topic] = och.handleChatWs2Mongo
|
||
|
och.historyConsumerGroup = kfk.NewMConsumerGroup(&kfk.MConsumerGroupConfig{KafkaVersion: sarama.V0_10_2_0,
|
||
4 years ago
|
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.Ws2mschat.Topic},
|
||
|
config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMongo)
|
||
|
|
||
|
}
|
||
3 years ago
|
func (och *OnlineHistoryConsumerHandler) TriggerCmd(status string) {
|
||
|
operationID := utils.OperationIDGenerator()
|
||
|
for {
|
||
|
err := sendCmd(och.cmdCh, Cmd2Value{Cmd: status, Value: ""}, 1)
|
||
|
if err != nil {
|
||
|
log.Error(operationID, "TriggerCmd failed ", err.Error(), status)
|
||
|
continue
|
||
|
}
|
||
|
log.Debug(operationID, "TriggerCmd success", status)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
func sendCmd(ch chan Cmd2Value, value Cmd2Value, timeout int64) error {
|
||
|
var flag = 0
|
||
|
select {
|
||
|
case ch <- value:
|
||
|
flag = 1
|
||
|
case <-time.After(time.Second * time.Duration(timeout)):
|
||
|
flag = 2
|
||
|
}
|
||
|
if flag == 1 {
|
||
|
return nil
|
||
|
} else {
|
||
|
return errors.New("send cmd timeout")
|
||
|
}
|
||
|
}
|
||
|
func (och *OnlineHistoryConsumerHandler) handleChatWs2Mongo(msg []byte, msgKey string) {
|
||
3 years ago
|
now := time.Now()
|
||
3 years ago
|
msgFromMQ := pbMsg.MsgDataToMQ{}
|
||
|
err := proto.Unmarshal(msg, &msgFromMQ)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error("msg_transfer Unmarshal msg err", "", "msg", string(msg), "err", err.Error())
|
||
4 years ago
|
return
|
||
|
}
|
||
3 years ago
|
operationID := msgFromMQ.OperationID
|
||
3 years ago
|
log.NewInfo(operationID, "msg come mongo!!!", "", "msg", string(msg))
|
||
4 years ago
|
//Control whether to store offline messages (mongo)
|
||
3 years ago
|
isHistory := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsHistory)
|
||
4 years ago
|
//Control whether to store history messages (mysql)
|
||
3 years ago
|
isPersist := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsPersistent)
|
||
3 years ago
|
isSenderSync := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsSenderSync)
|
||
3 years ago
|
switch msgFromMQ.MsgData.SessionType {
|
||
3 years ago
|
case constant.SingleChatType:
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer msg type = SingleChatType", isHistory, isPersist)
|
||
4 years ago
|
if isHistory {
|
||
3 years ago
|
err := saveUserChat(msgKey, &msgFromMQ)
|
||
|
if err != nil {
|
||
3 years ago
|
singleMsgFailedCount++
|
||
3 years ago
|
log.NewError(operationID, "single data insert to mongo err", err.Error(), msgFromMQ.String())
|
||
|
return
|
||
4 years ago
|
}
|
||
3 years ago
|
singleMsgSuccessCount++
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "sendMessageToPush cost time ", time.Since(now))
|
||
4 years ago
|
}
|
||
3 years ago
|
if !isSenderSync && msgKey == msgFromMQ.MsgData.SendID {
|
||
|
} else {
|
||
|
go sendMessageToPush(&msgFromMQ, msgKey)
|
||
|
}
|
||
3 years ago
|
log.NewDebug(operationID, "saveUserChat cost time ", time.Since(now))
|
||
3 years ago
|
case constant.GroupChatType:
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer msg type = GroupChatType", isHistory, isPersist)
|
||
3 years ago
|
if isHistory {
|
||
3 years ago
|
err := saveUserChat(msgFromMQ.MsgData.RecvID, &msgFromMQ)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
log.NewError(operationID, "group data insert to mongo err", msgFromMQ.String(), msgFromMQ.MsgData.RecvID, err.Error())
|
||
3 years ago
|
return
|
||
|
}
|
||
3 years ago
|
groupMsgCount++
|
||
3 years ago
|
}
|
||
3 years ago
|
go sendMessageToPush(&msgFromMQ, msgFromMQ.MsgData.RecvID)
|
||
3 years ago
|
case constant.NotificationChatType:
|
||
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer msg type = NotificationChatType", isHistory, isPersist)
|
||
|
if isHistory {
|
||
|
err := saveUserChat(msgKey, &msgFromMQ)
|
||
|
if err != nil {
|
||
|
log.NewError(operationID, "single data insert to mongo err", err.Error(), msgFromMQ.String())
|
||
|
return
|
||
|
}
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "sendMessageToPush cost time ", time.Since(now))
|
||
3 years ago
|
}
|
||
|
if !isSenderSync && msgKey == msgFromMQ.MsgData.SendID {
|
||
|
} else {
|
||
|
go sendMessageToPush(&msgFromMQ, msgKey)
|
||
|
}
|
||
3 years ago
|
log.NewDebug(operationID, "saveUserChat cost time ", time.Since(now))
|
||
3 years ago
|
default:
|
||
3 years ago
|
log.NewError(msgFromMQ.OperationID, "SessionType error", msgFromMQ.String())
|
||
3 years ago
|
return
|
||
4 years ago
|
}
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer handle topic data to database success...", msgFromMQ.String())
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (OnlineHistoryConsumerHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
|
||
|
func (OnlineHistoryConsumerHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
|
||
|
func (och *OnlineHistoryConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession,
|
||
|
claim sarama.ConsumerGroupClaim) error { // a instance in the consumer group
|
||
|
och.TriggerCmd(OnlineTopicBusy)
|
||
|
SetOnlineTopicStatus(OnlineTopicBusy)
|
||
4 years ago
|
for msg := range claim.Messages() {
|
||
3 years ago
|
log.NewDebug("", "kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value))
|
||
3 years ago
|
och.msgHandle[msg.Topic](msg.Value, string(msg.Key))
|
||
3 years ago
|
sess.MarkMessage(msg, "")
|
||
3 years ago
|
if claim.HighWaterMarkOffset()-msg.Offset <= 1 {
|
||
|
och.TriggerCmd(OnlineTopicVacancy)
|
||
|
SetOnlineTopicStatus(OnlineTopicVacancy)
|
||
|
}
|
||
4 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
3 years ago
|
func sendMessageToPush(message *pbMsg.MsgDataToMQ, pushToUserID string) {
|
||
3 years ago
|
log.Info(message.OperationID, "msg_transfer send message to push", "message", message.String())
|
||
3 years ago
|
rpcPushMsg := pbPush.PushMsgReq{OperationID: message.OperationID, MsgData: message.MsgData, PushToUserID: pushToUserID}
|
||
|
mqPushMsg := pbMsg.PushMsgDataToMQ{OperationID: message.OperationID, MsgData: message.MsgData, PushToUserID: pushToUserID}
|
||
4 years ago
|
grpcConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImPushName)
|
||
|
if grpcConn == nil {
|
||
3 years ago
|
log.Error(rpcPushMsg.OperationID, "rpc dial failed", "push data", rpcPushMsg.String())
|
||
3 years ago
|
pid, offset, err := producer.SendMessage(&mqPushMsg)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error(mqPushMsg.OperationID, "kafka send failed", "send data", message.String(), "pid", pid, "offset", offset, "err", err.Error())
|
||
4 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
msgClient := pbPush.NewPushMsgServiceClient(grpcConn)
|
||
3 years ago
|
_, err := msgClient.PushMsg(context.Background(), &rpcPushMsg)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error(rpcPushMsg.OperationID, "rpc send failed", rpcPushMsg.OperationID, "push data", rpcPushMsg.String(), "err", err.Error())
|
||
3 years ago
|
pid, offset, err := producer.SendMessage(&mqPushMsg)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error(message.OperationID, "kafka send failed", mqPushMsg.OperationID, "send data", mqPushMsg.String(), "pid", pid, "offset", offset, "err", err.Error())
|
||
4 years ago
|
}
|
||
|
} else {
|
||
3 years ago
|
log.Info(message.OperationID, "rpc send success", rpcPushMsg.OperationID, "push data", rpcPushMsg.String())
|
||
4 years ago
|
|
||
|
}
|
||
|
}
|