summaryrefslogtreecommitdiff
path: root/lib/api/helpers/rate_limiter.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-15 15:42:17 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-15 15:42:17 +0000
commit44fdf983bd35328dd577d3d3650d14163ef3e2b6 (patch)
tree84ff300d056cfbabb5a0fe2a9cbaa80aaeab1cc5 /lib/api/helpers/rate_limiter.rb
parentbc9fa07b26184b5c94808f704db6ea1ac81bf4de (diff)
downloadgitlab-ce-44fdf983bd35328dd577d3d3650d14163ef3e2b6.tar.gz
Add latest changes from gitlab-org/gitlab@12-10-stable-ee
Diffstat (limited to 'lib/api/helpers/rate_limiter.rb')
-rw-r--r--lib/api/helpers/rate_limiter.rb28
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