Skip to content

optimize the freeze caused by too many friends and group applications #852

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

Merged
merged 17 commits into from
Feb 7, 2025
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
16 changes: 16 additions & 0 deletions internal/group/full_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (g *Group) SyncAllJoinedGroupsAndMembers(ctx context.Context) error {
}

func (g *Group) SyncAllSelfGroupApplication(ctx context.Context) error {
if !g.groupRequestSyncerLock.TryLock() {
return nil
}
defer g.groupRequestSyncerLock.Unlock()
list, err := g.GetServerSelfGroupApplication(ctx)
if err != nil {
return err
Expand All @@ -45,6 +49,10 @@ func (g *Group) SyncAllSelfGroupApplication(ctx context.Context) error {
}

func (g *Group) SyncAllSelfGroupApplicationWithoutNotice(ctx context.Context) error {
if !g.groupRequestSyncerLock.TryLock() {
return nil
}
defer g.groupRequestSyncerLock.Unlock()
list, err := g.GetServerSelfGroupApplication(ctx)
if err != nil {
return err
Expand All @@ -64,6 +72,10 @@ func (g *Group) SyncSelfGroupApplications(ctx context.Context, groupIDs ...strin
}

func (g *Group) SyncAllAdminGroupApplication(ctx context.Context) error {
if !g.groupAdminRequestSyncerLock.TryLock() {
return nil
}
defer g.groupAdminRequestSyncerLock.Unlock()
requests, err := g.GetServerAdminGroupApplicationList(ctx)
if err != nil {
return err
Expand All @@ -76,6 +88,10 @@ func (g *Group) SyncAllAdminGroupApplication(ctx context.Context) error {
}

func (g *Group) SyncAllAdminGroupApplicationWithoutNotice(ctx context.Context) error {
if !g.groupAdminRequestSyncerLock.TryLock() {
return nil
}
defer g.groupAdminRequestSyncerLock.Unlock()
requests, err := g.GetServerAdminGroupApplicationList(ctx)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions internal/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type Group struct {
listenerForService open_im_sdk_callback.OnListenerForService

groupMemberCache *cache.Cache[string, *model_struct.LocalGroupMember]

groupRequestSyncerLock sync.Mutex
groupAdminRequestSyncerLock sync.Mutex
}

func (g *Group) initSyncer() {
Expand Down
3 changes: 3 additions & 0 deletions internal/relation/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type Relation struct {
conversationCh chan common.Cmd2Value
listenerForService open_im_sdk_callback.OnListenerForService
relationSyncMutex sync.Mutex

requestRecvSyncerLock sync.Mutex
requestSendSyncerLock sync.Mutex
}

func (r *Relation) initSyncer() {
Expand Down
27 changes: 27 additions & 0 deletions internal/relation/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import (
)

func (r *Relation) SyncBothFriendRequest(ctx context.Context, fromUserID, toUserID string) error {
if toUserID == r.loginUserID {
if !r.requestRecvSyncerLock.TryLock() {
return nil
}
defer r.requestRecvSyncerLock.Unlock()
} else {
if !r.requestSendSyncerLock.TryLock() {
return nil
}
defer r.requestSendSyncerLock.Unlock()
}
req := &relation.GetDesignatedFriendsApplyReq{FromUserID: fromUserID, ToUserID: toUserID}
friendRequests, err := r.getDesignatedFriendsApply(ctx, req)
if err != nil {
Expand All @@ -32,6 +43,10 @@ func (r *Relation) SyncBothFriendRequest(ctx context.Context, fromUserID, toUser

// SyncAllSelfFriendApplication send
func (r *Relation) SyncAllSelfFriendApplication(ctx context.Context) error {
if !r.requestSendSyncerLock.TryLock() {
return nil
}
defer r.requestSendSyncerLock.Unlock()
req := &relation.GetPaginationFriendsApplyFromReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
requests, err := r.getSelfFriendApplicationList(ctx, req)
if err != nil {
Expand All @@ -45,6 +60,10 @@ func (r *Relation) SyncAllSelfFriendApplication(ctx context.Context) error {
}

func (r *Relation) SyncAllSelfFriendApplicationWithoutNotice(ctx context.Context) error {
if !r.requestSendSyncerLock.TryLock() {
return nil
}
defer r.requestSendSyncerLock.Unlock()
req := &relation.GetPaginationFriendsApplyFromReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
requests, err := r.getSelfFriendApplicationList(ctx, req)
if err != nil {
Expand All @@ -59,6 +78,10 @@ func (r *Relation) SyncAllSelfFriendApplicationWithoutNotice(ctx context.Context

// SyncAllFriendApplication recv
func (r *Relation) SyncAllFriendApplication(ctx context.Context) error {
if !r.requestRecvSyncerLock.TryLock() {
return nil
}
defer r.requestRecvSyncerLock.Unlock()
req := &relation.GetPaginationFriendsApplyToReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
requests, err := r.getFriendApplicationList(ctx, req)
if err != nil {
Expand All @@ -71,6 +94,10 @@ func (r *Relation) SyncAllFriendApplication(ctx context.Context) error {
return r.requestRecvSyncer.Sync(ctx, datautil.Batch(ServerFriendRequestToLocalFriendRequest, requests), localData, nil)
}
func (r *Relation) SyncAllFriendApplicationWithoutNotice(ctx context.Context) error {
if !r.requestRecvSyncerLock.TryLock() {
return nil
}
defer r.requestRecvSyncerLock.Unlock()
req := &relation.GetPaginationFriendsApplyToReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
requests, err := r.getFriendApplicationList(ctx, req)
if err != nil {
Expand Down
Loading