Class: Gitlab::WebHooks::RateLimiter

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/web_hooks/rate_limiter.rb

Constant Summary collapse

LIMIT_NAME =
:web_hook_calls
NO_LIMIT =
0
EXCLUDED_HOOK_TYPES =

SystemHooks (instance admin hooks) and ServiceHooks (integration hooks) are not rate-limited.

%w[SystemHook ServiceHook].freeze

Instance Method Summary collapse

Constructor Details

#initialize(hook) ⇒ RateLimiter

Returns a new instance of RateLimiter.



14
15
16
17
# File 'lib/gitlab/web_hooks/rate_limiter.rb', line 14

def initialize(hook)
  @hook = hook
  @parent = hook.parent
end

Instance Method Details

#limitObject



43
44
45
46
47
48
49
# File 'lib/gitlab/web_hooks/rate_limiter.rb', line 43

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

#rate_limit!Object

Increments the rate-limit counter. Returns true if the hook should be rate-limited.



21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/web_hooks/rate_limiter.rb', line 21

def rate_limit!
  return false if no_limit?

  ::Gitlab::ApplicationRateLimiter.throttled?(
    limit_name,
    scope: [root_namespace],
    threshold: limit
  )
end

#rate_limited?Boolean

Returns true if the hook is currently over its rate-limit. It does not increment the rate-limit counter.

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/web_hooks/rate_limiter.rb', line 33

def rate_limited?
  return false if no_limit?

  Gitlab::ApplicationRateLimiter.peek(
    limit_name,
    scope: [root_namespace],
    threshold: limit
  )
end