From 5ee83a680bc133d69b56784a86bfcd4304132463 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 12 Aug 2025 04:15:22 -0400 Subject: [PATCH] Update stoplight to version 5.3.1 (#35129) --- Gemfile | 2 +- Gemfile.lock | 8 +++---- .../concerns/signature_verification.rb | 12 ++++++---- app/services/bulk_import_row_service.rb | 22 +++++++++++++------ app/workers/activitypub/delivery_worker.rb | 10 +++++---- config/initializers/stoplight.rb | 6 +++-- lib/paperclip/attachment_extensions.rb | 13 ++++++----- 7 files changed, 44 insertions(+), 29 deletions(-) diff --git a/Gemfile b/Gemfile index 57ea00cc8..c5d6f5582 100644 --- a/Gemfile +++ b/Gemfile @@ -88,7 +88,7 @@ gem 'sidekiq-scheduler', '~> 6.0' gem 'sidekiq-unique-jobs', '> 8' gem 'simple_form', '~> 5.2' gem 'simple-navigation', '~> 4.4' -gem 'stoplight', '~> 4.1' +gem 'stoplight' gem 'strong_migrations' gem 'tty-prompt', '~> 0.23', require: false gem 'twitter-text', '~> 3.1.0' diff --git a/Gemfile.lock b/Gemfile.lock index db58971b6..077ed68f5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -719,8 +719,6 @@ GEM redis (4.8.1) redis-client (0.25.2) connection_pool - redlock (1.3.2) - redis (>= 3.0.0, < 6.0) regexp_parser (2.11.0) reline (0.6.2) io-console (~> 0.5) @@ -855,8 +853,8 @@ GEM stackprof (0.2.27) starry (0.2.0) base64 - stoplight (4.1.1) - redlock (~> 1.0) + stoplight (5.3.1) + zeitwerk stringio (3.1.7) strong_migrations (2.5.0) activerecord (>= 7.1) @@ -1088,7 +1086,7 @@ DEPENDENCIES simplecov (~> 0.22) simplecov-lcov (~> 0.8) stackprof - stoplight (~> 4.1) + stoplight strong_migrations test-prof thor (~> 1.2) diff --git a/app/controllers/concerns/signature_verification.rb b/app/controllers/concerns/signature_verification.rb index b61a56986..2bdd35586 100644 --- a/app/controllers/concerns/signature_verification.rb +++ b/app/controllers/concerns/signature_verification.rb @@ -9,6 +9,8 @@ module SignatureVerification EXPIRATION_WINDOW_LIMIT = 12.hours CLOCK_SKEW_MARGIN = 1.hour + STOPLIGHT_COOL_OFF_TIME = 5.minutes.seconds + STOPLIGHT_THRESHOLD = 1 def require_account_signature! render json: signature_verification_failure_reason, status: signature_verification_failure_code unless signed_request_account @@ -107,10 +109,12 @@ module SignatureVerification end def stoplight_wrapper - Stoplight("source:#{request.remote_ip}") - .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) } + Stoplight( + "source:#{request.remote_ip}", + cool_off_time: STOPLIGHT_COOL_OFF_TIME, + threshold: STOPLIGHT_THRESHOLD, + tracked_errors: [HTTP::Error, OpenSSL::SSL::SSLError] + ) end def actor_refresh_key!(actor) diff --git a/app/services/bulk_import_row_service.rb b/app/services/bulk_import_row_service.rb index 26909dfe0..ac5080f0b 100644 --- a/app/services/bulk_import_row_service.rb +++ b/app/services/bulk_import_row_service.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true class BulkImportRowService + STOPLIGHT_COOL_OFF_TIME = 5.minutes.seconds + STOPLIGHT_THRESHOLD = 1 + def call(row) @account = row.bulk_import.account @data = row.data @@ -10,7 +13,7 @@ class BulkImportRowService when :following, :blocking, :muting, :lists target_acct = @data['acct'] target_domain = domain(target_acct) - @target_account = stoplight_wrapper(target_domain).run { ResolveAccountService.new.call(target_acct, { check_delivery_availability: true }) } + @target_account = stoplight_wrapper(target_domain).run(stoplight_fallback) { ResolveAccountService.new.call(target_acct, { check_delivery_availability: true }) } return false if @target_account.nil? when :bookmarks target_uri = @data['uri'] @@ -18,7 +21,7 @@ class BulkImportRowService @target_status = ActivityPub::TagManager.instance.uri_to_resource(target_uri, Status) return false if @target_status.nil? && ActivityPub::TagManager.instance.local_uri?(target_uri) - @target_status ||= stoplight_wrapper(target_domain).run { ActivityPub::FetchRemoteStatusService.new.call(target_uri) } + @target_status ||= stoplight_wrapper(target_domain).run(stoplight_fallback) { ActivityPub::FetchRemoteStatusService.new.call(target_uri) } return false if @target_status.nil? end @@ -51,13 +54,18 @@ class BulkImportRowService TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain) end + def stoplight_fallback + ->(error) {} + end + def stoplight_wrapper(domain) if domain.present? - Stoplight("source:#{domain}") - .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) } + Stoplight( + "source:#{domain}", + cool_off_time: STOPLIGHT_COOL_OFF_TIME, + threshold: STOPLIGHT_THRESHOLD, + tracked_errors: [HTTP::Error, OpenSSL::SSL::SSLError] + ) else Stoplight('domain-blank') end diff --git a/app/workers/activitypub/delivery_worker.rb b/app/workers/activitypub/delivery_worker.rb index 7a1440ed1..40b5c4240 100644 --- a/app/workers/activitypub/delivery_worker.rb +++ b/app/workers/activitypub/delivery_worker.rb @@ -5,8 +5,8 @@ class ActivityPub::DeliveryWorker include RoutingHelper include JsonLdHelper + STOPLIGHT_COOL_OFF_TIME = 60 STOPLIGHT_FAILURE_THRESHOLD = 10 - STOPLIGHT_COOLDOWN = 60 sidekiq_options queue: 'push', retry: 16, dead: false @@ -75,9 +75,11 @@ class ActivityPub::DeliveryWorker end def stoplight_wrapper - Stoplight(@inbox_url) - .with_threshold(STOPLIGHT_FAILURE_THRESHOLD) - .with_cool_off_time(STOPLIGHT_COOLDOWN) + Stoplight( + @inbox_url, + cool_off_time: STOPLIGHT_COOL_OFF_TIME, + threshold: STOPLIGHT_FAILURE_THRESHOLD + ) end def failure_tracker diff --git a/config/initializers/stoplight.rb b/config/initializers/stoplight.rb index 0ade504f6..c92a46930 100644 --- a/config/initializers/stoplight.rb +++ b/config/initializers/stoplight.rb @@ -3,6 +3,8 @@ require 'stoplight' Rails.application.reloader.to_prepare do - Stoplight.default_data_store = Stoplight::DataStore::Redis.new(RedisConnection.new.connection) - Stoplight.default_notifiers = [Stoplight::Notifier::Logger.new(Rails.logger)] + Stoplight.configure do |config| + config.data_store = Stoplight::DataStore::Redis.new(RedisConnection.new.connection) + config.notifiers = [Stoplight::Notifier::Logger.new(Rails.logger)] + end end diff --git a/lib/paperclip/attachment_extensions.rb b/lib/paperclip/attachment_extensions.rb index 401da1129..011e165ed 100644 --- a/lib/paperclip/attachment_extensions.rb +++ b/lib/paperclip/attachment_extensions.rb @@ -73,8 +73,8 @@ module Paperclip @url_generator.for_as_default(style_name) end + STOPLIGHT_COOL_OFF_TIME = 30 STOPLIGHT_THRESHOLD = 10 - STOPLIGHT_COOLDOWN = 30 # We overwrite this method to put a circuit breaker around # calls to object storage, to stop hitting APIs that are slow @@ -84,11 +84,12 @@ module Paperclip # Don't go through Stoplight if we don't have anything object-storage-oriented to do return super if @queued_for_delete.empty? && @queued_for_write.empty? && !dirty? - Stoplight('object-storage') - .with_threshold(STOPLIGHT_THRESHOLD) - .with_cool_off_time(STOPLIGHT_COOLDOWN) - .with_error_handler { |error, handle| error.is_a?(Seahorse::Client::NetworkingError) ? handle.call(error) : raise(error) } - .run { super } + Stoplight( + 'object-storage', + cool_off_time: STOPLIGHT_COOL_OFF_TIME, + threshold: STOPLIGHT_THRESHOLD, + tracked_errors: [Seahorse::Client::NetworkingError] + ).run { super } end end end