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

188 lines
6.6 KiB

3 years ago
package cache
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2 years ago
rocksCache "Open_IM/pkg/common/db/rocks_cache"
3 years ago
"Open_IM/pkg/common/log"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
2 years ago
"Open_IM/pkg/common/trace_log"
3 years ago
pbCache "Open_IM/pkg/proto/cache"
2 years ago
sdkws "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
"context"
2 years ago
"github.com/OpenIMSDK/getcdv3"
3 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"
3 years ago
)
type cacheServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
3 years ago
func NewCacheServer(port int) *cacheServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
return &cacheServer{
rpcPort: port,
3 years ago
rpcRegisterName: config.Config.RpcRegisterName.OpenImCacheName,
3 years ago
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
func (s *cacheServer) Run() {
log.NewInfo("0", "cacheServer rpc start ")
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(s.rpcPort)
3 years ago
//listener network
3 years ago
listener, err := net.Listen("tcp", address)
3 years ago
if err != nil {
3 years ago
panic("listening err:" + err.Error() + s.rpcRegisterName)
3 years ago
}
3 years ago
log.NewInfo("0", "listen network success, ", address, listener)
3 years ago
defer listener.Close()
//grpc server
2 years ago
var grpcOpts []grpc.ServerOption
if config.Config.Prometheus.Enable {
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
}
srv := grpc.NewServer(grpcOpts...)
3 years ago
defer srv.GracefulStop()
pbCache.RegisterCacheServer(srv, s)
3 years ago
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())
}
}
3 years ago
log.NewInfo("", "rpcRegisterIP", rpcRegisterIP)
2 years ago
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10, "")
3 years ago
if err != nil {
log.NewError("0", "RegisterEtcd failed ", err.Error())
panic(utils.Wrap(err, "register cache module rpc to etcd err"))
3 years ago
}
2 years ago
go rocksCache.DelKeys()
3 years ago
err = srv.Serve(listener)
if err != nil {
log.NewError("0", "Serve failed ", err.Error())
return
}
log.NewInfo("0", "message cms rpc success")
}
2 years ago
func (s *cacheServer) GetFriendIDListFromCache(ctx context.Context, req *pbCache.GetFriendIDListFromCacheReq) (resp *pbCache.GetFriendIDListFromCacheResp, err error) {
resp = &pbCache.GetFriendIDListFromCacheResp{CommonResp: &sdkws.CommonResp{}}
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), nil, "req", req.String(), "resp", resp.String())
trace_log.ShowLog(ctx)
}()
friendIDList, err := rocksCache.GetFriendIDListFromCache(ctx, req.UserID)
if err != nil {
2 years ago
constant.SetErrorForResp(err, resp.CommonResp)
return
}
resp.UserIDList = friendIDList
2 years ago
return
3 years ago
}
2 years ago
// this is for dtm call
2 years ago
func (s *cacheServer) DelFriendIDListFromCache(ctx context.Context, req *pbCache.DelFriendIDListFromCacheReq) (resp *pbCache.DelFriendIDListFromCacheResp, err error) {
resp = &pbCache.DelFriendIDListFromCacheResp{CommonResp: &sdkws.CommonResp{}}
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), nil, "req", req.String(), "resp", resp.String())
trace_log.ShowLog(ctx)
}()
if err := rocksCache.DelFriendIDListFromCache(ctx, req.UserID); err != nil {
constant.SetErrorForResp(err, resp.CommonResp)
return
}
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", resp.String())
2 years ago
return
3 years ago
}
2 years ago
func (s *cacheServer) GetBlackIDListFromCache(ctx context.Context, req *pbCache.GetBlackIDListFromCacheReq) (resp *pbCache.GetBlackIDListFromCacheResp, err error) {
resp = &pbCache.GetBlackIDListFromCacheResp{CommonResp: &sdkws.CommonResp{}}
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), nil, "req", req.String(), "resp", resp.String())
trace_log.ShowLog(ctx)
}()
blackUserIDList, err := rocksCache.GetBlackListFromCache(ctx, req.UserID)
if err != nil {
2 years ago
constant.SetErrorForResp(err, resp.CommonResp)
return
}
resp.UserIDList = blackUserIDList
2 years ago
return
3 years ago
}
2 years ago
func (s *cacheServer) DelBlackIDListFromCache(ctx context.Context, req *pbCache.DelBlackIDListFromCacheReq) (resp *pbCache.DelBlackIDListFromCacheResp, err error) {
resp = &pbCache.DelBlackIDListFromCacheResp{CommonResp: &sdkws.CommonResp{}}
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), nil, "req", req.String(), "resp", resp.String())
trace_log.ShowLog(ctx)
}()
if err := rocksCache.DelBlackIDListFromCache(ctx, req.UserID); err != nil {
constant.SetErrorForResp(err, resp.CommonResp)
return
}
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
3 years ago
2 years ago
func (s *cacheServer) GetGroupMemberIDListFromCache(ctx context.Context, req *pbCache.GetGroupMemberIDListFromCacheReq) (resp *pbCache.GetGroupMemberIDListFromCacheResp, err error) {
3 years ago
resp = &pbCache.GetGroupMemberIDListFromCacheResp{
2 years ago
CommonResp: &sdkws.CommonResp{},
3 years ago
}
2 years ago
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), nil, "req", req.String(), "resp", resp.String())
trace_log.ShowLog(ctx)
}()
userIDList, err := rocksCache.GetGroupMemberIDListFromCache(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
constant.SetErrorForResp(err, resp.CommonResp)
3 years ago
return resp, nil
}
resp.UserIDList = userIDList
return resp, nil
}
2 years ago
func (s *cacheServer) DelGroupMemberIDListFromCache(ctx context.Context, req *pbCache.DelGroupMemberIDListFromCacheReq) (resp *pbCache.DelGroupMemberIDListFromCacheResp, err error) {
resp = &pbCache.DelGroupMemberIDListFromCacheResp{CommonResp: &sdkws.CommonResp{}}
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), nil, "req", req.String(), "resp", resp.String())
trace_log.ShowLog(ctx)
}()
if err := rocksCache.DelGroupMemberIDListFromCache(ctx, req.GroupID); err != nil {
constant.SetErrorForResp(err, resp.CommonResp)
3 years ago
return resp, nil
}
return resp, nil
}