mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-15 06:25:58 +08:00
refactor: Refactor rpc call && auto gen rpc_call code (#2969)
* refactor: rpcclient * chore: err * fix: err * fix: err * fix: err * feat: change api
This commit is contained in:
+24
-10
@@ -3,18 +3,19 @@ package rpccache
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/openimsdk/protocol/constant"
|
||||
"github.com/openimsdk/protocol/user"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/openimsdk/protocol/constant"
|
||||
"github.com/openimsdk/protocol/rpccall"
|
||||
"github.com/openimsdk/protocol/user"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/localcache/lru"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/util/useronline"
|
||||
"github.com/openimsdk/tools/db/cacheutil"
|
||||
"github.com/openimsdk/tools/log"
|
||||
@@ -22,10 +23,10 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func NewOnlineCache(user rpcclient.UserRpcClient, group *GroupLocalCache, rdb redis.UniversalClient, fullUserCache bool, fn func(ctx context.Context, userID string, platformIDs []int32)) (*OnlineCache, error) {
|
||||
func NewOnlineCache(adminUserID []string, group *GroupLocalCache, rdb redis.UniversalClient, fullUserCache bool, fn func(ctx context.Context, userID string, platformIDs []int32)) (*OnlineCache, error) {
|
||||
l := &sync.Mutex{}
|
||||
x := &OnlineCache{
|
||||
user: user,
|
||||
adminUserID: adminUserID,
|
||||
group: group,
|
||||
fullUserCache: fullUserCache,
|
||||
Lock: l,
|
||||
@@ -65,8 +66,8 @@ const (
|
||||
)
|
||||
|
||||
type OnlineCache struct {
|
||||
user rpcclient.UserRpcClient
|
||||
group *GroupLocalCache
|
||||
adminUserID []string
|
||||
group *GroupLocalCache
|
||||
|
||||
// fullUserCache if enabled, caches the online status of all users using mapCache;
|
||||
// otherwise, only a portion of users' online statuses (regardless of whether they are online) will be cached using lruCache.
|
||||
@@ -112,7 +113,7 @@ func (o *OnlineCache) initUsersOnlineStatus(ctx context.Context) (err error) {
|
||||
cursor := uint64(0)
|
||||
for resp == nil || resp.NextCursor != 0 {
|
||||
if err = retryOperation(func() error {
|
||||
resp, err = o.user.GetAllOnlineUsers(ctx, cursor)
|
||||
resp, err = user.GetAllOnlineUsersCaller.Invoke(ctx, &user.GetAllOnlineUsersReq{Cursor: cursor})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -186,7 +187,17 @@ func (o *OnlineCache) doSubscribe(ctx context.Context, rdb redis.UniversalClient
|
||||
|
||||
func (o *OnlineCache) getUserOnlinePlatform(ctx context.Context, userID string) ([]int32, error) {
|
||||
platformIDs, err := o.lruCache.Get(userID, func() ([]int32, error) {
|
||||
return o.user.GetUserOnlinePlatform(ctx, userID)
|
||||
resp, err := rpccall.ExtractField(ctx, user.GetUserStatusCaller.Invoke, &user.GetUserStatusReq{
|
||||
UserID: o.adminUserID[0],
|
||||
UserIDs: []string{userID},
|
||||
}, (*user.GetUserStatusResp).GetStatusList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(resp) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return resp[0].PlatformIDs, nil
|
||||
})
|
||||
if err != nil {
|
||||
log.ZError(ctx, "OnlineCache GetUserOnlinePlatform", err, "userID", userID)
|
||||
@@ -228,7 +239,10 @@ func (o *OnlineCache) getUserOnlinePlatformBatch(ctx context.Context, userIDs []
|
||||
platformIDsMap, err := o.lruCache.GetBatch(userIDs, func(missingUsers []string) (map[string][]int32, error) {
|
||||
platformIDsMap := make(map[string][]int32)
|
||||
|
||||
usersStatus, err := o.user.GetUsersOnlinePlatform(ctx, missingUsers)
|
||||
usersStatus, err := rpccall.ExtractField(ctx, user.GetUserStatusCaller.Invoke, &user.GetUserStatusReq{
|
||||
UserID: o.adminUserID[0],
|
||||
UserIDs: missingUsers,
|
||||
}, (*user.GetUserStatusResp).GetStatusList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user