|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
"Open_IM/pkg/common/config"
|
|
|
|
"Open_IM/pkg/common/config"
|
|
|
|
"Open_IM/pkg/common/constant"
|
|
|
|
"Open_IM/pkg/common/constant"
|
|
|
|
"Open_IM/pkg/common/db"
|
|
|
|
"Open_IM/pkg/common/db"
|
|
|
|
|
|
|
|
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
|
|
|
"Open_IM/pkg/common/log"
|
|
|
|
"Open_IM/pkg/common/log"
|
|
|
|
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
|
|
|
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
|
|
|
pbCache "Open_IM/pkg/proto/cache"
|
|
|
|
pbCache "Open_IM/pkg/proto/cache"
|
|
|
@ -46,6 +47,14 @@ func (s *cacheServer) Run() {
|
|
|
|
log.NewInfo("0", "listen network success, ", registerAddress, listener)
|
|
|
|
log.NewInfo("0", "listen network success, ", registerAddress, listener)
|
|
|
|
defer listener.Close()
|
|
|
|
defer listener.Close()
|
|
|
|
//grpc server
|
|
|
|
//grpc server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//to cache
|
|
|
|
|
|
|
|
err = SyncDB2Cache()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.NewError("", err.Error(), "db to cache failed")
|
|
|
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
srv := grpc.NewServer()
|
|
|
|
srv := grpc.NewServer()
|
|
|
|
defer srv.GracefulStop()
|
|
|
|
defer srv.GracefulStop()
|
|
|
|
pbCache.RegisterCacheServer(srv, s)
|
|
|
|
pbCache.RegisterCacheServer(srv, s)
|
|
|
@ -62,14 +71,84 @@ func (s *cacheServer) Run() {
|
|
|
|
log.NewInfo("0", "message cms rpc success")
|
|
|
|
log.NewInfo("0", "message cms rpc success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) GetUserInfo(_ context.Context, req *pbCache.GetUserInfoReq) (resp *pbCache.GetUserInfoResp, err error) {
|
|
|
|
func SyncDB2Cache() error {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
log.NewInfo("0", utils.GetSelfFuncName())
|
|
|
|
|
|
|
|
userList, err := imdb.GetAllUser()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return utils.Wrap(err, "")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//err = updateAllUserToCache(userList)
|
|
|
|
|
|
|
|
err = updateAllFriendToCache(userList)
|
|
|
|
|
|
|
|
err = updateAllBlackListToCache(userList)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func updateAllUserToCache(userList []db.User) error {
|
|
|
|
|
|
|
|
for _, userInfo := range userList {
|
|
|
|
|
|
|
|
userInfoPb := &commonPb.UserInfo{
|
|
|
|
|
|
|
|
UserID: userInfo.UserID,
|
|
|
|
|
|
|
|
Nickname: userInfo.Nickname,
|
|
|
|
|
|
|
|
FaceURL: userInfo.FaceURL,
|
|
|
|
|
|
|
|
Gender: userInfo.Gender,
|
|
|
|
|
|
|
|
PhoneNumber: userInfo.PhoneNumber,
|
|
|
|
|
|
|
|
Birth: uint32(userInfo.Birth.Unix()),
|
|
|
|
|
|
|
|
Email: userInfo.Email,
|
|
|
|
|
|
|
|
Ex: userInfo.Ex,
|
|
|
|
|
|
|
|
CreateTime: uint32(userInfo.CreateTime.Unix()),
|
|
|
|
|
|
|
|
AppMangerLevel: userInfo.AppMangerLevel,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := utils.Pb2Map(userInfoPb)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.NewError("", utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.DB.SetUserInfoToCache(userInfo.UserID, m); err != nil {
|
|
|
|
|
|
|
|
log.NewError("0", utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo("0", utils.GetSelfFuncName(), "ok")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func updateAllFriendToCache(userList []db.User) error {
|
|
|
|
|
|
|
|
log.NewInfo("0", utils.GetSelfFuncName())
|
|
|
|
|
|
|
|
for _, user := range userList {
|
|
|
|
|
|
|
|
friendIDList, err := imdb.GetFriendIDListByUserID(user.UserID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.NewError("0", utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.DB.AddFriendToCache(user.UserID, friendIDList...); err != nil {
|
|
|
|
|
|
|
|
log.NewError("0", utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo("0", utils.GetSelfFuncName(), "ok")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func updateAllBlackListToCache(userList []db.User) error {
|
|
|
|
|
|
|
|
log.NewInfo("0", utils.GetSelfFuncName())
|
|
|
|
|
|
|
|
for _, user := range userList {
|
|
|
|
|
|
|
|
blackIDList, err := imdb.GetBlackIDListByUserID(user.UserID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.NewError("", utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.DB.AddBlackUserToCache(user.UserID, blackIDList...); err != nil {
|
|
|
|
|
|
|
|
log.NewError("0", utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo("0", utils.GetSelfFuncName(), "ok")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) GetUserInfoFromCache(_ context.Context, req *pbCache.GetUserInfoFromCacheReq) (resp *pbCache.GetUserInfoFromCacheResp, err error) {
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
resp = &pbCache.GetUserInfoResp{
|
|
|
|
resp = &pbCache.GetUserInfoFromCacheResp{
|
|
|
|
UserInfoList: []*commonPb.UserInfo{},
|
|
|
|
CommonResp: &pbCache.CommonResp{},
|
|
|
|
CommonResp: &pbCache.CommonResp{},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, userID := range req.UserIDList {
|
|
|
|
for _, userID := range req.UserIDList {
|
|
|
|
userInfo, err := db.DB.GetUserInfo(userID)
|
|
|
|
userInfo, err := db.DB.GetUserInfoFromCache(userID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), "get userInfo from cache failed", err.Error())
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), "get userInfo from cache failed", err.Error())
|
|
|
|
continue
|
|
|
|
continue
|
|
|
@ -80,39 +159,104 @@ func (s *cacheServer) GetUserInfo(_ context.Context, req *pbCache.GetUserInfoReq
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) UpdateUserInfo(_ context.Context, req *pbCache.UpdateUserInfoReq) (resp *pbCache.UpdateUserInfoResp, err error) {
|
|
|
|
func (s *cacheServer) UpdateUserInfoToCache(_ context.Context, req *pbCache.UpdateUserInfoToCacheReq) (resp *pbCache.UpdateUserInfoToCacheResp, err error) {
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
resp = &pbCache.UpdateUserInfoResp{
|
|
|
|
resp = &pbCache.UpdateUserInfoToCacheResp{
|
|
|
|
CommonResp: &pbCache.CommonResp{},
|
|
|
|
CommonResp: &pbCache.CommonResp{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, userInfo := range req.UserInfoList {
|
|
|
|
for _, userInfo := range req.UserInfoList {
|
|
|
|
if err := db.DB.SetUserInfo(userInfo); err != nil {
|
|
|
|
m, err := utils.Pb2Map(userInfo)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), *userInfo)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.DB.SetUserInfoToCache(userInfo.UserID, m); err != nil {
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error())
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error())
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) GetBlackList(_ context.Context, req *pbCache.GetBlackListReq) (resp *pbCache.GetBlackListResp, err error) {
|
|
|
|
func (s *cacheServer) GetFriendIDListFromCache(_ context.Context, req *pbCache.GetFriendIDListFromCacheReq) (resp *pbCache.GetFriendIDListFromCacheResp, err error) {
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
|
|
|
|
resp = &pbCache.GetFriendIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
|
|
|
|
|
|
|
|
friendIDList, err := db.DB.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
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), friendIDList)
|
|
|
|
|
|
|
|
resp.UserIDList = friendIDList
|
|
|
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) AddFriendToCache(_ context.Context, req *pbCache.AddFriendToCacheReq) (resp *pbCache.AddFriendToCacheResp, err error) {
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
return nil, nil
|
|
|
|
resp = &pbCache.AddFriendToCacheResp{CommonResp: &pbCache.CommonResp{}}
|
|
|
|
|
|
|
|
if err := db.DB.AddFriendToCache(req.UserID, req.FriendID); 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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) UpdateBlackList(_ context.Context, req *pbCache.UpdateBlackListReq) (resp *pbCache.UpdateBlackListResp, err error) {
|
|
|
|
func (s *cacheServer) ReduceFriendFromCache(_ context.Context, req *pbCache.ReduceFriendFromCacheReq) (resp *pbCache.ReduceFriendFromCacheResp, err error) {
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
|
|
|
|
resp = &pbCache.ReduceFriendFromCacheResp{CommonResp: &pbCache.CommonResp{}}
|
|
|
|
|
|
|
|
if err := db.DB.ReduceFriendToCache(req.UserID, req.FriendID); 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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) GetBlackIDListFromCache(_ context.Context, req *pbCache.GetBlackIDListFromCacheReq) (resp *pbCache.GetBlackIDListFromCacheResp, err error) {
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
return nil, nil
|
|
|
|
resp = &pbCache.GetBlackIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
|
|
|
|
|
|
|
|
blackUserIDList, err := db.DB.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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), blackUserIDList)
|
|
|
|
|
|
|
|
resp.UserIDList = blackUserIDList
|
|
|
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) GetFriendInfo(_ context.Context, req *pbCache.GetFriendInfoReq) (resp *pbCache.GetFriendInfoResp, err error) {
|
|
|
|
func (s *cacheServer) AddBlackUserToCache(_ context.Context, req *pbCache.AddBlackUserToCacheReq) (resp *pbCache.AddBlackUserToCacheResp, err error) {
|
|
|
|
return nil, nil
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
|
|
|
|
resp = &pbCache.AddBlackUserToCacheResp{CommonResp: &pbCache.CommonResp{}}
|
|
|
|
|
|
|
|
if err := db.DB.AddBlackUserToCache(req.UserID, req.BlackUserID); err != nil {
|
|
|
|
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
|
|
|
|
|
|
|
|
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cacheServer) UpdateFriendInfo(_ context.Context, req *pbCache.UpdateFriendInfoReq) (resp *pbCache.UpdateFriendInfoResp, err error) {
|
|
|
|
func (s *cacheServer) ReduceBlackUserFromCache(_ context.Context, req *pbCache.ReduceBlackUserFromCacheReq) (resp *pbCache.ReduceBlackUserFromCacheResp, err error) {
|
|
|
|
return nil, nil
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
|
|
|
|
|
|
|
resp = &pbCache.ReduceBlackUserFromCacheResp{CommonResp: &pbCache.CommonResp{}}
|
|
|
|
|
|
|
|
if err := db.DB.ReduceBlackUserFromCache(req.UserID, req.BlackUserID); err != nil {
|
|
|
|
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
|
|
|
|
|
|
|
|
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
|
|
|
|
|
|
|
|
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|