Class: Labkit::RateLimit::Rule
- Inherits:
-
Data
- Object
- Data
- Labkit::RateLimit::Rule
- Defined in:
- lib/labkit/rate_limit/rule.rb
Overview
Rule is a value object describing a single rate limit rule. name - stable identifier used in Redis keys and log entries match - hash of identifier key/value pairs that must all match for
the rule to apply; empty hash matches any identifier
limit - request threshold; may be a callable (resolved per check) period - window in seconds; may be a callable (resolved per check) action - :block (enforce) or :log (count and log, but do not block) characteristics - identifier keys used to build the compound Redis counter key
name must be a lowercase alphanumeric-and-underscore string of at most 64 characters. It is used as the middle segment of every Redis counter key for this rule, so changing a rule’s name mid-window abandons its in-flight counters.
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#characteristics ⇒ Object
readonly
Returns the value of attribute characteristics.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
-
#initialize(name:, limit:, period:, characteristics:, match: {}, action: :block) ⇒ Rule
constructor
A new instance of Rule.
Constructor Details
#initialize(name:, limit:, period:, characteristics:, match: {}, action: :block) ⇒ Rule
Returns a new instance of Rule.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/labkit/rate_limit/rule.rb', line 22 def initialize(name:, limit:, period:, characteristics:, match: {}, action: :block) raise ArgumentError, "name must be a String or Symbol, got #{name.class}" unless name.is_a?(String) || name.is_a?(Symbol) name_str = name.to_s raise ArgumentError, "name must not be empty" if name_str.empty? action_sym = action.to_sym raise ArgumentError, "Invalid action: #{action.inspect}. Must be one of: #{KNOWN_ACTIONS.inspect}" unless KNOWN_ACTIONS.include?(action_sym) if Labkit.dev_or_test? raise ArgumentError, "Invalid rule name: #{name.inspect}. Must match /\\A[a-z0-9_]+\\z/" unless RULE_NAME_PATTERN.match?(name_str) raise ArgumentError, "Rule name too long: #{name.inspect}. Maximum 64 characters" if name_str.length > RULE_NAME_MAX_LENGTH end super( name: name_str.freeze, match: match.transform_keys(&:to_sym).freeze, limit: limit, period: period, action: action_sym, characteristics: Array(characteristics).map(&:to_sym).freeze ) end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action
21 22 23 |
# File 'lib/labkit/rate_limit/rule.rb', line 21 def action @action end |
#characteristics ⇒ Object (readonly)
Returns the value of attribute characteristics
21 22 23 |
# File 'lib/labkit/rate_limit/rule.rb', line 21 def characteristics @characteristics end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit
21 22 23 |
# File 'lib/labkit/rate_limit/rule.rb', line 21 def limit @limit end |
#match ⇒ Object (readonly)
Returns the value of attribute match
21 22 23 |
# File 'lib/labkit/rate_limit/rule.rb', line 21 def match @match end |
#name ⇒ Object (readonly)
Returns the value of attribute name
21 22 23 |
# File 'lib/labkit/rate_limit/rule.rb', line 21 def name @name end |
#period ⇒ Object (readonly)
Returns the value of attribute period
21 22 23 |
# File 'lib/labkit/rate_limit/rule.rb', line 21 def period @period end |