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

147 lines
4.7 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"
3 years ago
pbCache "Open_IM/pkg/proto/cache"
"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) {
2 years ago
resp = &pbCache.GetFriendIDListFromCacheResp{}
2 years ago
friendIDList, err := rocksCache.GetFriendIDListFromCache(ctx, req.UserID)
if err != nil {
2 years ago
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) {
2 years ago
resp = &pbCache.DelFriendIDListFromCacheResp{}
2 years ago
if err := rocksCache.DelFriendIDListFromCache(ctx, req.UserID); err != nil {
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) {
2 years ago
resp = &pbCache.GetBlackIDListFromCacheResp{}
2 years ago
blackUserIDList, err := rocksCache.GetBlackListFromCache(ctx, req.UserID)
if err != nil {
2 years ago
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) {
2 years ago
resp = &pbCache.DelBlackIDListFromCacheResp{}
2 years ago
if err := rocksCache.DelBlackIDListFromCache(ctx, req.UserID); err != nil {
return
}
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) {
2 years ago
resp = &pbCache.GetGroupMemberIDListFromCacheResp{}
2 years ago
userIDList, err := rocksCache.GetGroupMemberIDListFromCache(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return
3 years ago
}
resp.UserIDList = userIDList
2 years ago
return
3 years ago
}
2 years ago
func (s *cacheServer) DelGroupMemberIDListFromCache(ctx context.Context, req *pbCache.DelGroupMemberIDListFromCacheReq) (resp *pbCache.DelGroupMemberIDListFromCacheResp, err error) {
2 years ago
resp = &pbCache.DelGroupMemberIDListFromCacheResp{}
2 years ago
if err := rocksCache.DelGroupMemberIDListFromCache(ctx, req.GroupID); err != nil {
3 years ago
return resp, nil
}
return resp, nil
}