Skip to content

Commit a213479

Browse files
authored
fix: add random prefix to remove duplicate messages. (#850)
* fix: refine exception message handling to prevent duplicate messages in clients with poor network conditions. Signed-off-by: Gordon <[email protected]> * fix: primary key conflicts caused by empty messages occupying seq due to sequence gaps. Signed-off-by: Gordon <[email protected]> * fix: server downtime and abnormal message handling may lead to message duplication, and the history retrieval interface might miss messages when the timestamps are the same. Signed-off-by: Gordon <[email protected]> * fix: server downtime and abnormal message handling may lead to message duplication, and the history retrieval interface might miss messages when the timestamps are the same. Signed-off-by: Gordon <[email protected]> * fix: add random prefix to remove duplicate messages. Signed-off-by: Gordon <[email protected]> --------- Signed-off-by: Gordon <[email protected]>
1 parent b1d8cbf commit a213479

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

internal/conversation_msg/conversation_msg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ func (c *Conversation) batchInsertMessageList(ctx context.Context, insertMsg map
673673
}
674674
err := c.db.BatchInsertMessageList(ctx, conversationID, messages)
675675
if err != nil {
676-
log.ZError(ctx, "insert GetMessage detail err:", err, "conversationID", conversationID, "messages", messages)
676+
log.ZError(ctx, "BatchInsertMessageList detail err:", err, "conversationID", conversationID, "messages", messages)
677677
for _, v := range messages {
678678
e := c.db.InsertMessage(ctx, conversationID, v)
679679
if e != nil {

internal/conversation_msg/message_check.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conversation_msg
33
import (
44
"context"
55
"errors"
6+
"math/rand"
67
"time"
78

89
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
@@ -395,11 +396,24 @@ func (c *Conversation) handleExceptionMessages(ctx context.Context, existingMess
395396
prefix = "[CLIENT_DUP]" // Client-side resend or server-side consume messages duplication
396397
}
397398
}
399+
getRandomString := func(length int) string {
400+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
401+
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
402+
403+
b := make([]byte, length)
404+
for i := range b {
405+
b[i] = charset[seededRand.Intn(len(charset))]
406+
}
407+
return string(b)
408+
}
409+
// Generate a random suffix to ensure uniqueness
410+
randomSuffix := "_" + getRandomString(8)
398411

399412
// Mark the message as deleted
400413
message.Status = constant.MsgStatusHasDeleted
401-
// Add the exception prefix to the ClientMsgID for identification
402-
message.ClientMsgID = prefix + message.ClientMsgID
414+
415+
// Add the exception prefix and random suffix to the ClientMsgID for identification
416+
message.ClientMsgID = prefix + message.ClientMsgID + randomSuffix
403417
}
404418

405419
func (c *Conversation) pullMessageIntoTable(ctx context.Context, pullMsgData map[string]*sdkws.PullMsgs, list *[]*model_struct.LocalChatLog) {
@@ -445,6 +459,7 @@ func (c *Conversation) pullMessageIntoTable(ctx context.Context, pullMsgData map
445459
// The message you sent is duplicated, possibly due to a resend or the server consuming
446460
// the message multiple times.
447461
c.handleExceptionMessages(ctx, existingMsg, msg)
462+
v.Status = msg.Status
448463
exceptionMsg = append(exceptionMsg, msg)
449464
insertMessage = append(insertMessage, msg)
450465
}
@@ -460,6 +475,7 @@ func (c *Conversation) pullMessageIntoTable(ctx context.Context, pullMsgData map
460475
// The message sent by others is duplicated, possibly due to a resend or the server consuming
461476
// the message multiple times.
462477
c.handleExceptionMessages(ctx, existingMsg, msg)
478+
v.Status = msg.Status
463479
exceptionMsg = append(exceptionMsg, msg)
464480
insertMessage = append(insertMessage, msg)
465481
}

0 commit comments

Comments
 (0)