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/rpcChat.go

160 lines
4.9 KiB

package msg
4 years ago
import (
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
3 years ago
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/kafka"
"Open_IM/pkg/common/log"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
2 years ago
"Open_IM/pkg/proto/msg"
3 years ago
"Open_IM/pkg/utils"
4 years ago
"net"
"strconv"
"strings"
2 years ago
2 years ago
"github.com/golang/protobuf/proto"
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
2 years ago
"google.golang.org/grpc"
)
2 years ago
type MessageWriter interface {
SendMessage(m proto.Message, key string, operationID string) (int32, int64, error)
}
4 years ago
type rpcChat struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
2 years ago
messageWriter MessageWriter
//offlineProducer *kafka.Producer
2 years ago
delMsgCh chan deleteMsg
dMessageLocker MessageLocker
3 years ago
}
type deleteMsg struct {
UserID string
OpUserID string
SeqList []uint32
OperationID string
4 years ago
}
func NewRpcChatServer(port int) *rpcChat {
3 years ago
log.NewPrivateLog(constant.LogFileName)
4 years ago
rc := rpcChat{
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
}
4 years ago
func (rpc *rpcChat) Run() {
3 years ago
log.Info("", "rpcChat init...")
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(rpc.rpcPort)
4 years ago
listener, err := net.Listen("tcp", address)
if err != nil {
3 years ago
panic("listening err:" + err.Error() + rpc.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, rpc)
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
err = getcdv3.RegisterEtcd(rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), rpcRegisterIP, rpc.rpcPort, rpc.rpcRegisterName, 10)
4 years ago
if err != nil {
3 years ago
log.Error("", "register rpcChat to etcd failed ", err.Error())
panic(utils.Wrap(err, "register chat module rpc to etcd err"))
4 years ago
}
3 years ago
go rpc.runCh()
rpc.initPrometheus()
4 years ago
err = srv.Serve(listener)
if err != nil {
3 years ago
log.Error("", "rpc rpcChat failed ", err.Error())
4 years ago
return
}
3 years ago
log.Info("", "rpc 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 {
DeleteMessageNotification(msg.OpUserID, msg.UserID, unexistSeqList, msg.OperationID)
3 years ago
}
}
}
}