From 081e75146c63f0325409f1ce898d1ce381d1ca14 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sun, 13 Mar 2022 16:15:19 +0800 Subject: [PATCH] Fix: add optimism lock when updating file size --- assets | 2 +- models/file.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/assets b/assets index 39b5171..e0da8f4 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 39b5171449fb212bab2bb55a10f564012cca88b9 +Subproject commit e0da8f48856e3fb6e3e9cc920a32390ca132935e diff --git a/models/file.go b/models/file.go index 3eb81e8..004d157 100644 --- a/models/file.go +++ b/models/file.go @@ -268,6 +268,7 @@ func (file *File) UpdatePicInfo(value string) error { } // UpdateSize 更新文件的大小信息 +// TODO: 全局锁 func (file *File) UpdateSize(value uint64) error { tx := DB.Begin() var sizeDelta uint64 @@ -281,7 +282,10 @@ func (file *File) UpdateSize(value uint64) error { sizeDelta = file.Size - value } - if res := tx.Model(&file).Set("gorm:association_autoupdate", false).Update("size", value); res.Error != nil { + if res := tx.Model(&file). + Where("size = ?", file.Size). + Set("gorm:association_autoupdate", false). + Update("size", value); res.Error != nil { tx.Rollback() return res.Error } @@ -291,6 +295,7 @@ func (file *File) UpdateSize(value uint64) error { return err } + file.Size = value return tx.Commit().Error }