0
0
Fork 0

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:
Eugen Rochko 2022-05-13 00:02:35 +02:00 committed by GitHub
parent 12535568f7
commit 6cf57c6765
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 112 additions and 179 deletions

View file

@ -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