Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rate_limit_helpers.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 653410a40a527503b0a600c023950704c7453956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module Gitlab
  module RateLimitHelpers
    ARCHIVE_RATE_LIMIT_REACHED_MESSAGE = 'This archive has been requested too many times. Try again later.'
    ARCHIVE_RATE_ANONYMOUS_THRESHOLD = 100 # Allow 100 requests/min for anonymous users
    ARCHIVE_RATE_THROTTLE_KEY = :project_repositories_archive

    def archive_rate_limit_reached?(user, project)
      return false unless Feature.enabled?(:archive_rate_limit)

      key = ARCHIVE_RATE_THROTTLE_KEY

      if rate_limiter.throttled?(key, scope: [project, user], threshold: archive_rate_threshold_by_user(user))
        rate_limiter.log_request(request, "#{key}_request_limit".to_sym, user)

        return true
      end

      false
    end

    def archive_rate_threshold_by_user(user)
      if user
        nil # Use the defaults
      else
        ARCHIVE_RATE_ANONYMOUS_THRESHOLD
      end
    end

    def rate_limiter
      ::Gitlab::ApplicationRateLimiter
    end
  end
end