From fa9ff6709daf8acfde39473f18137abfe8332dfb Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 21 Jun 2023 10:37:18 +0800 Subject: [PATCH] group --- pkg/common/db/localcache/conversation.go | 24 ++++++-------------- pkg/common/db/localcache/group.go | 29 ++++++++++-------------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/pkg/common/db/localcache/conversation.go b/pkg/common/db/localcache/conversation.go index 8db3f28fd..3cb647217 100644 --- a/pkg/common/db/localcache/conversation.go +++ b/pkg/common/db/localcache/conversation.go @@ -4,16 +4,16 @@ import ( "context" "sync" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" ) type ConversationLocalCache struct { lock sync.Mutex superGroupRecvMsgNotNotifyUserIDs map[string]Hash conversationIDs map[string]Hash - client discoveryregistry.SvcDiscoveryRegistry + client *rpcclient.Conversation } type Hash struct { @@ -21,21 +21,16 @@ type Hash struct { ids []string } -func NewConversationLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache { +func NewConversationLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache { return &ConversationLocalCache{ superGroupRecvMsgNotNotifyUserIDs: make(map[string]Hash), conversationIDs: make(map[string]Hash), - client: client, + client: rpcclient.NewConversation(discov), } } func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) { - conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName) - if err != nil { - return nil, err - } - client := conversation.NewConversationClient(conn) - resp, err := client.GetRecvMsgNotNotifyUserIDs(ctx, &conversation.GetRecvMsgNotNotifyUserIDsReq{ + resp, err := g.client.Client.GetRecvMsgNotNotifyUserIDs(ctx, &conversation.GetRecvMsgNotNotifyUserIDsReq{ GroupID: groupID, }) if err != nil { @@ -45,12 +40,7 @@ func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, } func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID string) ([]string, error) { - conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName) - if err != nil { - return nil, err - } - client := conversation.NewConversationClient(conn) - resp, err := client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{ + resp, err := g.client.Client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{ OwnerUserID: userID, }) if err != nil { @@ -60,7 +50,7 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID defer g.lock.Unlock() hash, ok := g.conversationIDs[userID] if !ok || hash.hash != resp.Hash { - conversationIDsResp, err := client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{ + conversationIDsResp, err := g.client.Client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{ UserID: userID, }) if err != nil { diff --git a/pkg/common/db/localcache/group.go b/pkg/common/db/localcache/group.go index c0b9d4cd8..49443412c 100644 --- a/pkg/common/db/localcache/group.go +++ b/pkg/common/db/localcache/group.go @@ -4,17 +4,16 @@ import ( "context" "sync" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" - "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" ) type GroupLocalCache struct { - lock sync.Mutex - cache map[string]GroupMemberIDsHash - conn *grpc.ClientConn + lock sync.Mutex + cache map[string]GroupMemberIDsHash + client *rpcclient.Group } type GroupMemberIDsHash struct { @@ -22,22 +21,16 @@ type GroupMemberIDsHash struct { userIDs []string } -func NewGroupLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache { - conn, err := client.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName) - if err != nil { - panic(err) - } +func NewGroupLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache { + client := rpcclient.NewGroup(discov) return &GroupLocalCache{ - cache: make(map[string]GroupMemberIDsHash, 0), - conn: conn, + cache: make(map[string]GroupMemberIDsHash, 0), + client: client, } } func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { - g.lock.Lock() - defer g.lock.Unlock() - client := group.NewGroupClient(g.conn) - resp, err := client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{ + resp, err := g.client.Client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{ GroupIDs: []string{groupID}, }) if err != nil { @@ -46,11 +39,13 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) if len(resp.GroupAbstractInfos) < 1 { return nil, errs.ErrGroupIDNotFound } + g.lock.Lock() + defer g.lock.Unlock() localHashInfo, ok := g.cache[groupID] if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash { return localHashInfo.userIDs, nil } - groupMembersResp, err := client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{ + groupMembersResp, err := g.client.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{ GroupID: groupID, }) if err != nil {