diff options
Diffstat (limited to 'app/services/vote_service.rb')
-rw-r--r-- | app/services/vote_service.rb | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/app/services/vote_service.rb b/app/services/vote_service.rb index b77812970..ccd04dbfc 100644 --- a/app/services/vote_service.rb +++ b/app/services/vote_service.rb @@ -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 |