Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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