0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-12-22 19:14:02 +01:00

Fix key usage time update if the key is used in parallel for multiple operations (#2185)

This commit is contained in:
Lauris BH 2017-07-20 06:15:10 +03:00 committed by Lunny Xiao
parent 3702dac0d5
commit dde0052ca2

View file

@ -496,16 +496,21 @@ func UpdatePublicKey(key *PublicKey) error {
// UpdatePublicKeyUpdated updates public key use time. // UpdatePublicKeyUpdated updates public key use time.
func UpdatePublicKeyUpdated(id int64) error { func UpdatePublicKeyUpdated(id int64) error {
now := time.Now() now := time.Now()
cnt, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{ // Check if key exists before update as affected rows count is unreliable
// and will return 0 affected rows if two updates are made at the same time
if cnt, err := x.ID(id).Count(&PublicKey{}); err != nil {
return err
} else if cnt != 1 {
return ErrKeyNotExist{id}
}
_, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{
Updated: now, Updated: now,
UpdatedUnix: now.Unix(), UpdatedUnix: now.Unix(),
}) })
if err != nil { if err != nil {
return err return err
} }
if cnt != 1 {
return ErrKeyNotExist{id}
}
return nil return nil
} }