diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index 6c24c5da8d..64defd31c3 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AccountSearchService < BaseService + include SearchStoplight + attr_reader :query, :limit, :offset, :options, :account MENTION_ONLY_RE = /\A#{Account::MENTION_RE}\z/i @@ -251,12 +253,12 @@ class AccountSearchService < BaseService end end - records = query_builder.build.limit(limit_for_non_exact_results).offset(offset).objects.compact + records = elastic_stoplight_wrapper.run { query_builder.build.limit(limit_for_non_exact_results).offset(offset).objects.compact } ActiveRecord::Associations::Preloader.new(records: records, associations: [:account_stat, { user: :role }]).call records - rescue Faraday::ConnectionFailed, Parslet::ParseFailed, Errno::ENETUNREACH + rescue Stoplight::Error::RedLight, Faraday::ConnectionFailed, Parslet::ParseFailed, Errno::ENETUNREACH, OpenSSL::SSL::SSLError, Elastic::Transport::Transport::Error nil end diff --git a/app/services/concerns/search_stoplight.rb b/app/services/concerns/search_stoplight.rb new file mode 100644 index 0000000000..c7f12a3bec --- /dev/null +++ b/app/services/concerns/search_stoplight.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module SearchStoplight + STOPLIGHT_COOL_OFF_TIME = 5.minutes.seconds + STOPLIGHT_THRESHOLD = 10 + + def elastic_stoplight_wrapper + Stoplight( + 'search:elasticsearch', + cool_off_time: STOPLIGHT_COOL_OFF_TIME, + threshold: STOPLIGHT_THRESHOLD, + tracked_errors: [Faraday::ConnectionFailed, Errno::ENETUNREACH, OpenSSL::SSL::SSLError, Elastic::Transport::Transport::Error] + ) + end +end diff --git a/app/services/statuses_search_service.rb b/app/services/statuses_search_service.rb index 3147349d70..8eda1a1430 100644 --- a/app/services/statuses_search_service.rb +++ b/app/services/statuses_search_service.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class StatusesSearchService < BaseService + include SearchStoplight + def call(query, account = nil, options = {}) MastodonOTELTracer.in_span('StatusesSearchService#call') do |span| @query = query&.strip @@ -26,14 +28,14 @@ class StatusesSearchService < BaseService def status_search_results request = parsed_query.request - results = request.collapse(field: :id).order(id: { order: :desc }).limit(@limit).offset(@offset).objects.compact + results = elastic_stoplight_wrapper.run { request.collapse(field: :id).order(id: { order: :desc }).limit(@limit).offset(@offset).objects.compact } account_ids = results.map(&:account_id) account_domains = results.map(&:account_domain) @account.preload_relations!(account_ids, account_domains) results.reject { |status| StatusFilter.new(status, @account).filtered? } - rescue Faraday::ConnectionFailed, Parslet::ParseFailed, Errno::ENETUNREACH + rescue Stoplight::Error::RedLight, Faraday::ConnectionFailed, Parslet::ParseFailed, Errno::ENETUNREACH, OpenSSL::SSL::SSLError, Elastic::Transport::Transport::Error [] end