Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: correct concurrecy in friend module. #590

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/friend/friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package friend

import (
"context"
"sync"

"github.com/openimsdk/openim-sdk-core/v3/internal/user"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
Expand Down Expand Up @@ -53,6 +54,7 @@ type Friend struct {
requestSendSyncer *syncer.Syncer[*model_struct.LocalFriendRequest, syncer.NoResp, [2]string]
conversationCh chan common.Cmd2Value
listenerForService open_im_sdk_callback.OnListenerForService
friendSyncMutex sync.Mutex
}

func (f *Friend) initSyncer() {
Expand Down
3 changes: 3 additions & 0 deletions internal/friend/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (f *Friend) DoNotification(ctx context.Context, msg *sdkws.MsgData) {
}

func (f *Friend) doNotification(ctx context.Context, msg *sdkws.MsgData) error {
f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

switch msg.ContentType {
case constant.FriendApplicationNotification:
tips := sdkws.FriendApplicationTips{}
Expand Down
24 changes: 24 additions & 0 deletions internal/friend/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (f *Friend) AddFriend(ctx context.Context, userIDReqMsg *friend.ApplyToAddF
if err := util.ApiPost(ctx, constant.AddFriendRouter, userIDReqMsg, nil); err != nil {
return err
}

f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

return f.SyncAllFriendApplication(ctx)
}

Expand All @@ -115,6 +119,9 @@ func (f *Friend) RespondFriendApply(ctx context.Context, req *friend.RespondFrie
if err := util.ApiPost(ctx, constant.AddFriendResponse, req, nil); err != nil {
return err
}
f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

if req.HandleResult == constant.FriendResponseAgree {
_ = f.IncrSyncFriends(ctx)
}
Expand Down Expand Up @@ -164,9 +171,14 @@ func (f *Friend) DeleteFriend(ctx context.Context, friendUserID string) error {
if err := util.ApiPost(ctx, constant.DeleteFriendRouter, &friend.DeleteFriendReq{OwnerUserID: f.loginUserID, FriendUserID: friendUserID}, nil); err != nil {
return err
}

f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

return f.IncrSyncFriends(ctx)
}

// Full GetFriendList
func (f *Friend) GetFriendList(ctx context.Context) ([]*server_api_params.FullUserInfo, error) {
localFriendList, err := f.db.GetAllFriendList(ctx)
if err != nil {
Expand Down Expand Up @@ -329,13 +341,21 @@ func (f *Friend) SetFriendRemark(ctx context.Context, userIDRemark *sdk.SetFrien
if err := util.ApiPost(ctx, constant.SetFriendRemark, &friend.SetFriendRemarkReq{OwnerUserID: f.loginUserID, FriendUserID: userIDRemark.ToUserID, Remark: userIDRemark.Remark}, nil); err != nil {
return err
}

f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

return f.IncrSyncFriends(ctx)
}

func (f *Friend) PinFriends(ctx context.Context, friends *sdk.SetFriendPinParams) error {
if err := util.ApiPost(ctx, constant.UpdateFriends, &friend.UpdateFriendsReq{OwnerUserID: f.loginUserID, FriendUserIDs: friends.ToUserIDs, IsPinned: friends.IsPinned}, nil); err != nil {
return err
}

f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

return f.IncrSyncFriends(ctx)
}

Expand All @@ -350,6 +370,10 @@ func (f *Friend) RemoveBlack(ctx context.Context, blackUserID string) error {
if err := util.ApiPost(ctx, constant.RemoveBlackRouter, &friend.RemoveBlackReq{OwnerUserID: f.loginUserID, BlackUserID: blackUserID}, nil); err != nil {
return err
}

f.friendSyncMutex.Lock()
defer f.friendSyncMutex.Unlock()

return f.SyncAllBlackList(ctx)
}

Expand Down
Loading