Skip to content

Commit

Permalink
Move redis lock to DistributionWorker to avoid extra Redis value
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Mar 15, 2019
1 parent 0f0758c commit 4f73117
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
43 changes: 15 additions & 28 deletions app/services/fan_out_on_write_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,27 @@ class FanOutOnWriteService < BaseService
# @param [Status] status
def call(status)
raise Mastodon::RaceConditionError if status.visibility.nil?
@status_id = status.id

RedisLock.acquire(lock_options) do |lock|
if lock.acquired?
return if Redis.current.exists("delete_upon_arrival:#{@status_id}")
render_anonymous_payload(status)

render_anonymous_payload(status)

if status.direct_visibility?
deliver_to_own_conversation(status)
elsif status.limited_visibility?
deliver_to_mentioned_followers(status)
else
deliver_to_self(status) if status.account.local?
deliver_to_followers(status)
deliver_to_lists(status)
end
if status.direct_visibility?
deliver_to_own_conversation(status)
elsif status.limited_visibility?
deliver_to_mentioned_followers(status)
else
deliver_to_self(status) if status.account.local?
deliver_to_followers(status)
deliver_to_lists(status)
end

return if status.account.silenced? || !status.public_visibility? || status.reblog?
return if status.account.silenced? || !status.public_visibility? || status.reblog?

deliver_to_hashtags(status)
deliver_to_hashtags(status)

return if status.reply? && status.in_reply_to_account_id != status.account_id
return if status.reply? && status.in_reply_to_account_id != status.account_id

deliver_to_public(status)
deliver_to_media(status) if status.media_attachments.any?
else
raise Mastodon::RaceConditionError
end
end
deliver_to_public(status)
deliver_to_media(status) if status.media_attachments.any?
end

private
Expand Down Expand Up @@ -103,8 +94,4 @@ def deliver_to_media(status)
def deliver_to_own_conversation(status)
AccountConversation.add_status(status.account, status)
end

def lock_options
{ redis: Redis.current, key: "distribute:#{@status_id}" }
end
end
2 changes: 0 additions & 2 deletions app/services/remove_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def call(status, **options)

RedisLock.acquire(lock_options) do |lock|
if lock.acquired?
redis.setex("delete_status:#{status.id}", 10.minutes.seconds, status.id.to_s)

remove_from_self if status.account.local?
remove_from_followers
remove_from_lists
Expand Down
8 changes: 7 additions & 1 deletion app/workers/distribution_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ class DistributionWorker
include Sidekiq::Worker

def perform(status_id)
FanOutOnWriteService.new.call(Status.find(status_id))
RedisLock.acquire(redis: Redis.current, key: "distribute:#{status_id}") do |lock|
if lock.acquired?
FanOutOnWriteService.new.call(Status.find(status_id))
else
raise Mastodon::RaceConditionError
end
end
rescue ActiveRecord::RecordNotFound
true
end
Expand Down

0 comments on commit 4f73117

Please sign in to comment.