Class: Labkit::RateLimit::Result

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

Overview

Result is the return value of Limiter#check.

It accumulates one Result::Evaluation per matched-and-counted rule; the reader methods report the single most-constraining evaluation (see #most_constraining). A matched :skip rule or a fail-open error replaces the evaluations as the source of the reported outcome.

Result is a mutable accumulator, not a value object: two Results built from the same evaluations are not #==.

matched? - true if at least one rule's match conditions were satisfied exceeded? - true if the reported evaluation is over its limit (see Result::Evaluation#exceeded) action - the outcome: what the caller should do :block = a :limit rule matched and is over its limit :allow = everything else, including an exceeded :log rule (visible via exceeded?), a matched :skip rule, no rule matched, and error (fail-open). The rule's configured action is available via rule.action. rule - the reported Rule: the :skip rule when one matched, otherwise the most-constraining evaluated Rule (nil when matched? is false). Other rules may also have matched and been counted; see #evaluations. error? - true if Redis was unavailable; result fails open (exceeded? is false) degraded? - true if a matched count_distinct rule was skipped by the missing-key fail-open path; the check still completed info - Result::Info with per-window counters for the reported rule; nil when matched? is false, error?, or the matched rule is :skip (no counter exists) evaluations - every counted evaluation, in rule declaration order

Defined Under Namespace

Classes: Evaluation, Info

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error: false) ⇒ Result

Returns a new instance of Result.



44
45
46
47
48
49
50
# File 'lib/labkit/rate_limit/result.rb', line 44

def initialize(error: false)
  @evaluations = []
  @skip_rule = nil
  @error = error
  @degraded = false
  @most_constraining = nil
end

Instance Attribute Details

#evaluations ⇒ Object (readonly)

Returns the value of attribute evaluations.



38
39
40
# File 'lib/labkit/rate_limit/result.rb', line 38

def evaluations
  @evaluations
end

Class Method Details

.error ⇒ Object



40
41
42
# File 'lib/labkit/rate_limit/result.rb', line 40

def self.error
  new(error: true)
end

Instance Method Details

#action ⇒ Object



98
99
100
# File 'lib/labkit/rate_limit/result.rb', line 98

def action
  block? ? :block : :allow
end

#add_evaluation(evaluation) ⇒ Object



52
53
54
55
56
# File 'lib/labkit/rate_limit/result.rb', line 52

def add_evaluation(evaluation)
  @most_constraining = nil
  @evaluations << evaluation
  self
end

#block? ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/labkit/rate_limit/result.rb', line 73

def block?
  !skipped? && @evaluations.any?(&:block?)
end

#degraded! ⇒ Object

Unlike error?, the check keeps going: the flag marks the outcome as degraded without changing it.



89
90
91
92
# File 'lib/labkit/rate_limit/result.rb', line 89

def degraded!
  @degraded = true
  self
end

#degraded? ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/labkit/rate_limit/result.rb', line 94

def degraded?
  @degraded
end

#error? ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/labkit/rate_limit/result.rb', line 83

def error?
  @error
end

#exceeded? ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/labkit/rate_limit/result.rb', line 77

def exceeded?
  return false if skipped?

  !most_constraining.nil? && most_constraining.exceeded?
end

#info ⇒ Object



106
107
108
109
110
# File 'lib/labkit/rate_limit/result.rb', line 106

def info
  return nil if skipped?

  most_constraining&.info
end

#matched? ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/labkit/rate_limit/result.rb', line 65

def matched?
  skipped? || @evaluations.any?
end

#most_constraining ⇒ Object

The evaluation with the strongest claim on the outcome; ties keep the earliest-declared rule (min returns the first of tied elements). Memoized; add_evaluation invalidates.



115
116
117
# File 'lib/labkit/rate_limit/result.rb', line 115

def most_constraining
  @most_constraining ||= @evaluations.min
end

#rule ⇒ Object



102
103
104
# File 'lib/labkit/rate_limit/result.rb', line 102

def rule
  @skip_rule || most_constraining&.rule
end

#skip!(rule) ⇒ Object

Records a matched :skip rule: the request is allowed and the skip rule is the one reported, regardless of any evaluations already collected.



60
61
62
63
# File 'lib/labkit/rate_limit/result.rb', line 60

def skip!(rule)
  @skip_rule = rule
  self
end

#skipped? ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/labkit/rate_limit/result.rb', line 69

def skipped?
  !!@skip_rule
end

#to_response_headers ⇒ Object

Returns the rate limit response headers of the most constraining evaluation, or {} when no rule matched, a :skip rule matched, or an error occurred. See Result::Evaluation#to_response_headers for the keys.



122
123
124
125
126
# File 'lib/labkit/rate_limit/result.rb', line 122

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

  most_constraining.to_response_headers
end