From 2fe38c1c07b304fa2f8cf0d81d7f24cfcea84619 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 18 May 2022 14:16:06 -0500 Subject: [PATCH] Fix #5168: Disable rate limits on testbooru/non-prod environments. Add a `rate_limits_enabled?` config option for disabling rate limits. --- app/logical/rate_limiter.rb | 8 +++++--- config/danbooru_default_config.rb | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/logical/rate_limiter.rb b/app/logical/rate_limiter.rb index 8693c22c0..47b6e52ba 100644 --- a/app/logical/rate_limiter.rb +++ b/app/logical/rate_limiter.rb @@ -13,14 +13,16 @@ class RateLimiter class RateLimitError < StandardError; end - attr_reader :action, :keys, :cost, :rate, :burst + attr_reader :action, :keys, :cost, :rate, :burst, :enabled + alias_method :enabled?, :enabled - def initialize(action, keys = ["*"], cost: 1, rate: 1, burst: 1) + def initialize(action, keys = ["*"], cost: 1, rate: 1, burst: 1, enabled: Danbooru.config.rate_limits_enabled?) @action = action @keys = keys @cost = cost @rate = rate @burst = burst + @enabled = enabled end # Create a RateLimiter object for the given action. A RateLimiter usually has @@ -46,7 +48,7 @@ class RateLimiter # @return [Boolean] true if the action is limited for the user or their IP def limited? - rate_limits.any?(&:limited?) + enabled? && rate_limits.any?(&:limited?) end def as_json(options = {}) diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 68001fab0..47c7dc655 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -372,6 +372,11 @@ module Danbooru true end + # Whether to enable API rate limits. + def rate_limits_enabled? + true + end + def stripe_secret_key end