From 461542784b555237316f3dd5e32ea224cd3ab8ef Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 29 May 2018 22:55:33 +0200 Subject: [PATCH] Reduce wasted work in RemoveStatusService due to inactive followers (#7672) --- app/models/concerns/account_interactions.rb | 11 +++++++++++ app/services/batched_remove_status_service.rb | 4 ++-- app/services/fan_out_on_write_service.rb | 4 ++-- app/services/remove_status_service.rb | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index ae43711bea..ef59f5d151 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -183,4 +183,15 @@ module AccountInteractions def pinned?(status) status_pins.where(status: status).exists? end + + def followers_for_local_distribution + followers.local + .joins(:user) + .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago) + end + + def lists_for_local_distribution + lists.joins(account: :user) + .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago) + end end diff --git a/app/services/batched_remove_status_service.rb b/app/services/batched_remove_status_service.rb index ace51a1fcc..dab1c47940 100644 --- a/app/services/batched_remove_status_service.rb +++ b/app/services/batched_remove_status_service.rb @@ -53,7 +53,7 @@ class BatchedRemoveStatusService < BaseService end def unpush_from_home_timelines(account, statuses) - recipients = account.followers.local.to_a + recipients = account.followers_for_local_distribution.to_a recipients << account if account.local? @@ -65,7 +65,7 @@ class BatchedRemoveStatusService < BaseService end def unpush_from_list_timelines(account, statuses) - account.lists.select(:id, :account_id).each do |list| + account.lists_for_local_distribution.select(:id, :account_id).each do |list| statuses.each do |status| FeedManager.instance.unpush_from_list(list, status) end diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb index 8b36302299..5efd3edb2e 100644 --- a/app/services/fan_out_on_write_service.rb +++ b/app/services/fan_out_on_write_service.rb @@ -38,7 +38,7 @@ class FanOutOnWriteService < BaseService def deliver_to_followers(status) Rails.logger.debug "Delivering status #{status.id} to followers" - status.account.followers.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).select(:id).reorder(nil).find_in_batches do |followers| + status.account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers| FeedInsertWorker.push_bulk(followers) do |follower| [status.id, follower.id, :home] end @@ -48,7 +48,7 @@ class FanOutOnWriteService < BaseService def deliver_to_lists(status) Rails.logger.debug "Delivering status #{status.id} to lists" - status.account.lists.joins(account: :user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).select(:id).reorder(nil).find_in_batches do |lists| + status.account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists| FeedInsertWorker.push_bulk(lists) do |list| [status.id, list.id, :list] end diff --git a/app/services/remove_status_service.rb b/app/services/remove_status_service.rb index 8c3e184442..b9631077cc 100644 --- a/app/services/remove_status_service.rb +++ b/app/services/remove_status_service.rb @@ -43,13 +43,13 @@ class RemoveStatusService < BaseService end def remove_from_followers - @account.followers.local.find_each do |follower| + @account.followers_for_local_distribution.find_each do |follower| FeedManager.instance.unpush_from_home(follower, @status) end end def remove_from_lists - @account.lists.select(:id, :account_id).find_each do |list| + @account.lists_for_local_distribution.select(:id, :account_id).find_each do |list| FeedManager.instance.unpush_from_list(list, @status) end end