Optimize filter methods in FeedManager a bit, use redis pipelining on merge/unmerge feed methods,

do not re-create a dynamic class on each feed push call, make sure redis-rb uses hiredis
This commit is contained in:
Eugen Rochko 2017-04-04 13:01:14 +02:00
parent eb023beb49
commit ce9df2fa82
3 changed files with 51 additions and 42 deletions

View file

@ -38,7 +38,7 @@ gem 'rqrcode'
gem 'twitter-text' gem 'twitter-text'
gem 'oj' gem 'oj'
gem 'hiredis' gem 'hiredis'
gem 'redis', '~>3.2' gem 'redis', '~>3.2', require: ['redis', 'redis/connection/hiredis']
gem 'fast_blank' gem 'fast_blank'
gem 'htmlentities' gem 'htmlentities'
gem 'simple_form' gem 'simple_form'

View file

@ -51,9 +51,11 @@ class FeedManager
def merge_into_timeline(from_account, into_account) def merge_into_timeline(from_account, into_account)
timeline_key = key(:home, into_account.id) timeline_key = key(:home, into_account.id)
from_account.statuses.limit(MAX_ITEMS).each do |status| redis.pipelined do
next if status.direct_visibility? || filter?(:home, status, into_account) from_account.statuses.limit(MAX_ITEMS).each do |status|
redis.zadd(timeline_key, status.id, status.id) next if status.direct_visibility? || filter?(:home, status, into_account)
redis.zadd(timeline_key, status.id, status.id)
end
end end
trim(:home, into_account.id) trim(:home, into_account.id)
@ -62,30 +64,18 @@ class FeedManager
def unmerge_from_timeline(from_account, into_account) def unmerge_from_timeline(from_account, into_account)
timeline_key = key(:home, into_account.id) timeline_key = key(:home, into_account.id)
from_account.statuses.select('id').find_each do |status| from_account.statuses.select('id').find_in_batches do |statuses|
redis.zrem(timeline_key, status.id) redis.pipelined do
redis.zremrangebyscore(timeline_key, status.id, status.id) statuses.each do |status|
redis.zrem(timeline_key, status.id)
redis.zremrangebyscore(timeline_key, status.id, status.id)
end
end
end end
end end
def inline_render(target_account, template, object) def inline_render(target_account, template, object)
rabl_scope = Class.new do Rabl::Renderer.new(template, object, view_path: 'app/views', format: :json, scope: InlineRablScope.new(target_account)).render
include RoutingHelper
def initialize(account)
@account = account
end
def current_user
@account.try(:user)
end
def current_account
@account
end
end
Rabl::Renderer.new(template, object, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
end end
private private
@ -95,36 +85,38 @@ class FeedManager
end end
def filter_from_home?(status, receiver) def filter_from_home?(status, receiver)
return true if receiver.muting?(status.account) return true if status.reply? && status.in_reply_to_id.nil?
should_filter = false check_for_mutes = [status.account_id]
check_for_mutes.concat([status.reblog.account_id]) if status.reblog?
if status.reply? && status.in_reply_to_id.nil? return true if receiver.muting?(check_for_mutes)
should_filter = true
elsif status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply check_for_blocks = status.mentions.map(&:account_id)
check_for_blocks.concat([status.reblog.account_id]) if status.reblog?
return true if receiver.blocking?(check_for_blocks)
if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
should_filter = !receiver.following?(status.in_reply_to_account) # and I'm not following the person it's a reply to should_filter = !receiver.following?(status.in_reply_to_account) # and I'm not following the person it's a reply to
should_filter &&= !(receiver.id == status.in_reply_to_account_id) # and it's not a reply to me should_filter &&= !(receiver.id == status.in_reply_to_account_id) # and it's not a reply to me
should_filter &&= !(status.account_id == status.in_reply_to_account_id) # and it's not a self-reply should_filter &&= !(status.account_id == status.in_reply_to_account_id) # and it's not a self-reply
return should_filter
elsif status.reblog? # Filter out a reblog elsif status.reblog? # Filter out a reblog
should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person return status.reblog.account.blocking?(receiver) # or if the author of the reblogged status is blocking me
should_filter ||= receiver.muting?(status.reblog.account) # or muting that person
should_filter ||= status.reblog.account.blocking?(receiver) # or if the author of the reblogged status is blocking me
end end
should_filter ||= receiver.blocking?(status.mentions.map(&:account_id)) # or if it mentions someone I blocked false
should_filter
end end
def filter_from_mentions?(status, receiver) def filter_from_mentions?(status, receiver)
should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself check_for_blocks = [status.account_id]
should_filter ||= receiver.blocking?(status.account) # or it's from someone I blocked check_for_blocks.concat(status.mentions.select('account_id').map(&:account_id))
should_filter ||= receiver.blocking?(status.mentions.includes(:account).map(&:account)) # or if it mentions someone I blocked check_for_blocks.concat([status.in_reply_to_account]) if status.reply? && !status.in_reply_to_account_id.nil?
should_filter ||= (status.account.silenced? && !receiver.following?(status.account)) # of if the account is silenced and I'm not following them
if status.reply? && !status.in_reply_to_account_id.nil? # or it's a reply should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
should_filter ||= receiver.blocking?(status.in_reply_to_account) # to a user I blocked should_filter ||= receiver.blocking?(check_for_blocks) # or it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked
end should_filter ||= (status.account.silenced? && !receiver.following?(status.account)) # of if the account is silenced and I'm not following them
should_filter should_filter
end end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
class InlineRablScope
include RoutingHelper
def initialize(account)
@account = account
end
def current_user
@account.try(:user)
end
def current_account
@account
end
end