mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-14 10:54:27 +00:00
cbd0ee1d07
* Update devise-two-factor to unreleased fork for Rails 6 support Update tests to match new `rotp` version. * Update nsa gem to unreleased fork for Rails 6 support * Update rails to 6.1.3 and rails-i18n to 6.0 * Update to unreleased fork of pluck_each for Ruby 6 support * Run "rails app:update" * Add missing ActiveStorage config file * Use config.ssl_options instead of removed ApplicationController#force_ssl Disabled force_ssl-related tests as they do not seem to be easily testable anymore. * Fix nonce directives by removing Rails 5 specific monkey-patching * Fix fixture_file_upload deprecation warning * Fix yield-based test failing with Rails 6 * Use Rails 6's index_with when possible * Use ActiveRecord::Cache::Store#delete_multi from Rails 6 This will yield better performances when deleting an account * Disable Rails 6.1's automatic preload link headers Since Rails 6.1, ActionView adds preload links for javascript files in the Links header per default. In our case, that will bloat headers too much and potentially cause issues with reverse proxies. Furhermore, we don't need those links, as we already output them as HTML link tags. * Switch to Rails 6.0 default config * Switch to Rails 6.1 default config * Do not include autoload paths in the load path
63 lines
1.4 KiB
Ruby
63 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class DeliveryFailureTracker
|
|
FAILURE_DAYS_THRESHOLD = 7
|
|
|
|
def initialize(url_or_host)
|
|
@host = url_or_host.start_with?('https://') || url_or_host.start_with?('http://') ? Addressable::URI.parse(url_or_host).normalized_host : url_or_host
|
|
end
|
|
|
|
def track_failure!
|
|
Redis.current.sadd(exhausted_deliveries_key, today)
|
|
UnavailableDomain.create(domain: @host) if reached_failure_threshold?
|
|
end
|
|
|
|
def track_success!
|
|
Redis.current.del(exhausted_deliveries_key)
|
|
UnavailableDomain.find_by(domain: @host)&.destroy
|
|
end
|
|
|
|
def days
|
|
Redis.current.scard(exhausted_deliveries_key) || 0
|
|
end
|
|
|
|
def available?
|
|
!UnavailableDomain.where(domain: @host).exists?
|
|
end
|
|
|
|
alias reset! track_success!
|
|
|
|
class << self
|
|
def without_unavailable(urls)
|
|
unavailable_domains_map = Rails.cache.fetch('unavailable_domains') { UnavailableDomain.pluck(:domain).index_with(true) }
|
|
|
|
urls.reject do |url|
|
|
host = Addressable::URI.parse(url).normalized_host
|
|
unavailable_domains_map[host]
|
|
end
|
|
end
|
|
|
|
def available?(url)
|
|
new(url).available?
|
|
end
|
|
|
|
def reset!(url)
|
|
new(url).reset!
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def exhausted_deliveries_key
|
|
"exhausted_deliveries:#{@host}"
|
|
end
|
|
|
|
def today
|
|
Time.now.utc.strftime('%Y%m%d')
|
|
end
|
|
|
|
def reached_failure_threshold?
|
|
days >= FAILURE_DAYS_THRESHOLD
|
|
end
|
|
end
|