Skip to content

Commit 5ee83a6

Browse files
authored
Update stoplight to version 5.3.1 (mastodon#35129)
1 parent d9d7914 commit 5ee83a6

7 files changed

Lines changed: 44 additions & 29 deletions

File tree

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ gem 'sidekiq-scheduler', '~> 6.0'
8888
gem 'sidekiq-unique-jobs', '> 8'
8989
gem 'simple_form', '~> 5.2'
9090
gem 'simple-navigation', '~> 4.4'
91-
gem 'stoplight', '~> 4.1'
91+
gem 'stoplight'
9292
gem 'strong_migrations'
9393
gem 'tty-prompt', '~> 0.23', require: false
9494
gem 'twitter-text', '~> 3.1.0'

Gemfile.lock

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,6 @@ GEM
719719
redis (4.8.1)
720720
redis-client (0.25.2)
721721
connection_pool
722-
redlock (1.3.2)
723-
redis (>= 3.0.0, < 6.0)
724722
regexp_parser (2.11.0)
725723
reline (0.6.2)
726724
io-console (~> 0.5)
@@ -855,8 +853,8 @@ GEM
855853
stackprof (0.2.27)
856854
starry (0.2.0)
857855
base64
858-
stoplight (4.1.1)
859-
redlock (~> 1.0)
856+
stoplight (5.3.1)
857+
zeitwerk
860858
stringio (3.1.7)
861859
strong_migrations (2.5.0)
862860
activerecord (>= 7.1)
@@ -1088,7 +1086,7 @@ DEPENDENCIES
10881086
simplecov (~> 0.22)
10891087
simplecov-lcov (~> 0.8)
10901088
stackprof
1091-
stoplight (~> 4.1)
1089+
stoplight
10921090
strong_migrations
10931091
test-prof
10941092
thor (~> 1.2)

app/controllers/concerns/signature_verification.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module SignatureVerification
99

1010
EXPIRATION_WINDOW_LIMIT = 12.hours
1111
CLOCK_SKEW_MARGIN = 1.hour
12+
STOPLIGHT_COOL_OFF_TIME = 5.minutes.seconds
13+
STOPLIGHT_THRESHOLD = 1
1214

1315
def require_account_signature!
1416
render json: signature_verification_failure_reason, status: signature_verification_failure_code unless signed_request_account
@@ -107,10 +109,12 @@ def actor_from_key_id
107109
end
108110

109111
def stoplight_wrapper
110-
Stoplight("source:#{request.remote_ip}")
111-
.with_threshold(1)
112-
.with_cool_off_time(5.minutes.seconds)
113-
.with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
112+
Stoplight(
113+
"source:#{request.remote_ip}",
114+
cool_off_time: STOPLIGHT_COOL_OFF_TIME,
115+
threshold: STOPLIGHT_THRESHOLD,
116+
tracked_errors: [HTTP::Error, OpenSSL::SSL::SSLError]
117+
)
114118
end
115119

116120
def actor_refresh_key!(actor)

app/services/bulk_import_row_service.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# frozen_string_literal: true
22

33
class BulkImportRowService
4+
STOPLIGHT_COOL_OFF_TIME = 5.minutes.seconds
5+
STOPLIGHT_THRESHOLD = 1
6+
47
def call(row)
58
@account = row.bulk_import.account
69
@data = row.data
@@ -10,15 +13,15 @@ def call(row)
1013
when :following, :blocking, :muting, :lists
1114
target_acct = @data['acct']
1215
target_domain = domain(target_acct)
13-
@target_account = stoplight_wrapper(target_domain).run { ResolveAccountService.new.call(target_acct, { check_delivery_availability: true }) }
16+
@target_account = stoplight_wrapper(target_domain).run(stoplight_fallback) { ResolveAccountService.new.call(target_acct, { check_delivery_availability: true }) }
1417
return false if @target_account.nil?
1518
when :bookmarks
1619
target_uri = @data['uri']
1720
target_domain = Addressable::URI.parse(target_uri).normalized_host
1821
@target_status = ActivityPub::TagManager.instance.uri_to_resource(target_uri, Status)
1922
return false if @target_status.nil? && ActivityPub::TagManager.instance.local_uri?(target_uri)
2023

21-
@target_status ||= stoplight_wrapper(target_domain).run { ActivityPub::FetchRemoteStatusService.new.call(target_uri) }
24+
@target_status ||= stoplight_wrapper(target_domain).run(stoplight_fallback) { ActivityPub::FetchRemoteStatusService.new.call(target_uri) }
2225
return false if @target_status.nil?
2326
end
2427

@@ -51,13 +54,18 @@ def domain(uri)
5154
TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
5255
end
5356

57+
def stoplight_fallback
58+
->(error) {}
59+
end
60+
5461
def stoplight_wrapper(domain)
5562
if domain.present?
56-
Stoplight("source:#{domain}")
57-
.with_fallback { nil }
58-
.with_threshold(1)
59-
.with_cool_off_time(5.minutes.seconds)
60-
.with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
63+
Stoplight(
64+
"source:#{domain}",
65+
cool_off_time: STOPLIGHT_COOL_OFF_TIME,
66+
threshold: STOPLIGHT_THRESHOLD,
67+
tracked_errors: [HTTP::Error, OpenSSL::SSL::SSLError]
68+
)
6169
else
6270
Stoplight('domain-blank')
6371
end

app/workers/activitypub/delivery_worker.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class ActivityPub::DeliveryWorker
55
include RoutingHelper
66
include JsonLdHelper
77

8+
STOPLIGHT_COOL_OFF_TIME = 60
89
STOPLIGHT_FAILURE_THRESHOLD = 10
9-
STOPLIGHT_COOLDOWN = 60
1010

1111
sidekiq_options queue: 'push', retry: 16, dead: false
1212

@@ -75,9 +75,11 @@ def unsalvageable_authorization_failure?(response)
7575
end
7676

7777
def stoplight_wrapper
78-
Stoplight(@inbox_url)
79-
.with_threshold(STOPLIGHT_FAILURE_THRESHOLD)
80-
.with_cool_off_time(STOPLIGHT_COOLDOWN)
78+
Stoplight(
79+
@inbox_url,
80+
cool_off_time: STOPLIGHT_COOL_OFF_TIME,
81+
threshold: STOPLIGHT_FAILURE_THRESHOLD
82+
)
8183
end
8284

8385
def failure_tracker

config/initializers/stoplight.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'stoplight'
44

55
Rails.application.reloader.to_prepare do
6-
Stoplight.default_data_store = Stoplight::DataStore::Redis.new(RedisConnection.new.connection)
7-
Stoplight.default_notifiers = [Stoplight::Notifier::Logger.new(Rails.logger)]
6+
Stoplight.configure do |config|
7+
config.data_store = Stoplight::DataStore::Redis.new(RedisConnection.new.connection)
8+
config.notifiers = [Stoplight::Notifier::Logger.new(Rails.logger)]
9+
end
810
end

lib/paperclip/attachment_extensions.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def default_url(style_name = default_style)
7373
@url_generator.for_as_default(style_name)
7474
end
7575

76+
STOPLIGHT_COOL_OFF_TIME = 30
7677
STOPLIGHT_THRESHOLD = 10
77-
STOPLIGHT_COOLDOWN = 30
7878

7979
# We overwrite this method to put a circuit breaker around
8080
# calls to object storage, to stop hitting APIs that are slow
@@ -84,11 +84,12 @@ def save
8484
# Don't go through Stoplight if we don't have anything object-storage-oriented to do
8585
return super if @queued_for_delete.empty? && @queued_for_write.empty? && !dirty?
8686

87-
Stoplight('object-storage')
88-
.with_threshold(STOPLIGHT_THRESHOLD)
89-
.with_cool_off_time(STOPLIGHT_COOLDOWN)
90-
.with_error_handler { |error, handle| error.is_a?(Seahorse::Client::NetworkingError) ? handle.call(error) : raise(error) }
91-
.run { super }
87+
Stoplight(
88+
'object-storage',
89+
cool_off_time: STOPLIGHT_COOL_OFF_TIME,
90+
threshold: STOPLIGHT_THRESHOLD,
91+
tracked_errors: [Seahorse::Client::NetworkingError]
92+
).run { super }
9293
end
9394
end
9495
end

0 commit comments

Comments
 (0)