From a60f209c855a38bdf6c1e5f5a8285c3a2290ec2d Mon Sep 17 00:00:00 2001 From: wozulong <> Date: Tue, 10 Sep 2024 17:24:44 +0800 Subject: [PATCH] optimized blocking issue during bulk log data deletion Signed-off-by: wozulong <> --- controller/log.go | 2 +- model/log.go | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/controller/log.go b/controller/log.go index ca0e1ec..f817835 100644 --- a/controller/log.go +++ b/controller/log.go @@ -192,7 +192,7 @@ func DeleteHistoryLogs(c *gin.Context) { }) return } - count, err := model.DeleteOldLog(targetTimestamp) + count, err := model.DeleteOldLog(c.Request.Context(), targetTimestamp, 100) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, diff --git a/model/log.go b/model/log.go index 5493a05..f056adb 100644 --- a/model/log.go +++ b/model/log.go @@ -247,7 +247,25 @@ func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelNa return token } -func DeleteOldLog(targetTimestamp int64) (int64, error) { - result := LOG_DB.Where("created_at < ?", targetTimestamp).Delete(&Log{}) - return result.RowsAffected, result.Error +func DeleteOldLog(ctx context.Context, targetTimestamp int64, limit int) (int64, error) { + var total int64 = 0 + + for { + if nil != ctx.Err() { + return total, ctx.Err() + } + + result := LOG_DB.Where("created_at < ?", targetTimestamp).Limit(limit).Delete(&Log{}) + if nil != result.Error { + return total, result.Error + } + + if 0 == result.RowsAffected { + break + } + + total += result.RowsAffected + } + + return total, nil }