Experimental Async Refreshes API (#34918)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::Timelines::HomeController < Api::V1::Timelines::BaseController
|
||||
include AsyncRefreshesConcern
|
||||
|
||||
before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, only: [:show]
|
||||
before_action :require_user!, only: [:show]
|
||||
|
||||
@@ -12,6 +14,8 @@ class Api::V1::Timelines::HomeController < Api::V1::Timelines::BaseController
|
||||
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
add_async_refresh_header(account_home_feed.async_refresh, retry_seconds: 5)
|
||||
|
||||
render json: @statuses,
|
||||
each_serializer: REST::StatusSerializer,
|
||||
relationships: @relationships,
|
||||
|
||||
16
app/controllers/api/v1_alpha/async_refreshes_controller.rb
Normal file
16
app/controllers/api/v1_alpha/async_refreshes_controller.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1Alpha::AsyncRefreshesController < Api::BaseController
|
||||
before_action -> { doorkeeper_authorize! :read }
|
||||
before_action :require_user!
|
||||
|
||||
def show
|
||||
async_refresh = AsyncRefresh.find(params[:id])
|
||||
|
||||
if async_refresh
|
||||
render json: async_refresh
|
||||
else
|
||||
not_found
|
||||
end
|
||||
end
|
||||
end
|
||||
11
app/controllers/concerns/async_refreshes_concern.rb
Normal file
11
app/controllers/concerns/async_refreshes_concern.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module AsyncRefreshesConcern
|
||||
private
|
||||
|
||||
def add_async_refresh_header(async_refresh, retry_seconds: 3)
|
||||
return unless async_refresh.running?
|
||||
|
||||
response.headers['Mastodon-Async-Refresh'] = "id=\"#{async_refresh.id}\", retry=#{retry_seconds}"
|
||||
end
|
||||
end
|
||||
76
app/models/async_refresh.rb
Normal file
76
app/models/async_refresh.rb
Normal file
@@ -0,0 +1,76 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AsyncRefresh
|
||||
extend Redisable
|
||||
include Redisable
|
||||
|
||||
NEW_REFRESH_EXPIRATION = 1.day
|
||||
FINISHED_REFRESH_EXPIRATION = 1.hour
|
||||
|
||||
def self.find(id)
|
||||
redis_key = Rails.application.message_verifier('async_refreshes').verify(id)
|
||||
new(redis_key) if redis.exists?(redis_key)
|
||||
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
||||
nil
|
||||
end
|
||||
|
||||
def self.create(redis_key, count_results: false)
|
||||
data = { 'status' => 'running' }
|
||||
data['result_count'] = 0 if count_results
|
||||
redis.hset(redis_key, data)
|
||||
redis.expire(redis_key, NEW_REFRESH_EXPIRATION)
|
||||
new(redis_key)
|
||||
end
|
||||
|
||||
attr_reader :status, :result_count
|
||||
|
||||
def initialize(redis_key)
|
||||
@redis_key = redis_key
|
||||
fetch_data_from_redis
|
||||
end
|
||||
|
||||
def id
|
||||
Rails.application.message_verifier('async_refreshes').generate(@redis_key)
|
||||
end
|
||||
|
||||
def running?
|
||||
@status == 'running'
|
||||
end
|
||||
|
||||
def finished?
|
||||
@status == 'finished'
|
||||
end
|
||||
|
||||
def finish!
|
||||
redis.pipelined do |pipeline|
|
||||
pipeline.hset(@redis_key, { 'status' => 'finished' })
|
||||
pipeline.expire(@redis_key, FINISHED_REFRESH_EXPIRATION)
|
||||
end
|
||||
@status = 'finished'
|
||||
end
|
||||
|
||||
def reload
|
||||
fetch_data_from_redis
|
||||
self
|
||||
end
|
||||
|
||||
def to_json(_options)
|
||||
{
|
||||
async_refresh: {
|
||||
id:,
|
||||
status:,
|
||||
result_count:,
|
||||
},
|
||||
}.to_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_data_from_redis
|
||||
@status, @result_count = redis.pipelined do |pipeline|
|
||||
pipeline.hget(@redis_key, 'status')
|
||||
pipeline.hget(@redis_key, 'result_count')
|
||||
end
|
||||
@result_count = @result_count.presence&.to_i
|
||||
end
|
||||
end
|
||||
@@ -6,15 +6,39 @@ class HomeFeed < Feed
|
||||
super(:home, account.id)
|
||||
end
|
||||
|
||||
def async_refresh
|
||||
@async_refresh ||= AsyncRefresh.new(redis_regeneration_key)
|
||||
end
|
||||
|
||||
def regenerating?
|
||||
redis.exists?("account:#{@account.id}:regeneration")
|
||||
async_refresh.running?
|
||||
rescue Redis::CommandError
|
||||
retry if upgrade_redis_key!
|
||||
end
|
||||
|
||||
def regeneration_in_progress!
|
||||
redis.set("account:#{@account.id}:regeneration", true, nx: true, ex: 1.day.seconds)
|
||||
@async_refresh = AsyncRefresh.create(redis_regeneration_key)
|
||||
rescue Redis::CommandError
|
||||
upgrade_redis_key!
|
||||
end
|
||||
|
||||
def regeneration_finished!
|
||||
redis.del("account:#{@account.id}:regeneration")
|
||||
async_refresh.finish!
|
||||
rescue Redis::CommandError
|
||||
retry if upgrade_redis_key!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def redis_regeneration_key
|
||||
@redis_regeneration_key = "account:#{@account.id}:regeneration"
|
||||
end
|
||||
|
||||
def upgrade_redis_key!
|
||||
if redis.type(redis_regeneration_key) == 'string'
|
||||
redis.del(redis_regeneration_key)
|
||||
regeneration_in_progress!
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user