Fix database race condition when tracking OTP requests#5529
Merged
mitchellhenke merged 1 commit intomainfrom Oct 21, 2021
Merged
Fix database race condition when tracking OTP requests#5529mitchellhenke merged 1 commit intomainfrom
mitchellhenke merged 1 commit intomainfrom
Conversation
ac52e99 to
012bb4f
Compare
zachmargolis
approved these changes
Oct 21, 2021
Contributor
zachmargolis
left a comment
There was a problem hiding this comment.
LGTM, this looks like it reduces test coverage but we're relying on this new create_or_find to itself rely on DB indexes and be smarter, right?
app/models/otp_requests_tracker.rb
Outdated
Contributor
There was a problem hiding this comment.
we can just inline this now, right?
Contributor
Author
|
012bb4f to
356ce57
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OtpRequestshas a unique index on[phone_fingerprint, phone_confirmed], so we get exceptions (NewRelic link) due to the race condition when two requests try to create the row in a short time period.This PR makes the change use
create_or_find_bywhich attempts to create the row and rescues if the unique index if the unique constraint is violated and then issues a select to get that row. This is similar to the work done in #5341There is a slight behavior change in this in that the row will have
nilforotp_last_sent_atwhen it is initially created, but this seems to be safe at first glance since we callincrementwhen an OTP is sent.