Skip to content

Commit a88f90f

Browse files
authored
feat: add retry in msg_sync. (#1044)
1 parent aea3fb8 commit a88f90f

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

internal/interaction/msg_sync.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,34 @@ func (m *MsgSyncer) doConnected(ctx context.Context) {
412412
common.DispatchSyncFlag(ctx, constant.MsgSyncBegin, m.conversationEventQueue)
413413
}
414414
var resp sdkws.GetMaxSeqResp
415-
if err := m.longConnMgr.SendReqWaitResp(ctx, &sdkws.GetMaxSeqReq{UserID: m.loginUserID}, constant.GetNewestSeq, &resp); err != nil {
415+
maxRetries := 3 // max number of retries
416+
retryInterval := 2 * time.Second // wait time between retries
417+
418+
var err error
419+
for retry := range maxRetries {
420+
if retry > 0 {
421+
log.ZWarn(ctx, "retrying to get max seq", nil, "attempt", retry+1, "max", maxRetries)
422+
423+
// Exponential Backoff Strategy
424+
time.Sleep(retryInterval)
425+
retryInterval *= 2
426+
}
427+
428+
err = m.longConnMgr.SendReqWaitResp(ctx, &sdkws.GetMaxSeqReq{UserID: m.loginUserID}, constant.GetNewestSeq, &resp)
429+
if err == nil {
430+
log.ZDebug(ctx, "get max seq success", "resp", resp.MaxSeqs)
431+
break
432+
}
433+
434+
log.ZWarn(ctx, "get max seq attempt failed", err, "attempt", retry+1, "max", maxRetries)
435+
}
436+
437+
if err != nil {
416438
log.ZError(ctx, "get max seq error", err)
417439
common.DispatchSyncFlag(ctx, constant.MsgSyncFailed, m.conversationEventQueue)
418440
return
419-
} else {
420-
log.ZDebug(ctx, "get max seq success", "resp", resp.MaxSeqs)
421441
}
442+
422443
m.compareSeqsAndBatchSync(ctx, resp.MaxSeqs, connectPullNums)
423444
if reinstalled {
424445
common.DispatchSyncFlag(ctx, constant.AppDataSyncFinish, m.conversationEventQueue)
@@ -665,7 +686,7 @@ func (m *MsgSyncer) syncMsgBySeqs(ctx context.Context, conversationID string, se
665686
var pullMsgResp sdkws.PullMessageBySeqsResp
666687
err := m.longConnMgr.SendReqWaitResp(ctx, &pullMsgReq, constant.PullMsgByRange, &pullMsgResp)
667688
if err != nil {
668-
log.ZError(ctx, "syncMsgFromServerSplit err", err, "pullMsgReq", pullMsgReq)
689+
log.ZError(ctx, "syncMsgFromServerSplit err", err, "pullMsgReq", &pullMsgReq)
669690
continue
670691
}
671692
i++

internal/third/file/file_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ package file
22

33
import (
44
"context"
5-
"github.com/openimsdk/openim-sdk-core/v3/pkg/ccontext"
6-
"github.com/openimsdk/openim-sdk-core/v3/sdk_struct"
75
"path/filepath"
86
"testing"
7+
8+
"github.com/openimsdk/openim-sdk-core/v3/pkg/ccontext"
9+
"github.com/openimsdk/openim-sdk-core/v3/sdk_struct"
910
)
1011

1112
func TestUpload(t *testing.T) {
1213
conf := &ccontext.GlobalConfig{
1314
UserID: `4931176757`,
1415
Token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiI0OTMxMTc2NzU3IiwiUGxhdGZvcm1JRCI6MSwiZXhwIjoxNzA3MTE0MjIyLCJuYmYiOjE2OTkzMzc5MjIsImlhdCI6MTY5OTMzODIyMn0.AyNvrMGEdXD5rkvn7ZLHCNs-lNbDCb2otn97yLXia5Y`,
15-
IMConfig: sdk_struct.IMConfig{
16+
IMConfig: &sdk_struct.IMConfig{
1617
ApiAddr: `http://203.56.175.233:10002`,
1718
},
1819
}
1920
ctx := ccontext.WithInfo(context.WithValue(context.Background(), "operationID", "OP123456"), conf)
20-
f := NewFile(nil, conf.UserID)
21+
f := NewFile()
2122

2223
//fp := `C:\Users\openIM\Desktop\my_image (2).tar`
2324
//fp := `C:\Users\openIM\Desktop\1234.zip`

pkg/common/trigger_channel.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,19 @@ type DeleteConNode struct {
104104
type SyncReactionExtensionsNode struct {
105105
OperationID string
106106
Action int
107-
Args interface{}
107+
Args any
108108
}
109109

110110
type UpdateConNode struct {
111111
ConID string
112112
Action int //1 Delete the conversation; 2 Update the latest news in the conversation or add a conversation; 3 Put a conversation on the top;
113113
// 4 Cancel a conversation on the top, 5 Messages are not read and set to 0, 6 New conversations
114-
Args interface{}
114+
Args any
115115
}
116116

117117
type UpdateMessageNode struct {
118118
Action int
119-
Args interface{}
119+
Args any
120120
}
121121

122122
type UpdateConInfo struct {

0 commit comments

Comments
 (0)