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

166 lines
6.4 KiB

3 years ago
package cache
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db/rocks_cache"
3 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbCache "Open_IM/pkg/proto/cache"
"Open_IM/pkg/utils"
"context"
2 years ago
"google.golang.org/grpc"
3 years ago
"net"
"strconv"
"strings"
)
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
3 years ago
srv := grpc.NewServer()
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)
3 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())
return
}
err = srv.Serve(listener)
if err != nil {
log.NewError("0", "Serve failed ", err.Error())
return
}
log.NewInfo("0", "message cms rpc success")
}
func (s *cacheServer) GetFriendIDListFromCache(_ context.Context, req *pbCache.GetFriendIDListFromCacheReq) (resp *pbCache.GetFriendIDListFromCacheResp, err error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.GetFriendIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
2 years ago
friendIDList, err := rocksCache.GetFriendIDListFromCache(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetFriendIDListFromCache", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
3 years ago
return resp, nil
}
3 years ago
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), friendIDList)
resp.UserIDList = friendIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
2 years ago
// this is for dtm call
func (s *cacheServer) DelFriendIDListFromCache(_ context.Context, req *pbCache.DelFriendIDListFromCacheReq) (resp *pbCache.DelFriendIDListFromCacheResp, err error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.DelFriendIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
2 years ago
if err := rocksCache.DelFriendIDListFromCache(req.UserID); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "args: ", req.UserID, err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
2 years ago
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
func (s *cacheServer) GetBlackIDListFromCache(_ context.Context, req *pbCache.GetBlackIDListFromCacheReq) (resp *pbCache.GetBlackIDListFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.GetBlackIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
2 years ago
blackUserIDList, err := rocksCache.GetBlackListFromCache(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "AddFriendToCache failed", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
3 years ago
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), blackUserIDList)
resp.UserIDList = blackUserIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *cacheServer) DelBlackIDListFromCache(_ context.Context, req *pbCache.DelBlackIDListFromCacheReq) (resp *pbCache.DelBlackIDListFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.DelBlackIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
2 years ago
if err := rocksCache.DelBlackIDListFromCache(req.UserID); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "args: ", req.UserID, err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
2 years ago
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
3 years ago
func (s *cacheServer) GetGroupMemberIDListFromCache(_ context.Context, req *pbCache.GetGroupMemberIDListFromCacheReq) (resp *pbCache.GetGroupMemberIDListFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.GetGroupMemberIDListFromCacheResp{
CommonResp: &pbCache.CommonResp{},
}
2 years ago
userIDList, err := rocksCache.GetGroupMemberIDListFromCache(req.GroupID)
3 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMemberIDListFromCache failed", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
resp.UserIDList = userIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
2 years ago
func (s *cacheServer) DelGroupMemberIDListFromCache(_ context.Context, req *pbCache.DelGroupMemberIDListFromCacheReq) (resp *pbCache.DelGroupMemberIDListFromCacheResp, err error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.DelGroupMemberIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
2 years ago
if err := rocksCache.DelGroupMemberIDListFromCache(req.GroupID); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "args: ", req.GroupID, err.Error())
3 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
2 years ago
resp.CommonResp.ErrMsg = err.Error()
3 years ago
return resp, nil
}
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", resp.String())
3 years ago
return resp, nil
}