Class: Labkit::RateLimit::Result
- Inherits:
-
Data
- Object
- Data
- Labkit::RateLimit::Result
- 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, rule configured to allow, rule configured to skip (bypass, nothing counted), 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, error?, or the matched rule is :skip (no counter exists)
Defined Under Namespace
Classes: Info
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#exceeded ⇒ Object
readonly
Returns the value of attribute exceeded.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#matched ⇒ Object
readonly
Returns the value of attribute matched.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Instance Method Summary collapse
- #error? ⇒ Boolean
- #exceeded? ⇒ Boolean
-
#initialize(matched:, action: nil, exceeded: false, rule: nil, error: false, info: nil) ⇒ Result
constructor
A new instance of Result.
- #matched? ⇒ Boolean
-
#to_response_headers ⇒ Object
Returns RFC-compliant rate limit response headers, or {} when no rule matched or an error occurred.
Constructor Details
#initialize(matched:, action: nil, exceeded: false, rule: nil, error: false, info: nil) ⇒ Result
Returns a new instance of Result.
20 21 22 |
# File 'lib/labkit/rate_limit/result.rb', line 20 def initialize(matched:, action: nil, exceeded: false, rule: nil, error: false, info: nil) super end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action
19 20 21 |
# File 'lib/labkit/rate_limit/result.rb', line 19 def action @action end |
#error ⇒ Object (readonly)
Returns the value of attribute error
19 20 21 |
# File 'lib/labkit/rate_limit/result.rb', line 19 def error @error end |
#exceeded ⇒ Object (readonly)
Returns the value of attribute exceeded
19 20 21 |
# File 'lib/labkit/rate_limit/result.rb', line 19 def exceeded @exceeded end |
#info ⇒ Object (readonly)
Returns the value of attribute info
19 20 21 |
# File 'lib/labkit/rate_limit/result.rb', line 19 def info @info end |
#matched ⇒ Object (readonly)
Returns the value of attribute matched
19 20 21 |
# File 'lib/labkit/rate_limit/result.rb', line 19 def matched @matched end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule
19 20 21 |
# File 'lib/labkit/rate_limit/result.rb', line 19 def rule @rule end |
Instance Method Details
#error? ⇒ Boolean
32 33 34 |
# File 'lib/labkit/rate_limit/result.rb', line 32 def error? error end |
#exceeded? ⇒ Boolean
28 29 30 |
# File 'lib/labkit/rate_limit/result.rb', line 28 def exceeded? exceeded end |
#matched? ⇒ Boolean
24 25 26 |
# File 'lib/labkit/rate_limit/result.rb', line 24 def matched? matched end |
#to_response_headers ⇒ Object
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). remaining is coerced to Integer for header output even when info.remaining is fractional; the RateLimit header spec requires integer values.
40 41 42 43 44 45 46 47 48 |
# File 'lib/labkit/rate_limit/result.rb', line 40 def to_response_headers return {} unless matched? && !error? && info { "RateLimit-Limit" => info.resolved_limit.to_i.to_s, "RateLimit-Remaining" => info.remaining.to_i.to_s, "RateLimit-Reset" => info.reset_at.to_i.to_s } end |