Skip to content

Commit

Permalink
Fix performance of follow import (mastodon#13836)
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo authored and Mage committed Jan 14, 2022
1 parent 2252fdb commit 13ed915
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
19 changes: 19 additions & 0 deletions app/helpers/webfinger_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# frozen_string_literal: true

# Monkey-patch on monkey-patch.
# Because it conflicts with the request.rb patch.
class HTTP::Timeout::PerOperationOriginal < HTTP::Timeout::PerOperation
def connect(socket_class, host, port, nodelay = false)
::Timeout.timeout(@connect_timeout, HTTP::TimeoutError) do
@socket = socket_class.open(host, port)
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
end
end
end

module WebfingerHelper
def webfinger!(uri)
hidden_service_uri = /\.(onion|i2p)(:\d+)?$/.match(uri)
Expand All @@ -12,6 +23,14 @@ def webfinger!(uri)
headers: {
'User-Agent': Mastodon::Version.user_agent,
},

timeout_class: HTTP::Timeout::PerOperationOriginal,

timeout_options: {
write_timeout: 10,
connect_timeout: 5,
read_timeout: 10,
},
}

Goldfinger::Client.new(uri, opts.merge(Rails.configuration.x.http_client_proxy)).finger
Expand Down
4 changes: 3 additions & 1 deletion app/services/import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def import_relationships!(action, undo_action, overwrite_scope, limit, extra_fie
end
end

Import::RelationshipWorker.push_bulk(items) do |acct, extra|
head_items = items.uniq { |acct, _| acct.split('@')[1] }
tail_items = items - head_items
Import::RelationshipWorker.push_bulk(head_items + tail_items) do |acct, extra|
[@account.id, acct, action, extra]
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/services/resolve_account_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def process_account!
end

def webfinger_update_due?
return false if @options[:check_delivery_availability] && !DeliveryFailureTracker.available?(@domain)

@account.nil? || ((!@options[:skip_webfinger] || @account.ostatus?) && @account.possibly_stale?)
end

Expand Down
21 changes: 20 additions & 1 deletion app/workers/import/relationship_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Import::RelationshipWorker

def perform(account_id, target_account_uri, relationship, options = {})
from_account = Account.find(account_id)
target_account = ResolveAccountService.new.call(target_account_uri)
target_domain = domain(target_account_uri)
target_account = stoplight_wrap_request(target_domain) { ResolveAccountService.new.call(target_account_uri, { check_delivery_availability: true }) }
options.symbolize_keys!

return if target_account.nil?
Expand All @@ -29,4 +30,22 @@ def perform(account_id, target_account_uri, relationship, options = {})
rescue ActiveRecord::RecordNotFound
true
end

def domain(uri)
domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
end

def stoplight_wrap_request(domain, &block)
if domain.present?
Stoplight("source:#{domain}", &block)
.with_fallback { nil }
.with_threshold(1)
.with_cool_off_time(5.minutes.seconds)
.with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
.run
else
block.call
end
end
end

0 comments on commit 13ed915

Please sign in to comment.