Skip to content

Commit

Permalink
Fix key usage time update if the key is used in parallel for multiple…
Browse files Browse the repository at this point in the history
… operations (#2185)
  • Loading branch information
lafriks authored and lunny committed Jul 20, 2017
1 parent 3702dac commit dde0052
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit dde0052

Please sign in to comment.