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_rpc_server.go

97 lines
2.8 KiB

2 years ago
package push
4 years ago
import (
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db/cache"
"Open_IM/pkg/common/db/controller"
"Open_IM/pkg/common/log"
2 years ago
"Open_IM/pkg/common/prome"
2 years ago
pbPush "Open_IM/pkg/proto/push"
"Open_IM/pkg/utils"
4 years ago
"context"
"net"
3 years ago
"strconv"
4 years ago
"strings"
2 years ago
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
2 years ago
"google.golang.org/grpc"
4 years ago
)
type RPCServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
2 years ago
push controller.PushInterface
4 years ago
}
2 years ago
func (r *RPCServer) Init(rpcPort int, cache cache.Cache) {
4 years ago
r.rpcPort = rpcPort
r.rpcRegisterName = config.Config.RpcRegisterName.OpenImPushName
r.etcdSchema = config.Config.Etcd.EtcdSchema
r.etcdAddr = config.Config.Etcd.EtcdAddr
}
2 years ago
4 years ago
func (r *RPCServer) run() {
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(r.rpcPort)
listener, err := net.Listen("tcp", address)
4 years ago
if err != nil {
3 years ago
panic("listening err:" + err.Error() + r.rpcRegisterName)
4 years ago
}
defer listener.Close()
2 years ago
var grpcOpts []grpc.ServerOption
if config.Config.Prometheus.Enable {
2 years ago
prome.NewGrpcRequestCounter()
prome.NewGrpcRequestFailedCounter()
prome.NewGrpcRequestSuccessCounter()
2 years ago
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(prome.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()
pbPush.RegisterPushMsgServiceServer(srv, r)
2 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
2 years ago
err = rpc.RegisterEtcd(r.etcdSchema, strings.Join(r.etcdAddr, ","), rpcRegisterIP, r.rpcPort, r.rpcRegisterName, 10)
4 years ago
if err != nil {
3 years ago
log.Error("", "register push module rpc to etcd err", err.Error(), r.etcdSchema, strings.Join(r.etcdAddr, ","), rpcRegisterIP, r.rpcPort, r.rpcRegisterName)
panic(utils.Wrap(err, "register push module rpc to etcd err"))
4 years ago
}
err = srv.Serve(listener)
if err != nil {
3 years ago
log.Error("", "push module rpc start err", err.Error())
4 years ago
return
}
}
2 years ago
func (r *RPCServer) PushMsg(ctx context.Context, pbData *pbPush.PushMsgReq) (resp *pbPush.PushMsgResp, err error) {
3 years ago
switch pbData.MsgData.SessionType {
case constant.SuperGroupChatType:
MsgToSuperGroupUser(pbData)
default:
MsgToUser(pbData)
}
2 years ago
return &pbPush.PushMsgResp{}, nil
4 years ago
}
2 years ago
func (r *RPCServer) DelUserPushToken(ctx context.Context, req *pbPush.DelUserPushTokenReq) (resp *pbPush.DelUserPushTokenResp, err error) {
return &pbPush.DelUserPushTokenResp{}, r.push.DelFcmToken(ctx, req.UserID, int(req.PlatformID))
}