]> cat aescling's git repositories - mastodon.git/commitdiff
Fix some timeouts when searching URLs by limiting some database queries (#13253)
authorThibG <thib@sitedethib.com>
Thu, 12 Mar 2020 22:06:43 +0000 (23:06 +0100)
committerGitHub <noreply@github.com>
Thu, 12 Mar 2020 22:06:43 +0000 (23:06 +0100)
Only look up private toots from database if the request failed because of 401,
403 or 404 errors, as those may indicate a private toot, rather than something
that isn't a toot or cannot be processed.

app/services/fetch_resource_service.rb
app/services/resolve_url_service.rb

index abe7766d4f74681fef466b2f52b25ac32bddc6b8..880cdde92244362af68c45eb30947dd41d185547 100644 (file)
@@ -5,6 +5,8 @@ class FetchResourceService < BaseService
 
   ACCEPT_HEADER = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams", text/html;q=0.1'
 
+  attr_reader :response_code
+
   def call(url)
     return if url.blank?
 
@@ -27,6 +29,7 @@ class FetchResourceService < BaseService
   end
 
   def process_response(response, terminal = false)
+    @response_code = response.code
     return nil if response.code != 200
 
     if ['application/activity+json', 'application/ld+json'].include?(response.mime_type)
index 1a2b0d60cbc61c4eba037b2d903cbbe8d67a2a48..78080d878f9dba5f238ceb0044331952c9305ec9 100644 (file)
@@ -12,7 +12,7 @@ class ResolveURLService < BaseService
       process_local_url
     elsif !fetched_resource.nil?
       process_url
-    elsif @on_behalf_of.present?
+    else
       process_url_from_db
     end
   end
@@ -30,6 +30,8 @@ class ResolveURLService < BaseService
   end
 
   def process_url_from_db
+    return unless @on_behalf_of.present? && [401, 403, 404].include?(fetch_resource_service.response_code)
+
     # It may happen that the resource is a private toot, and thus not fetchable,
     # but we can return the toot if we already know about it.
     status = Status.find_by(uri: @url) || Status.find_by(url: @url)
@@ -40,7 +42,11 @@ class ResolveURLService < BaseService
   end
 
   def fetched_resource
-    @fetched_resource ||= FetchResourceService.new.call(@url)
+    @fetched_resource ||= fetch_resource_service.call(@url)
+  end
+
+  def fetch_resource_service
+    @_fetch_resource_service ||= FetchResourceService.new
   end
 
   def resource_url