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/rpc/msg/server.go

156 lines
4.8 KiB

package msg
4 years ago
import (
2 years ago
"Open_IM/internal/common/check"
"Open_IM/internal/common/notification"
"Open_IM/internal/common/rpcserver"
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db/controller"
"Open_IM/pkg/common/kafka"
"Open_IM/pkg/common/log"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
2 years ago
"Open_IM/pkg/proto/msg"
3 years ago
"Open_IM/pkg/utils"
2 years ago
"github.com/OpenIMSDK/getcdv3"
4 years ago
"net"
"strconv"
"strings"
2 years ago
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
2 years ago
"google.golang.org/grpc"
)
type msgServer struct {
*rpcserver.RpcServer
2 years ago
MsgInterface controller.MsgInterface
2 years ago
Group *check.GroupChecker
User *check.UserCheck
Conversation *check.ConversationChecker
3 years ago
}
type deleteMsg struct {
UserID string
OpUserID string
SeqList []uint32
OperationID string
4 years ago
}
func NewRpcChatServer(port int) *msgServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
rc := msgServer{
4 years ago
rpcPort: port,
2 years ago
rpcRegisterName: config.Config.RpcRegisterName.OpenImMsgName,
4 years ago
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
2 years ago
dMessageLocker: NewLockerMessage(),
4 years ago
}
2 years ago
rc.messageWriter = kafka.NewKafkaProducer(config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.Ws2mschat.Topic)
//rc.offlineProducer = kafka.NewKafkaProducer(config.Config.Kafka.Ws2mschatOffline.Addr, config.Config.Kafka.Ws2mschatOffline.Topic)
3 years ago
rc.delMsgCh = make(chan deleteMsg, 1000)
4 years ago
return &rc
}
2 years ago
func (rpc *rpcChat) initPrometheus() {
//sendMsgSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
// Name: "send_msg_success",
// Help: "The number of send msg success",
//})
//sendMsgFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
// Name: "send_msg_failed",
// Help: "The number of send msg failed",
//})
promePkg.NewMsgPullFromRedisSuccessCounter()
promePkg.NewMsgPullFromRedisFailedCounter()
promePkg.NewMsgPullFromMongoSuccessCounter()
promePkg.NewMsgPullFromMongoFailedCounter()
promePkg.NewSingleChatMsgRecvSuccessCounter()
promePkg.NewGroupChatMsgRecvSuccessCounter()
promePkg.NewWorkSuperGroupChatMsgRecvSuccessCounter()
promePkg.NewSingleChatMsgProcessSuccessCounter()
promePkg.NewSingleChatMsgProcessFailedCounter()
promePkg.NewGroupChatMsgProcessSuccessCounter()
promePkg.NewGroupChatMsgProcessFailedCounter()
promePkg.NewWorkSuperGroupChatMsgProcessSuccessCounter()
promePkg.NewWorkSuperGroupChatMsgProcessFailedCounter()
2 years ago
}
2 years ago
func (m *msgServer) Run() {
3 years ago
log.Info("", "rpcChat init...")
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
2 years ago
address := listenIP + ":" + strconv.Itoa(m.rpcPort)
4 years ago
listener, err := net.Listen("tcp", address)
if err != nil {
2 years ago
panic("listening err:" + err.Error() + m.rpcRegisterName)
4 years ago
}
3 years ago
log.Info("", "listen network success, address ", address)
2 years ago
recvSize := 1024 * 1024 * 30
sendSize := 1024 * 1024 * 30
var grpcOpts = []grpc.ServerOption{
grpc.MaxRecvMsgSize(recvSize),
grpc.MaxSendMsgSize(sendSize),
}
2 years ago
if config.Config.Prometheus.Enable {
2 years ago
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
2 years ago
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
2 years ago
}
srv := grpc.NewServer(grpcOpts...)
4 years ago
defer srv.GracefulStop()
2 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
2 years ago
msg.RegisterMsgServer(srv, m)
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
2 years ago
err = getcdv3.RegisterEtcd(m.etcdSchema, strings.Join(m.etcdAddr, ","), rpcRegisterIP, m.rpcPort, m.rpcRegisterName, 10, "")
4 years ago
if err != nil {
3 years ago
log.Error("", "register rpcChat to etcd failed ", err.Error())
2 years ago
panic(utils.Wrap(err, "register chat module m to etcd err"))
4 years ago
}
2 years ago
go m.runCh()
m.initPrometheus()
4 years ago
err = srv.Serve(listener)
if err != nil {
2 years ago
log.Error("", "m rpcChat failed ", err.Error())
4 years ago
return
}
2 years ago
log.Info("", "m rpcChat init success")
4 years ago
}
3 years ago
func (rpc *rpcChat) runCh() {
log.NewInfo("", "start del msg chan ")
for {
select {
case msg := <-rpc.delMsgCh:
log.NewInfo(msg.OperationID, utils.GetSelfFuncName(), "delmsgch recv new: ", msg)
db.DB.DelMsgFromCache(msg.UserID, msg.SeqList, msg.OperationID)
unexistSeqList, err := db.DB.DelMsgBySeqList(msg.UserID, msg.SeqList, msg.OperationID)
if err != nil {
3 years ago
log.NewError(msg.OperationID, utils.GetSelfFuncName(), "DelMsgBySeqList args: ", msg.UserID, msg.SeqList, msg.OperationID, err.Error())
continue
}
if len(unexistSeqList) > 0 {
notification.DeleteMessageNotification(msg.OpUserID, msg.UserID, unexistSeqList, msg.OperationID)
3 years ago
}
}
}
}