gitlab-labkit 1.15.0 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/labkit/rate_limit/evaluator.rb +4 -3
- data/lib/labkit/rate_limit/limiter.rb +3 -16
- data/lib/labkit/rate_limit/result.rb +7 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 55b7255672a78e7ce050b17dee4a50b0c9ceadd2093986dc0d5d3822076423b0
|
|
4
|
+
data.tar.gz: fe70e4a910d27fa2d8d1a61216825bb811a8a43b9fbd75d4c0bd32220e8551b3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 38e4de6ac2d9e677a269f757e4c01a1bb1d9e3ce4cec0065512a03d07650339ae6e5f27415be76ba02cc316c56e5fd8d4922389e856da152be527800a6f58de6
|
|
7
|
+
data.tar.gz: 613915d89f71db61f3a984ae979d704753c9284d196dc7f31c92426b0f037ded2c0140ea9fc778dbbd42bb7c0ccfa0884b3d67e3ebc38b791bc84edfa6cb852d
|
|
@@ -25,7 +25,7 @@ module Labkit
|
|
|
25
25
|
# Intentionally broad: fail-open applies to any unexpected error (network,
|
|
26
26
|
# timeout, OOM) not only Redis protocol errors.
|
|
27
27
|
log_error(e, identifier)
|
|
28
|
-
Result.new(matched: false, error: true)
|
|
28
|
+
Result.new(matched: false, error: true, action: :allow)
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
private
|
|
@@ -37,7 +37,7 @@ module Labkit
|
|
|
37
37
|
return evaluate_rule(rule, identifier)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
Result.new(matched: false)
|
|
40
|
+
Result.new(matched: false, action: :allow)
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def rule_matches?(rule, identifier)
|
|
@@ -51,8 +51,9 @@ module Labkit
|
|
|
51
51
|
|
|
52
52
|
count = incr_with_ttl(redis_key, resolved_period)
|
|
53
53
|
exceeded = count > resolved_limit
|
|
54
|
+
action = exceeded ? rule.action : :allow
|
|
54
55
|
|
|
55
|
-
Result.new(matched: true, exceeded: exceeded, action:
|
|
56
|
+
Result.new(matched: true, exceeded: exceeded, action: action, rule: rule)
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
def build_redis_key(rule, identifier)
|
|
@@ -21,11 +21,10 @@ module Labkit
|
|
|
21
21
|
|
|
22
22
|
def initialize(name:, rules:, redis: nil, logger: nil)
|
|
23
23
|
@logger = logger || RateLimit.config.logger || Labkit::Logging::JsonLogger.new($stdout)
|
|
24
|
-
|
|
25
|
-
@name = validated_name
|
|
24
|
+
@name = validate_name!(name)
|
|
26
25
|
|
|
27
26
|
@evaluator = Evaluator.new(
|
|
28
|
-
name:
|
|
27
|
+
name: @name,
|
|
29
28
|
rules: prepare_rules(rules),
|
|
30
29
|
redis: redis || RateLimit.config.redis,
|
|
31
30
|
logger: @logger
|
|
@@ -36,19 +35,7 @@ module Labkit
|
|
|
36
35
|
# @return [Result]
|
|
37
36
|
def check(identifier)
|
|
38
37
|
id = identifier.is_a?(Identifier) ? identifier : Identifier.new(identifier)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if result.exceeded? && result.action == :block
|
|
42
|
-
@logger.warn(
|
|
43
|
-
message: "rate_limit_check",
|
|
44
|
-
name: @name,
|
|
45
|
-
rule_name: result.rule.name,
|
|
46
|
-
exceeded: true,
|
|
47
|
-
severity: "WARN"
|
|
48
|
-
)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
result
|
|
38
|
+
@evaluator.check(id)
|
|
52
39
|
end
|
|
53
40
|
|
|
54
41
|
private
|
|
@@ -5,11 +5,16 @@ module Labkit
|
|
|
5
5
|
# Result is the return value of Limiter#check.
|
|
6
6
|
# matched? - true if a rule's match conditions were satisfied
|
|
7
7
|
# exceeded? - true if the matched rule's counter exceeded its limit
|
|
8
|
-
# action -
|
|
8
|
+
# action - the outcome: what the caller should do
|
|
9
|
+
# :block = rule matched, exceeded, rule configured to block
|
|
10
|
+
# :log = rule matched, exceeded, rule configured to log only
|
|
11
|
+
# :allow = rule matched but count within limit, or
|
|
12
|
+
# no rule matched, or error (fail-open)
|
|
13
|
+
# The rule's configured action is available via rule.action
|
|
9
14
|
# rule - the matched Rule object (nil when matched? is false)
|
|
10
15
|
# error? - true if Redis was unavailable; result fails open (exceeded? is false)
|
|
11
16
|
Result = Data.define(:matched, :exceeded, :action, :rule, :error) do
|
|
12
|
-
def initialize(matched:, exceeded: false,
|
|
17
|
+
def initialize(matched:, action:, exceeded: false, rule: nil, error: false)
|
|
13
18
|
super
|
|
14
19
|
end
|
|
15
20
|
|