Skip to content

Commit

Permalink
Reduce number of items in feeds, optimize regeneration worker slightly,
Browse files Browse the repository at this point in the history
make regeneration worker unique, (only schedule/execute once at a time)
  • Loading branch information
Gargron committed Apr 4, 2017
1 parent b1f3499 commit 82aaede
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ gem 'will_paginate'
gem 'rack-attack'
gem 'rack-cors', require: 'rack/cors'
gem 'sidekiq'
gem 'sidekiq-unique-jobs'
gem 'sidekiq-merger'
gem 'rails-settings-cached'
gem 'simple-navigation'
gem 'statsd-instrument'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ GEM
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (~> 3.2, >= 3.2.1)
sidekiq-merger (0.0.11)
activesupport (>= 3.2, < 6)
concurrent-ruby (~> 1.0)
sidekiq (>= 3.4, < 5)
sidekiq-unique-jobs (4.0.18)
sidekiq (>= 2.6)
thor
simple-navigation (4.0.3)
activesupport (>= 2.3.2)
simple_form (3.2.1)
Expand Down Expand Up @@ -510,6 +517,8 @@ DEPENDENCIES
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
sidekiq
sidekiq-merger
sidekiq-unique-jobs
simple-navigation
simple_form
simplecov
Expand Down
6 changes: 3 additions & 3 deletions app/lib/feed_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class FeedManager
include Singleton

MAX_ITEMS = 800
MAX_ITEMS = 400

def key(type, id)
"feed:#{type}:#{id}"
Expand Down Expand Up @@ -50,9 +50,9 @@ def trim(type, account_id)

def merge_into_timeline(from_account, into_account)
timeline_key = key(:home, into_account.id)
query = from_account.statuses.limit(MAX_ITEMS)
query = from_account.statuses.limit(FeedManager::MAX_ITEMS / 4)

if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS
if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
query = query.where('id > ?', oldest_home_score)
end
Expand Down
8 changes: 5 additions & 3 deletions app/services/precompute_feed_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ class PrecomputeFeedService < BaseService
# @param [Symbol] type :home or :mentions
# @param [Account] account
def call(_, account)
Status.as_home_timeline(account).limit(FeedManager::MAX_ITEMS).each do |status|
next if status.direct_visibility? || FeedManager.instance.filter?(:home, status, account)
redis.zadd(FeedManager.instance.key(:home, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
redis.pipelined do
Status.as_home_timeline(account).limit(FeedManager::MAX_ITEMS / 4).each do |status|
next if status.direct_visibility? || FeedManager.instance.filter?(:home, status, account)
redis.zadd(FeedManager.instance.key(:home, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/workers/regeneration_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class RegenerationWorker
include Sidekiq::Worker

sidekiq_options queue: 'pull', backtrace: true
sidekiq_options queue: 'pull', backtrace: true, unique: :until_executed

def perform(account_id, _ = :home)
PrecomputeFeedService.new.call(:home, Account.find(account_id))
Expand Down

0 comments on commit 82aaede

Please sign in to comment.