Class: Labkit::RateLimit::Matcher
- Inherits:
-
Data
- Object
- Data
- Labkit::RateLimit::Matcher
- Defined in:
- lib/labkit/rate_limit/matcher.rb
Overview
Matcher is the internal representation of a single key/value predicate in a Rule#match hash. Rule.new normalizes every match value through Matcher.build; the Evaluator calls Matcher#match? per identifier value.
Accepted input shapes (everything else raises ArgumentError):
- any plain value (String, Symbol, Integer, ...) -> :eq matcher
- a Regexp instance -> :re matcher (Ruby convenience)
- { eq: <value> } -> :eq matcher (canonical, YAML-compatible)
- { ne: <value> } -> :ne matcher (complement of :eq)
- { re: <String|Regexp> } -> :re matcher (canonical, YAML-compatible)
- { nre: <String|Regexp> } -> :nre matcher (complement of :re)
- { oneOf: <Array> } -> :oneOf matcher (set membership, YAML-compatible)
- { noneOf: <Array> } -> :noneOf matcher (complement of :oneOf)
- [ <matcher>, <matcher>, ... ] -> :all matcher (every entry must match)
An Array means AND, so several predicates can apply to one identifier key. Its entries must be matcher Hashes or bare Regexps, the two shapes that already mean "a matcher" on their own. An Array of plain values is rejected, so a value that was meant as a single matcher but arrived as a list fails loudly instead of silently becoming a membership test. An Array holds matchers rather than other Arrays, so a match value nests at most MAX_NESTING_DEPTH deep; see too_deeply_nested?.
:oneOf and :noneOf membership is Set#include? (eql?/hash equality), not :eq's ==, so cross-type numerics differ (1 == 1.0, but [1] does not match 1.0).
Hash-key naming follows the metrics-catalog selector pattern. Glob, prefix, and other matcher kinds are intentionally out of scope here; see gitlab-com/gl-infra/production-engineering#28853 for that follow-up.
The :re and :nre matchers coerce the identifier value via #to_s before applying the regex, so callers can match non-String identifier values such as Integer status codes (e.g. { re: "^5" } against status: 503).
Each negation matcher is the plain complement of its positive form, with no special case for a missing key. Evaluator passes identifier, so an absent key arrives as nil, and the complement is therefore usually true for one: a request with no path is not a request to an excluded path. A rule that should apply only when the key is present says so, by adding a presence matcher alongside the exclusions:
path: [/./, { nre: '\A/api/' }]
Rules from a config file are built with engine: :re2. Their :re and :nre
patterns compile with RE2, the syntax every SDK shares, so a pattern that
only Ruby accepts (lookbehind, a possessive quantifier) is rejected and a
^ anchors the whole text, not each line. RE2 has no backtracking, so
those matchers need no timeout.
Constant Summary collapse
- KNOWN_HASH_KEYS =
%i[eq ne re nre oneOf noneOf].freeze
- ERROR_INSPECT_LIMIT =
80- MAX_NESTING_DEPTH =
No Hash or Array may sit this many levels below the match value, which is level 0. The deepest valid shape is an Array (0) holding a matcher Hash (1) whose :oneOf list (2) holds plain values; see too_deeply_nested?.
3- MATCH_TIMEOUT_SECONDS =
Wall-clock budget for a single #match? call. Without it a match is bounded only by whatever global the host sets (40s in GitLab Rails, unbounded elsewhere), and a timeout reaches Evaluator's fail-open rescue, so the request goes unlimited.
50ms is a safety net against catastrophic backtracking, not a performance bound: real matches are far cheaper (median 0.087ms, worst 1.484ms across 6935 paths that timed out in production at 5ms). Those timeouts were wall-clock stalls, since Ruby counts real time and GC pauses on GitLab's git fleet have a p50 of ~72ms. Under 4x CPU oversubscription 50ms still fires on 0.16% of those matches, 5ms 1.33%.
This timeout is the only bound on a pattern. There is no cap on regex source length: rules are written by operators rather than end users, and a cap that applied to "..." but not to a bare Regexp bounded nothing in practice, since callers pass Regexp constants.
0.05- ENGINES =
%i[ruby re2].freeze
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.build(input, engine = :ruby) ⇒ Object
engine is positional, not a keyword: callers write build(eq: x) and a keyword here would swallow that Hash.
Instance Method Summary collapse
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type
56 57 58 |
# File 'lib/labkit/rate_limit/matcher.rb', line 56 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value
56 57 58 |
# File 'lib/labkit/rate_limit/matcher.rb', line 56 def value @value end |
Class Method Details
.build(input, engine = :ruby) ⇒ Object
engine is positional, not a keyword: callers write build(eq: x) and a keyword here would swallow that Hash.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/labkit/rate_limit/matcher.rb', line 87 def self.build(input, engine = :ruby) raise ArgumentError, "engine must be one of #{ENGINES.inspect}, got #{engine.inspect}" unless ENGINES.include?(engine) raise ArgumentError, nesting_error(input) if too_deeply_nested?(input) case input when Regexp new(type: :re, value: regexp(:re, input, engine)) when Hash from_hash(input, engine) when Array compose(input, engine) else new(type: :eq, value: input) end end |
Instance Method Details
#match?(identifier_value) ⇒ Boolean
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/labkit/rate_limit/matcher.rb', line 278 def match?(identifier_value) case type when :eq value == identifier_value when :ne value != identifier_value when :oneOf value.include?(identifier_value) when :noneOf # exclude? is ActiveSupport, and nothing in rate_limit requires it. !value.include?(identifier_value) # rubocop:disable Rails/NegateInclude when :re value.match?(identifier_value.to_s) when :nre !value.match?(identifier_value.to_s) when :all value.all? { |matcher| matcher.match?(identifier_value) } else raise ArgumentError, "unknown matcher type: #{type.inspect}" end end |