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

94 lines
2.8 KiB

2 years ago
package push
4 years ago
import (
2 years ago
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/cache"
"OpenIM/pkg/common/db/controller"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/prome"
pbPush "OpenIM/pkg/proto/push"
"OpenIM/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
2 years ago
pushInterface controller.PushInterface
pusher Pusher
4 years ago
}
2 years ago
func (r *RPCServer) Init(rpcPort int, cache cache.MsgCache) {
4 years ago
r.rpcPort = rpcPort
r.rpcRegisterName = config.Config.RpcRegisterName.OpenImPushName
}
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:
2 years ago
err = r.pusher.MsgToSuperGroupUser(ctx, pbData.SourceID, pbData.MsgData)
3 years ago
default:
2 years ago
err = r.pusher.MsgToUser(ctx, pbData.SourceID, pbData.MsgData)
3 years ago
}
2 years ago
return &pbPush.PushMsgResp{}, err
4 years ago
}
2 years ago
func (r *RPCServer) DelUserPushToken(ctx context.Context, req *pbPush.DelUserPushTokenReq) (resp *pbPush.DelUserPushTokenResp, err error) {
2 years ago
return &pbPush.DelUserPushTokenResp{}, r.pushInterface.DelFcmToken(ctx, req.UserID, int(req.PlatformID))
}