Skip to content

Commit

Permalink
feat: correct concurrecy in friend module. (#590)
Browse files Browse the repository at this point in the history
* feat: correct concurrecy in friend module.

* update doNotification logic.

* remove unnecessary space.
  • Loading branch information
mo3et authored Jul 15, 2024
1 parent 9b93997 commit dbc88d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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

0 comments on commit dbc88d6

Please sign in to comment.