mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-17 16:06:38 +08:00
optimized blocking issue during bulk log data deletion
Signed-off-by: wozulong <>
This commit is contained in:
parent
208bc5e794
commit
a60f209c85
@ -192,7 +192,7 @@ func DeleteHistoryLogs(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
count, err := model.DeleteOldLog(targetTimestamp)
|
count, err := model.DeleteOldLog(c.Request.Context(), targetTimestamp, 100)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
|
24
model/log.go
24
model/log.go
@ -247,7 +247,25 @@ func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelNa
|
|||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteOldLog(targetTimestamp int64) (int64, error) {
|
func DeleteOldLog(ctx context.Context, targetTimestamp int64, limit int) (int64, error) {
|
||||||
result := LOG_DB.Where("created_at < ?", targetTimestamp).Delete(&Log{})
|
var total int64 = 0
|
||||||
return result.RowsAffected, result.Error
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user