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

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.



14
15
16
17
18
19
20
21
22
23
# File 'lib/labkit/rate_limit/rule.rb', line 14

def initialize(name:, limit:, period:, characteristics:, match: {}, action: :block)
  super(
    name: name.to_s.tr(":", "_"),
    match: match.transform_keys(&:to_sym).freeze,
    limit: limit,
    period: period,
    action: action.to_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



13
14
15
# File 'lib/labkit/rate_limit/rule.rb', line 13

def action
  @action
end

#characteristicsObject (readonly)

Returns the value of attribute characteristics

Returns:

  • (Object)

    the current value of characteristics



13
14
15
# File 'lib/labkit/rate_limit/rule.rb', line 13

def characteristics
  @characteristics
end

#limitObject (readonly)

Returns the value of attribute limit

Returns:

  • (Object)

    the current value of limit



13
14
15
# File 'lib/labkit/rate_limit/rule.rb', line 13

def limit
  @limit
end

#matchObject (readonly)

Returns the value of attribute match

Returns:

  • (Object)

    the current value of match



13
14
15
# File 'lib/labkit/rate_limit/rule.rb', line 13

def match
  @match
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



13
14
15
# File 'lib/labkit/rate_limit/rule.rb', line 13

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period

Returns:

  • (Object)

    the current value of period



13
14
15
# File 'lib/labkit/rate_limit/rule.rb', line 13

def period
  @period
end