mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-05 09:36:00 +08:00
proto modify
This commit is contained in:
@@ -19,18 +19,18 @@ import (
|
||||
)
|
||||
|
||||
type Push struct {
|
||||
rpcServer RPCServer
|
||||
rpcServer PushServer
|
||||
pushCh ConsumerHandler
|
||||
offlinePusher OfflinePusher
|
||||
successCount uint64
|
||||
}
|
||||
|
||||
func (p *Push) Init(rpcPort int) error {
|
||||
redisClient, err := cache.NewRedis()
|
||||
rdb, err := cache.NewRedis()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var cacheInterface cache.Cache = redisClient
|
||||
var cacheInterface cache.Cache
|
||||
p.rpcServer.Init(rpcPort, cacheInterface)
|
||||
p.pushCh.Init()
|
||||
statistics.NewStatistics(&p.successCount, config.Config.ModuleName.PushName, fmt.Sprintf("%d second push to msg_gateway count", constant.StatisticsTimeInterval), constant.StatisticsTimeInterval)
|
||||
|
||||
@@ -4,90 +4,39 @@ import (
|
||||
"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"
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
||||
"github.com/OpenIMSDK/openKeeper"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type RPCServer struct {
|
||||
rpcPort int
|
||||
rpcRegisterName string
|
||||
PushDatabase controller.PushDatabase
|
||||
pusher Pusher
|
||||
type pushServer struct {
|
||||
pusher *Pusher
|
||||
}
|
||||
|
||||
func (r *RPCServer) Init(rpcPort int, cache cache.Cache) {
|
||||
r.rpcPort = rpcPort
|
||||
r.rpcRegisterName = config.Config.RpcRegisterName.OpenImPushName
|
||||
func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
|
||||
|
||||
pbPush.RegisterPushMsgServiceServer(server, &pushServer{
|
||||
pusher: NewPusher(),
|
||||
})
|
||||
}
|
||||
|
||||
func (r *RPCServer) run() {
|
||||
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)
|
||||
if err != nil {
|
||||
panic("listening err:" + err.Error() + r.rpcRegisterName)
|
||||
}
|
||||
defer listener.Close()
|
||||
var grpcOpts []grpc.ServerOption
|
||||
if config.Config.Prometheus.Enable {
|
||||
prome.NewGrpcRequestCounter()
|
||||
prome.NewGrpcRequestFailedCounter()
|
||||
prome.NewGrpcRequestSuccessCounter()
|
||||
grpcOpts = append(grpcOpts, []grpc.ServerOption{
|
||||
// grpc.UnaryInterceptor(prome.UnaryServerInterceptorProme),
|
||||
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
|
||||
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
|
||||
}...)
|
||||
}
|
||||
srv := grpc.NewServer(grpcOpts...)
|
||||
defer srv.GracefulStop()
|
||||
pbPush.RegisterPushMsgServiceServer(srv, r)
|
||||
rpcRegisterIP := config.Config.RpcRegisterIP
|
||||
if config.Config.RpcRegisterIP == "" {
|
||||
rpcRegisterIP, err = utils.GetLocalIP()
|
||||
if err != nil {
|
||||
log.Error("", "GetLocalIP failed ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
err = rpc.RegisterEtcd(r.etcdSchema, strings.Join(r.etcdAddr, ","), rpcRegisterIP, r.rpcPort, r.rpcRegisterName, 10)
|
||||
if err != nil {
|
||||
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"))
|
||||
}
|
||||
err = srv.Serve(listener)
|
||||
if err != nil {
|
||||
log.Error("", "push module rpc start err", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RPCServer) PushMsg(ctx context.Context, pbData *pbPush.PushMsgReq) (resp *pbPush.PushMsgResp, err error) {
|
||||
func (r *pushServer) PushMsg(ctx context.Context, pbData *pbPush.PushMsgReq) (resp *pbPush.PushMsgResp, err error) {
|
||||
switch pbData.MsgData.SessionType {
|
||||
case constant.SuperGroupChatType:
|
||||
err = r.pusher.MsgToSuperGroupUser(ctx, pbData.SourceID, pbData.MsgData)
|
||||
default:
|
||||
err = r.pusher.MsgToUser(ctx, pbData.SourceID, pbData.MsgData)
|
||||
}
|
||||
return &pbPush.PushMsgResp{}, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbPush.PushMsgResp{}, nil
|
||||
}
|
||||
|
||||
func (r *RPCServer) DelUserPushToken(ctx context.Context, req *pbPush.DelUserPushTokenReq) (resp *pbPush.DelUserPushTokenResp, err error) {
|
||||
return &pbPush.DelUserPushTokenResp{}, r.PushDatabase.DelFcmToken(ctx, req.UserID, int(req.PlatformID))
|
||||
func (r *pushServer) DelUserPushToken(ctx context.Context, req *pbPush.DelUserPushTokenReq) (resp *pbPush.DelUserPushTokenResp, err error) {
|
||||
if err = r.pusher.database.DelFcmToken(ctx, req.UserID, int(req.PlatformID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbPush.DelUserPushTokenResp{}, nil
|
||||
}
|
||||
|
||||
@@ -9,14 +9,13 @@ package push
|
||||
import (
|
||||
"OpenIM/pkg/common/config"
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/db/cache"
|
||||
"OpenIM/pkg/common/db/controller"
|
||||
"OpenIM/pkg/common/db/localcache"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/prome"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/discoveryregistry"
|
||||
msggateway "OpenIM/pkg/proto/msggateway"
|
||||
pbRtc "OpenIM/pkg/proto/rtc"
|
||||
"OpenIM/pkg/proto/msggateway"
|
||||
"OpenIM/pkg/proto/sdkws"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
@@ -25,7 +24,7 @@ import (
|
||||
)
|
||||
|
||||
type Pusher struct {
|
||||
cache cache.Cache
|
||||
database controller.PushDatabase
|
||||
client discoveryregistry.SvcDiscoveryRegistry
|
||||
offlinePusher OfflinePusher
|
||||
groupLocalCache localcache.GroupLocalCache
|
||||
@@ -33,11 +32,14 @@ type Pusher struct {
|
||||
successCount int
|
||||
}
|
||||
|
||||
func NewPusher(cache cache.Cache, client discoveryregistry.SvcDiscoveryRegistry, offlinePusher OfflinePusher) *Pusher {
|
||||
func NewPusher(client discoveryregistry.SvcDiscoveryRegistry, offlinePusher OfflinePusher, database controller.PushDatabase,
|
||||
groupLocalCache localcache.GroupLocalCache, conversationLocalCache localcache.ConversationLocalCache) *Pusher {
|
||||
return &Pusher{
|
||||
cache: cache,
|
||||
client: client,
|
||||
offlinePusher: offlinePusher,
|
||||
database: database,
|
||||
client: client,
|
||||
offlinePusher: offlinePusher,
|
||||
groupLocalCache: groupLocalCache,
|
||||
conversationLocalCache: conversationLocalCache,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +48,7 @@ func (p *Pusher) MsgToUser(ctx context.Context, userID string, msg *sdkws.MsgDat
|
||||
var userIDs = []string{userID}
|
||||
log.Debug(operationID, "Get msg from msg_transfer And push msg", msg.String(), userID)
|
||||
// callback
|
||||
if err := callbackOnlinePush(ctx, userIDs, msg); err != nil {
|
||||
if err := callbackOnlinePush(ctx, userIDs, msg); err != nil && err != constant.ErrCallbackContinue {
|
||||
return err
|
||||
}
|
||||
// push
|
||||
@@ -65,7 +67,7 @@ func (p *Pusher) MsgToUser(ctx context.Context, userID string, msg *sdkws.MsgDat
|
||||
}
|
||||
}
|
||||
if msg.ContentType == constant.SignalingNotification {
|
||||
isSend, err := p.cache.HandleSignalInfo(ctx, msg, userID)
|
||||
isSend, err := p.database.HandleSignalInvite(ctx, msg, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -77,7 +79,7 @@ func (p *Pusher) MsgToUser(ctx context.Context, userID string, msg *sdkws.MsgDat
|
||||
if err := callbackOfflinePush(ctx, userIDs, msg, &[]string{}); err != nil {
|
||||
return err
|
||||
}
|
||||
err = p.OfflinePushMsg(ctx, userID, msg, userIDs)
|
||||
err = p.offlinePushMsg(ctx, userID, msg, userIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -89,7 +91,7 @@ func (p *Pusher) MsgToSuperGroupUser(ctx context.Context, groupID string, msg *s
|
||||
operationID := tracelog.GetOperationID(ctx)
|
||||
log.Debug(operationID, "Get super group msg from msg_transfer And push msg", msg.String(), groupID)
|
||||
var pushToUserIDs []string
|
||||
if err := callbackBeforeSuperGroupOnlinePush(ctx, groupID, msg, &pushToUserIDs); err != nil {
|
||||
if err := callbackBeforeSuperGroupOnlinePush(ctx, groupID, msg, &pushToUserIDs); err != nil && err != constant.ErrCallbackContinue {
|
||||
return err
|
||||
}
|
||||
if len(pushToUserIDs) == 0 {
|
||||
@@ -145,14 +147,14 @@ func (p *Pusher) MsgToSuperGroupUser(ctx context.Context, groupID string, msg *s
|
||||
if len(offlinePushUserIDs) > 0 {
|
||||
needOfflinePushUserIDs = offlinePushUserIDs
|
||||
}
|
||||
err = p.OfflinePushMsg(ctx, groupID, msg, offlinePushUserIDs)
|
||||
err = p.offlinePushMsg(ctx, groupID, msg, offlinePushUserIDs)
|
||||
if err != nil {
|
||||
log.NewError(operationID, "OfflinePushMsg failed", groupID)
|
||||
log.NewError(operationID, "offlinePushMsg failed", groupID)
|
||||
return err
|
||||
}
|
||||
_, err := p.GetConnsAndOnlinePush(ctx, msg, utils.IntersectString(needOfflinePushUserIDs, WebAndPcBackgroundUserIDs))
|
||||
if err != nil {
|
||||
log.NewError(operationID, "OfflinePushMsg failed", groupID)
|
||||
log.NewError(operationID, "offlinePushMsg failed", groupID)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -160,14 +162,14 @@ func (p *Pusher) MsgToSuperGroupUser(ctx context.Context, groupID string, msg *s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Pusher) GetConnsAndOnlinePush(ctx context.Context, msg *sdkws.MsgData, pushToUserIDs []string) (wsResults []*msggateway.SingelMsgToUserResultList, err error) {
|
||||
func (p *Pusher) GetConnsAndOnlinePush(ctx context.Context, msg *sdkws.MsgData, pushToUserIDs []string) (wsResults []*msggateway.SingleMsgToUserResultList, err error) {
|
||||
conns, err := p.client.GetConns(config.Config.RpcRegisterName.OpenImMessageGatewayName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//Online push message
|
||||
for _, v := range conns {
|
||||
msgClient := msggateway.NewRelayClient(v)
|
||||
msgClient := msggateway.NewMsgGatewayClient(v)
|
||||
reply, err := msgClient.SuperGroupOnlineBatchPushOneMsg(ctx, &msggateway.OnlineBatchPushOneMsgReq{OperationID: tracelog.GetOperationID(ctx), MsgData: msg, PushToUserIDList: pushToUserIDs})
|
||||
if err != nil {
|
||||
log.NewError(tracelog.GetOperationID(ctx), msg, len(pushToUserIDs), "err", err)
|
||||
@@ -180,8 +182,8 @@ func (p *Pusher) GetConnsAndOnlinePush(ctx context.Context, msg *sdkws.MsgData,
|
||||
return wsResults, nil
|
||||
}
|
||||
|
||||
func (p *Pusher) OfflinePushMsg(ctx context.Context, sourceID string, msg *sdkws.MsgData, offlinePushUserIDs []string) error {
|
||||
title, content, opts, err := p.GetOfflinePushInfos(sourceID, msg)
|
||||
func (p *Pusher) offlinePushMsg(ctx context.Context, sourceID string, msg *sdkws.MsgData, offlinePushUserIDs []string) error {
|
||||
title, content, opts, err := p.getOfflinePushInfos(sourceID, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -197,12 +199,12 @@ func (p *Pusher) OfflinePushMsg(ctx context.Context, sourceID string, msg *sdkws
|
||||
func (p *Pusher) GetOfflinePushOpts(msg *sdkws.MsgData) (opts *Opts, err error) {
|
||||
opts = &Opts{}
|
||||
if msg.ContentType > constant.SignalingNotificationBegin && msg.ContentType < constant.SignalingNotificationEnd {
|
||||
req := &pbRtc.SignalReq{}
|
||||
req := &sdkws.SignalReq{}
|
||||
if err := proto.Unmarshal(msg.Content, req); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
switch req.Payload.(type) {
|
||||
case *pbRtc.SignalReq_Invite, *pbRtc.SignalReq_InviteInGroup:
|
||||
case *sdkws.SignalReq_Invite, *sdkws.SignalReq_InviteInGroup:
|
||||
opts.Signal = &Signal{ClientMsgID: msg.ClientMsgID}
|
||||
}
|
||||
}
|
||||
@@ -214,7 +216,7 @@ func (p *Pusher) GetOfflinePushOpts(msg *sdkws.MsgData) (opts *Opts, err error)
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
func (p *Pusher) GetOfflinePushInfos(sourceID string, msg *sdkws.MsgData) (title, content string, opts *Opts, err error) {
|
||||
func (p *Pusher) getOfflinePushInfos(sourceID string, msg *sdkws.MsgData) (title, content string, opts *Opts, err error) {
|
||||
if p.offlinePusher == nil {
|
||||
err = errors.New("no offlinePusher is configured")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user