diff options
author | Lauris BH <lauris@nix.lv> | 2017-07-20 06:15:10 +0300 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-07-20 11:15:10 +0800 |
commit | dde0052ca2c478f298942f6fbbe88d27259374ae (patch) | |
tree | 01f06177c4151ee53bef3880b4a68e3d2955ac51 /models/ssh_key.go | |
parent | 3702dac0d5bc39f63da987227822d86224b6c446 (diff) | |
download | gitea-dde0052ca2c478f298942f6fbbe88d27259374ae.tar.gz gitea-dde0052ca2c478f298942f6fbbe88d27259374ae.zip |
Fix key usage time update if the key is used in parallel for multiple operations (#2185)
Diffstat (limited to 'models/ssh_key.go')
-rw-r--r-- | models/ssh_key.go | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/models/ssh_key.go b/models/ssh_key.go index f70e16c62a..649c50be6c 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -496,16 +496,21 @@ func UpdatePublicKey(key *PublicKey) error { // UpdatePublicKeyUpdated updates public key use time. func UpdatePublicKeyUpdated(id int64) error { 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, UpdatedUnix: now.Unix(), }) if err != nil { return err } - if cnt != 1 { - return ErrKeyNotExist{id} - } return nil } |