Skip to content

Commit

Permalink
防止修改锁状态 同时修改了其他的数据
Browse files Browse the repository at this point in the history
  • Loading branch information
fusikai committed Aug 15, 2024
1 parent 7f67919 commit 2a97247
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/pkg/service/confgov2/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TryLock(uid, configId uint) (err error) {
config.LockUid = uid
now := time.Now()
config.LockAt = &now
err = tx.Save(&config).Error
err = tx.Table("configuration").Where("id = ?", config.ID).Updates(map[string]interface{}{"lock_uid": config.LockUid,
"lock_at": config.LockAt}).Error
if err != nil {
tx.Rollback()
return errors.Wrap(err, "获取编辑锁失败")
Expand All @@ -59,7 +60,8 @@ func Unlock(uid, configId uint) (err error) {

config.LockUid = 0
config.LockAt = nil
err = tx.Save(&config).Error
err = tx.Table("configuration").Where("id = ?", config.ID).Updates(map[string]interface{}{"lock_uid": config.LockUid,
"lock_at": config.LockAt}).Error
if err != nil {
tx.Rollback()
return errors.Wrap(err, "释放编辑锁失败")
Expand All @@ -68,7 +70,7 @@ func Unlock(uid, configId uint) (err error) {
return tx.Commit().Error
}

//clearLockPeriodically 定期清除编辑锁
// clearLockPeriodically 定期清除编辑锁
func clearLockPeriodically() {
var configs []db.Configuration

Expand All @@ -88,7 +90,8 @@ func clearLockPeriodically() {
for _, config := range configs {
config.LockAt = nil
config.LockUid = 0
tx.Save(&config)
tx.Table("configuration").Where("id = ?", config.ID).Updates(map[string]interface{}{"lock_uid": config.LockUid,
"lock_at": config.LockAt})
}
}
tx.Commit()
Expand Down

0 comments on commit 2a97247

Please sign in to comment.