diff options
author | ThibG <thib@sitedethib.com> | 2021-02-02 14:49:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 14:49:57 +0100 |
commit | a044ddac5b3c0e2012c0e91bfbc07aa256a0684f (patch) | |
tree | f06b15414afa9204c50471de1846672a98ba81db /app/models | |
parent | c8d11b8bdb97a1a2f8aaf5deca5f1c6c7c0d2688 (diff) |
Fix race conditions on account migration creation (#15597)
* Atomically check for processing lock in Move handler * Prevent race condition when creating account migrations Fixes #15595 * Add tests Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account_migration.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/app/models/account_migration.rb b/app/models/account_migration.rb index 4fae98ed7..ded32c9c6 100644 --- a/app/models/account_migration.rb +++ b/app/models/account_migration.rb @@ -14,6 +14,8 @@ # class AccountMigration < ApplicationRecord + include Redisable + COOLDOWN_PERIOD = 30.days.freeze belongs_to :account @@ -39,7 +41,13 @@ class AccountMigration < ApplicationRecord return false unless errors.empty? - save + RedisLock.acquire(lock_options) do |lock| + if lock.acquired? + save + else + raise Mastodon::RaceConditionError + end + end end def cooldown_at @@ -75,4 +83,8 @@ class AccountMigration < ApplicationRecord def validate_migration_cooldown errors.add(:base, I18n.t('migrations.errors.on_cooldown')) if account.migrations.within_cooldown.exists? end + + def lock_options + { redis: redis, key: "account_migration:#{account.id}" } + end end |