summaryrefslogtreecommitdiff
path: root/lib/gitlab
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-12-12 15:46:05 -0800
committerStan Hu <stanhu@gmail.com>2017-12-12 17:12:45 -0800
commit4b0465f20de1bf58326c7daf6876b63438f00d84 (patch)
tree0424373b472e20906f66d94783c86f4ff7f3060f /lib/gitlab
parent02e7e499d4011733c1cf0f6fb675dd2d309f0d52 (diff)
downloadgitlab-ce-sh-add-schedule-pipeline-run-now.tar.gz
Address review comments with playing pipeline schedulersh-add-schedule-pipeline-run-now
Diffstat (limited to 'lib/gitlab')
-rw-r--r--lib/gitlab/action_rate_limiter.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/gitlab/action_rate_limiter.rb b/lib/gitlab/action_rate_limiter.rb
index 53add503c60..4cd3bdefda3 100644
--- a/lib/gitlab/action_rate_limiter.rb
+++ b/lib/gitlab/action_rate_limiter.rb
@@ -12,11 +12,15 @@ module Gitlab
@expiry_time = expiry_time
end
+ # Increments the given cache key and increments the value by 1 with the
+ # given expiration time. Returns the incremented value.
+ #
+ # key - An array of ActiveRecord instances
def increment(key)
value = 0
Gitlab::Redis::Cache.with do |redis|
- cache_key = "action_rate_limiter:#{action}:#{key}"
+ cache_key = action_key(key)
value = redis.incr(cache_key)
redis.expire(cache_key, expiry_time) if value == 1
end
@@ -24,8 +28,20 @@ module Gitlab
value
end
+ # Increments the given key and returns true if the action should
+ # be throttled.
+ #
+ # key - An array of ActiveRecord instances
+ # threshold_value - The maximum number of times this action should occur in the given time interval
def throttled?(key, threshold_value)
self.increment(key) > threshold_value
end
+
+ private
+
+ def action_key(key)
+ serialized = key.map { |obj| "#{obj.class.model_name.to_s.underscore}:#{obj.id}" }.join(":")
+ "action_rate_limiter:#{action}:#{serialized}"
+ end
end
end