Skip to content

Commit b1d8cbf

Browse files
authored
optimize the freeze caused by too many friends and group applications (#852)
* feat: code adjustment * feat: Cmd2Value carry caller * feat: Cmd2Value carry caller * feat: Cmd2Value carry caller * feat: Cmd2Value carry caller * fix: SearchLocalMessages no such table * fix: optimize the freeze caused by too many friends and group applications
1 parent b632cc2 commit b1d8cbf

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

internal/group/full_sync.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func (g *Group) SyncAllJoinedGroupsAndMembers(ctx context.Context) error {
2929
}
3030

3131
func (g *Group) SyncAllSelfGroupApplication(ctx context.Context) error {
32+
if !g.groupRequestSyncerLock.TryLock() {
33+
return nil
34+
}
35+
defer g.groupRequestSyncerLock.Unlock()
3236
list, err := g.GetServerSelfGroupApplication(ctx)
3337
if err != nil {
3438
return err
@@ -45,6 +49,10 @@ func (g *Group) SyncAllSelfGroupApplication(ctx context.Context) error {
4549
}
4650

4751
func (g *Group) SyncAllSelfGroupApplicationWithoutNotice(ctx context.Context) error {
52+
if !g.groupRequestSyncerLock.TryLock() {
53+
return nil
54+
}
55+
defer g.groupRequestSyncerLock.Unlock()
4856
list, err := g.GetServerSelfGroupApplication(ctx)
4957
if err != nil {
5058
return err
@@ -64,6 +72,10 @@ func (g *Group) SyncSelfGroupApplications(ctx context.Context, groupIDs ...strin
6472
}
6573

6674
func (g *Group) SyncAllAdminGroupApplication(ctx context.Context) error {
75+
if !g.groupAdminRequestSyncerLock.TryLock() {
76+
return nil
77+
}
78+
defer g.groupAdminRequestSyncerLock.Unlock()
6779
requests, err := g.GetServerAdminGroupApplicationList(ctx)
6880
if err != nil {
6981
return err
@@ -76,6 +88,10 @@ func (g *Group) SyncAllAdminGroupApplication(ctx context.Context) error {
7688
}
7789

7890
func (g *Group) SyncAllAdminGroupApplicationWithoutNotice(ctx context.Context) error {
91+
if !g.groupAdminRequestSyncerLock.TryLock() {
92+
return nil
93+
}
94+
defer g.groupAdminRequestSyncerLock.Unlock()
7995
requests, err := g.GetServerAdminGroupApplicationList(ctx)
8096
if err != nil {
8197
return err

internal/group/group.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ type Group struct {
6969
listenerForService open_im_sdk_callback.OnListenerForService
7070

7171
groupMemberCache *cache.Cache[string, *model_struct.LocalGroupMember]
72+
73+
groupRequestSyncerLock sync.Mutex
74+
groupAdminRequestSyncerLock sync.Mutex
7275
}
7376

7477
func (g *Group) initSyncer() {

internal/relation/relation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type Relation struct {
4242
conversationCh chan common.Cmd2Value
4343
listenerForService open_im_sdk_callback.OnListenerForService
4444
relationSyncMutex sync.Mutex
45+
46+
requestRecvSyncerLock sync.Mutex
47+
requestSendSyncerLock sync.Mutex
4548
}
4649

4750
func (r *Relation) initSyncer() {

internal/relation/sync.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import (
1313
)
1414

1515
func (r *Relation) SyncBothFriendRequest(ctx context.Context, fromUserID, toUserID string) error {
16+
if toUserID == r.loginUserID {
17+
if !r.requestRecvSyncerLock.TryLock() {
18+
return nil
19+
}
20+
defer r.requestRecvSyncerLock.Unlock()
21+
} else {
22+
if !r.requestSendSyncerLock.TryLock() {
23+
return nil
24+
}
25+
defer r.requestSendSyncerLock.Unlock()
26+
}
1627
req := &relation.GetDesignatedFriendsApplyReq{FromUserID: fromUserID, ToUserID: toUserID}
1728
friendRequests, err := r.getDesignatedFriendsApply(ctx, req)
1829
if err != nil {
@@ -32,6 +43,10 @@ func (r *Relation) SyncBothFriendRequest(ctx context.Context, fromUserID, toUser
3243

3344
// SyncAllSelfFriendApplication send
3445
func (r *Relation) SyncAllSelfFriendApplication(ctx context.Context) error {
46+
if !r.requestSendSyncerLock.TryLock() {
47+
return nil
48+
}
49+
defer r.requestSendSyncerLock.Unlock()
3550
req := &relation.GetPaginationFriendsApplyFromReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
3651
requests, err := r.getSelfFriendApplicationList(ctx, req)
3752
if err != nil {
@@ -45,6 +60,10 @@ func (r *Relation) SyncAllSelfFriendApplication(ctx context.Context) error {
4560
}
4661

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

6079
// SyncAllFriendApplication recv
6180
func (r *Relation) SyncAllFriendApplication(ctx context.Context) error {
81+
if !r.requestRecvSyncerLock.TryLock() {
82+
return nil
83+
}
84+
defer r.requestRecvSyncerLock.Unlock()
6285
req := &relation.GetPaginationFriendsApplyToReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
6386
requests, err := r.getFriendApplicationList(ctx, req)
6487
if err != nil {
@@ -71,6 +94,10 @@ func (r *Relation) SyncAllFriendApplication(ctx context.Context) error {
7194
return r.requestRecvSyncer.Sync(ctx, datautil.Batch(ServerFriendRequestToLocalFriendRequest, requests), localData, nil)
7295
}
7396
func (r *Relation) SyncAllFriendApplicationWithoutNotice(ctx context.Context) error {
97+
if !r.requestRecvSyncerLock.TryLock() {
98+
return nil
99+
}
100+
defer r.requestRecvSyncerLock.Unlock()
74101
req := &relation.GetPaginationFriendsApplyToReq{UserID: r.loginUserID, Pagination: &sdkws.RequestPagination{}}
75102
requests, err := r.getFriendApplicationList(ctx, req)
76103
if err != nil {

0 commit comments

Comments
 (0)