blob: 73d59f6f786f4a523cdd98bb1e0d816e0ff4d70e (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# frozen_string_literal: true
module Gitlab
module WebHooks
class RateLimiter
include Gitlab::Utils::StrongMemoize
LIMIT_NAME = :web_hook_calls
NO_LIMIT = 0
# SystemHooks (instance admin hooks) and ServiceHooks (integration hooks)
# are not rate-limited.
EXCLUDED_HOOK_TYPES = %w(SystemHook ServiceHook).freeze
def initialize(hook)
@hook = hook
@parent = hook.parent
end
# Increments the rate-limit counter.
# Returns true if the hook should be rate-limited.
def rate_limit!
return false if no_limit?
::Gitlab::ApplicationRateLimiter.throttled?(
limit_name,
scope: [root_namespace],
threshold: limit
)
end
# Returns true if the hook is currently over its rate-limit.
# It does not increment the rate-limit counter.
def rate_limited?
return false if no_limit?
Gitlab::ApplicationRateLimiter.peek(
limit_name,
scope: [root_namespace],
threshold: limit
)
end
def limit
strong_memoize(:limit) do
next NO_LIMIT if hook.class.name.in?(EXCLUDED_HOOK_TYPES)
root_namespace.actual_limits.limit_for(limit_name) || NO_LIMIT
end
end
private
attr_reader :hook, :parent
def no_limit?
limit == NO_LIMIT
end
def root_namespace
@root_namespace ||= parent.root_ancestor
end
def limit_name
LIMIT_NAME
end
end
end
end
Gitlab::WebHooks::RateLimiter.prepend_mod
|