diff options
Diffstat (limited to 'lib/gitlab/throttle.rb')
-rw-r--r-- | lib/gitlab/throttle.rb | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/lib/gitlab/throttle.rb b/lib/gitlab/throttle.rb index 8f045021088..622dc7d9ed0 100644 --- a/lib/gitlab/throttle.rb +++ b/lib/gitlab/throttle.rb @@ -4,6 +4,11 @@ module Gitlab class Throttle DEFAULT_RATE_LIMITING_RESPONSE_TEXT = 'Retry later' + # Each of these settings follows the same pattern of specifying separate + # authenticated and unauthenticated rates via settings. New throttles should + # ideally be regular as well. + REGULAR_THROTTLES = [:api, :packages_api, :files_api].freeze + def self.settings Gitlab::CurrentSettings.current_application_settings end @@ -24,21 +29,38 @@ module Gitlab "HTTP_#{env_value.upcase.tr('-', '_')}" end - def self.unauthenticated_options + class << self + def options(throttle, authenticated:) + fragment = throttle_fragment!(throttle, authenticated: authenticated) + + # rubocop:disable GitlabSecurity/PublicSend + limit_proc = proc { |req| settings.public_send("#{fragment}_requests_per_period") } + period_proc = proc { |req| settings.public_send("#{fragment}_period_in_seconds").seconds } + # rubocop:enable GitlabSecurity/PublicSend + + { limit: limit_proc, period: period_proc } + end + + def throttle_fragment!(throttle, authenticated:) + raise("Unknown throttle: #{throttle}") unless REGULAR_THROTTLES.include?(throttle) + + "throttle_#{'un' unless authenticated}authenticated_#{throttle}" + end + end + + def self.unauthenticated_web_options + # TODO: Columns will be renamed in https://gitlab.com/gitlab-org/gitlab/-/issues/340031 + # Once this is done, web can be made into a regular throttle limit_proc = proc { |req| settings.throttle_unauthenticated_requests_per_period } period_proc = proc { |req| settings.throttle_unauthenticated_period_in_seconds.seconds } - { limit: limit_proc, period: period_proc } - end - def self.authenticated_api_options - limit_proc = proc { |req| settings.throttle_authenticated_api_requests_per_period } - period_proc = proc { |req| settings.throttle_authenticated_api_period_in_seconds.seconds } { limit: limit_proc, period: period_proc } end def self.authenticated_web_options limit_proc = proc { |req| settings.throttle_authenticated_web_requests_per_period } period_proc = proc { |req| settings.throttle_authenticated_web_period_in_seconds.seconds } + { limit: limit_proc, period: period_proc } end @@ -49,16 +71,9 @@ module Gitlab { limit: limit_proc, period: period_proc } end - def self.unauthenticated_packages_api_options - limit_proc = proc { |req| settings.throttle_unauthenticated_packages_api_requests_per_period } - period_proc = proc { |req| settings.throttle_unauthenticated_packages_api_period_in_seconds.seconds } - - { limit: limit_proc, period: period_proc } - end - - def self.authenticated_packages_api_options - limit_proc = proc { |req| settings.throttle_authenticated_packages_api_requests_per_period } - period_proc = proc { |req| settings.throttle_authenticated_packages_api_period_in_seconds.seconds } + def self.throttle_authenticated_git_lfs_options + limit_proc = proc { |req| settings.throttle_authenticated_git_lfs_requests_per_period } + period_proc = proc { |req| settings.throttle_authenticated_git_lfs_period_in_seconds.seconds } { limit: limit_proc, period: period_proc } end |