diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index 40914e624..a3bd2c00a 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -10,6 +10,8 @@ import ( goRedis "github.com/go-redis/redis/v8" "github.com/golang/protobuf/proto" "math" + "strconv" + "strings" ) const oldestList = 0 @@ -120,11 +122,12 @@ func deleteMongoMsg(operationID string, ID string, index int64, delMsgIDList *[] return getDelMaxSeqByIDList(*delMsgIDList) + 1, utils.Wrap(err, "") } } + // 递归结束 return msgPb.Seq, nil } } - // 该列表中消息全部为老消息, 加入删除列表继续递归 - if len(msgs.Msg) > 0 { + // 该列表中消息全部为老消息并且列表满了, 加入删除列表继续递归 + if msgListIsFull(msgs) { msgPb := &server_api_params.MsgData{} err = proto.Unmarshal(msgs.Msg[len(msgs.Msg)-1].Msg, msgPb) if err != nil { @@ -141,6 +144,19 @@ func deleteMongoMsg(operationID string, ID string, index int64, delMsgIDList *[] return seq, nil } +func msgListIsFull(chat *db.UserChat) bool { + index, _ := strconv.Atoi(strings.Split(chat.UID, ":")[1]) + if index == 0 { + if len(chat.Msg) >= 4999 { + return true + } + } + if len(chat.Msg) >= 5000 { + return true + } + return false +} + func getDelMaxSeqByIDList(delMsgIDList [][2]interface{}) uint32 { if len(delMsgIDList) == 0 { return 0