Skip to content

Commit

Permalink
Avoid race conditions when creating backups (mastodon#10234)
Browse files Browse the repository at this point in the history
Under load, multiple backups for a single user could be planned, which
is very expensive.
  • Loading branch information
ClearlyClaire authored and Gargron committed Mar 10, 2019
1 parent 7d0076d commit 440f173
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/controllers/settings/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ def show
end

def create
authorize :backup, :create?
raise Mastodon::NotPermittedError unless user_signed_in?

backup = nil

RedisLock.acquire(lock_options) do |lock|
if lock.acquired?
authorize :backup, :create?
backup = current_user.backups.create!
else
raise Mastodon::RaceConditionError
end
end

backup = current_user.backups.create!
BackupWorker.perform_async(backup.id)

redirect_to settings_export_path
end

def lock_options
{ redis: Redis.current, key: "backup:#{current_user.id}" }
end
end

0 comments on commit 440f173

Please sign in to comment.