diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-03-08 20:16:11 +0100 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-03-08 20:20:45 +0100 |
commit | 6c4c84b161947cb11ad0451a39e26b25be4c93d5 (patch) | |
tree | fa2a6f4aaff71fcf76c745a57cb7732102814871 /app/services/fan_out_on_write_service.rb | |
parent | fe57f6330f089d023f0fa4db7f7c8a51551d2ee9 (diff) |
Distrubute statuses as a fan-out-on-write system, with optional precomputing
Diffstat (limited to 'app/services/fan_out_on_write_service.rb')
-rw-r--r-- | app/services/fan_out_on_write_service.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb new file mode 100644 index 000000000..87a7c55ac --- /dev/null +++ b/app/services/fan_out_on_write_service.rb @@ -0,0 +1,46 @@ +class FanOutOnWriteService < BaseService + MAX_FEED_SIZE = 800 + + # Push a status into home and mentions feeds + # @param [Status] status + def call(status) + replied_to_user = status.reply? ? status.thread.account : nil + + # Deliver to local self + push(:home, status.account.id, status) if status.account.local? + + # Deliver to local followers + status.account.followers.each do |follower| + next if (status.reply? && !follower.following?(replied_to_user)) || !follower.local? + push(:home, follower.id, status) + end + + # Deliver to local mentioned + status.mentions.each do |mentioned_account| + next unless mentioned_account.local? + push(:mentions, mentioned_account.id, status) + end + end + + private + + def push(type, receiver_id, status) + redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id) + trim(type, receiver_id) + end + + def trim(type, receiver_id) + return unless redis.zcard(key(type, receiver_id)) > MAX_FEED_SIZE + + last = redis.zrevrange(key(type, receiver_id), MAX_FEED_SIZE - 1, MAX_FEED_SIZE - 1) + redis.zremrangebyscore(key(type, receiver_id), '-inf', "(#{last.last}") + end + + def key(type, id) + "feed:#{type}:#{id}" + end + + def redis + $redis + end +end |