diff options
Diffstat (limited to 'lib/api/helpers/rate_limiter.rb')
-rw-r--r-- | lib/api/helpers/rate_limiter.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/api/helpers/rate_limiter.rb b/lib/api/helpers/rate_limiter.rb new file mode 100644 index 00000000000..5a531b5324a --- /dev/null +++ b/lib/api/helpers/rate_limiter.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module API + module Helpers + module RateLimiter + def check_rate_limit!(key, scope) + if rate_limiter.throttled?(key, scope: scope) + log_request(key) + render_exceeded_limit_error! + end + end + + private + + def rate_limiter + ::Gitlab::ApplicationRateLimiter + end + + def render_exceeded_limit_error! + render_api_error!({ error: _('This endpoint has been requested too many times. Try again later.') }, 429) + end + + def log_request(key) + rate_limiter.log_request(request, "#{key}_request_limit".to_sym, current_user) + end + end + end +end |