Class: Labkit::RateLimit::Rule

Inherits:
Data
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name:, limit:, period:, characteristics:, match: {}, action: :block) ⇒ Rule

Returns a new instance of Rule.

Raises:

  • (ArgumentError)


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

#actionObject (readonly)

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 21

def action
  @action
end

#characteristicsObject (readonly)

Returns the value of attribute characteristics

Returns:

  • (Object)

    the current value of characteristics



21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 21

def characteristics
  @characteristics
end

#limitObject (readonly)

Returns the value of attribute limit

Returns:

  • (Object)

    the current value of limit



21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 21

def limit
  @limit
end

#matchObject (readonly)

Returns the value of attribute match

Returns:

  • (Object)

    the current value of match



21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 21

def match
  @match
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 21

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period

Returns:

  • (Object)

    the current value of period



21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 21

def period
  @period
end