Class: Labkit::RateLimit::Result

Inherits:
Data
  • Object
show all
Defined in:
lib/labkit/rate_limit/result.rb

Overview

Result is the return value of Limiter#check. matched? - true if a rule’s match conditions were satisfied exceeded? - true if the matched rule’s counter exceeded its limit action - the outcome: what the caller should do

:block = rule matched, exceeded, rule configured to block
:log   = rule matched, exceeded, rule configured to log only
:allow = rule matched but count within limit, or
         no rule matched, or error (fail-open)
The rule's configured action is available via rule.action

rule - the matched Rule object (nil when matched? is false) error? - true if Redis was unavailable; result fails open (exceeded? is false) info - Result::Info with per-window counters; nil when matched? is false or error?

Defined Under Namespace

Classes: Info

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matched:, action: nil, exceeded: false, rule: nil, error: false, info: nil) ⇒ Result

Returns a new instance of Result.



18
19
20
# File 'lib/labkit/rate_limit/result.rb', line 18

def initialize(matched:, action: nil, exceeded: false, rule: nil, error: false, info: nil)
  super
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



17
18
19
# File 'lib/labkit/rate_limit/result.rb', line 17

def action
  @action
end

#errorObject (readonly)

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



17
18
19
# File 'lib/labkit/rate_limit/result.rb', line 17

def error
  @error
end

#exceededObject (readonly)

Returns the value of attribute exceeded

Returns:

  • (Object)

    the current value of exceeded



17
18
19
# File 'lib/labkit/rate_limit/result.rb', line 17

def exceeded
  @exceeded
end

#infoObject (readonly)

Returns the value of attribute info

Returns:

  • (Object)

    the current value of info



17
18
19
# File 'lib/labkit/rate_limit/result.rb', line 17

def info
  @info
end

#matchedObject (readonly)

Returns the value of attribute matched

Returns:

  • (Object)

    the current value of matched



17
18
19
# File 'lib/labkit/rate_limit/result.rb', line 17

def matched
  @matched
end

#ruleObject (readonly)

Returns the value of attribute rule

Returns:

  • (Object)

    the current value of rule



17
18
19
# File 'lib/labkit/rate_limit/result.rb', line 17

def rule
  @rule
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/labkit/rate_limit/result.rb', line 30

def error?
  error
end

#exceeded?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/labkit/rate_limit/result.rb', line 26

def exceeded?
  exceeded
end

#matched?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/labkit/rate_limit/result.rb', line 22

def matched?
  matched
end

#to_response_headersObject

Returns RFC-compliant rate limit response headers, or {} when no rule matched or an error occurred. Keys: RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset (Unix timestamp). reset_at is advisory only - derived from a pipelined redis.ttl call, not fully atomic.



37
38
39
40
41
42
43
44
45
# File 'lib/labkit/rate_limit/result.rb', line 37

def to_response_headers
  return {} unless matched? && !error? && info

  {
    "RateLimit-Limit" => info.resolved_limit.to_s,
    "RateLimit-Remaining" => info.remaining.to_s,
    "RateLimit-Reset" => info.reset_at.to_i.to_s
  }
end