Refactor how Redis locks are created (#18400)
* Refactor how Redis locks are created * Fix autorelease duration on account deletion lock
This commit is contained in:
parent
12535568f7
commit
6cf57c6765
16 changed files with 112 additions and 179 deletions
|
@ -4,6 +4,7 @@ class VoteService < BaseService
|
|||
include Authorization
|
||||
include Payloadable
|
||||
include Redisable
|
||||
include Lockable
|
||||
|
||||
def call(account, poll, choices)
|
||||
authorize_with account, poll, :vote?
|
||||
|
@ -15,17 +16,13 @@ class VoteService < BaseService
|
|||
|
||||
already_voted = true
|
||||
|
||||
RedisLock.acquire(lock_options) do |lock|
|
||||
if lock.acquired?
|
||||
already_voted = @poll.votes.where(account: @account).exists?
|
||||
with_lock("vote:#{@poll.id}:#{@account.id}") do
|
||||
already_voted = @poll.votes.where(account: @account).exists?
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
@choices.each do |choice|
|
||||
@votes << @poll.votes.create!(account: @account, choice: Integer(choice))
|
||||
end
|
||||
ApplicationRecord.transaction do
|
||||
@choices.each do |choice|
|
||||
@votes << @poll.votes.create!(account: @account, choice: Integer(choice))
|
||||
end
|
||||
else
|
||||
raise Mastodon::RaceConditionError
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -76,8 +73,4 @@ class VoteService < BaseService
|
|||
@poll.reload
|
||||
retry
|
||||
end
|
||||
|
||||
def lock_options
|
||||
{ redis: redis, key: "vote:#{@poll.id}:#{@account.id}" }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue