From f592a3abd751ca5be42f395f5f61f1a6c2ecb8e8 Mon Sep 17 00:00:00 2001 From: pluto <83957921+plutoyty@users.noreply.github.com> Date: Thu, 3 Aug 2023 09:44:10 +0800 Subject: [PATCH] Fix bug 755 (#758) * Resolving code conflicts after project directory changes and Add subscribe and unsubscribe mongodb operations * Organize and update module dependencies * Get user online status * Get user online status * Get user online status * fix-bug-755 * Modify statusmod to a prime number to reduce hash conflicts --- pkg/common/db/cache/user.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index 5c76af22f..65afb400d 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -18,6 +18,8 @@ import ( "context" "encoding/json" "github.com/OpenIMSDK/protocol/user" + "github.com/OpenIMSDK/tools/errs" + "hash/crc32" "strconv" "time" @@ -33,7 +35,7 @@ const ( userGlobalRecvMsgOptKey = "USER_GLOBAL_RECV_MSG_OPT_KEY:" olineStatusKey = "ONLINE_STATUS:" userOlineStatusExpireTime = time.Second * 60 * 60 * 24 - statusMod = 500 + statusMod = 501 ) type UserCache interface { @@ -165,11 +167,8 @@ func (u *UserCacheRedis) getOnlineStatusKey(userID string) string { func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) { var res []*user.OnlineStatus for _, userID := range userIDs { - UserIDNum, err := strconv.Atoi(userID) - if err != nil { - return nil, err - } - var modKey = strconv.Itoa(UserIDNum % statusMod) + UserIDNum := crc32.ChecksumIEEE([]byte(userID)) + var modKey = strconv.Itoa(int(UserIDNum % statusMod)) var onlineStatus user.OnlineStatus key := olineStatusKey + modKey result, err := u.rdb.HGet(ctx, key, userID).Result() @@ -183,12 +182,12 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([ }) continue } else { - return nil, err + return nil, errs.Wrap(err) } } err = json.Unmarshal([]byte(result), &onlineStatus) if err != nil { - return nil, err + return nil, errs.Wrap(err) } onlineStatus.UserID = userID res = append(res, &onlineStatus) @@ -200,23 +199,20 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, list []*user.OnlineStatus) error { for _, status := range list { var isNewKey int64 - UserIDNum, err := strconv.Atoi(status.UserID) - if err != nil { - return err - } - var modKey = strconv.Itoa(UserIDNum % statusMod) + UserIDNum := crc32.ChecksumIEEE([]byte(status.UserID)) + var modKey = strconv.Itoa(int(UserIDNum % statusMod)) key := olineStatusKey + modKey jsonData, err := json.Marshal(status) if err != nil { - return err + return errs.Wrap(err) } isNewKey, err = u.rdb.Exists(ctx, key).Result() if err != nil { - return err + return errs.Wrap(err) } _, err = u.rdb.HSet(ctx, key, status.UserID, string(jsonData)).Result() if err != nil { - return err + return errs.Wrap(err) } if isNewKey > 0 { u.rdb.Expire(ctx, key, userOlineStatusExpireTime)