Class: Labkit::RateLimit::Result::Evaluation

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

Overview

The outcome of counting one matched rule. rule - the evaluated Rule exceeded - whether the rule is over its limit: the post-increment count exceeded it, a ban holds, or an admitting rule refused the value info - Result::Info with the per-window counters

Evaluations order by constraint: a blocking evaluation (a :limit rule over its limit, or one whose ban is still in force) ranks strictly first - it decides the request no matter what any other rule reports - then exceeded ones, then fewest remaining. remaining floors at 0, so an exceeded :log rule ties with one sitting exactly on its limit; ranking exceeded ahead keeps a breach from being hidden by a rule that merely reached its limit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exceeded ⇒ Object (readonly)

Returns the value of attribute exceeded

Returns:

  • (Object) —

    the current value of exceeded



142
143
144
# File 'lib/labkit/rate_limit/result.rb', line 142

def exceeded
  @exceeded
end

#info ⇒ Object (readonly)

Returns the value of attribute info

Returns:

  • (Object) —

    the current value of info



142
143
144
# File 'lib/labkit/rate_limit/result.rb', line 142

def info
  @info
end

#rule ⇒ Object (readonly)

Returns the value of attribute rule

Returns:

  • (Object) —

    the current value of rule



142
143
144
# File 'lib/labkit/rate_limit/result.rb', line 142

def rule
  @rule
end

Instance Method Details

#<=>(other) ⇒ Object



153
154
155
# File 'lib/labkit/rate_limit/result.rb', line 153

def <=>(other)
  constraint_rank <=> other.constraint_rank
end

#block? ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/labkit/rate_limit/result.rb', line 149

def block?
  exceeded? && rule.action == :limit
end

#exceeded? ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/labkit/rate_limit/result.rb', line 145

def exceeded?
  exceeded
end

#to_response_headers ⇒ Object

Returns the rate limit response headers for this evaluation. Values are Strings, and counts are coerced to Integer because the RateLimit header spec requires integer values even when count is fractional.

RateLimit-Name (the rule name), RateLimit-Limit, RateLimit-Observed, RateLimit-Remaining and RateLimit-Reset (Unix timestamp) are always present. Limit, Observed and Remaining all count within the rule's own period. A blocking evaluation also gets Retry-After (seconds) and RateLimit-ResetTime (HTTP date), which belong on the 429.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/labkit/rate_limit/result.rb', line 166

def to_response_headers
  headers = {}
  headers["RateLimit-Name"] = rule.name
  # Floored at 0 like remaining: a callable limit can resolve below zero
  # to deny all (deny new values on an admit_counted_values rule), and an
  # older client could credit a counter below zero.
  headers["RateLimit-Limit"] = [info.resolved_limit.to_i, 0].max.to_s
  headers["RateLimit-Observed"] = [info.count.to_i, 0].max.to_s
  headers["RateLimit-Remaining"] = info.remaining.to_i.to_s
  headers["RateLimit-Reset"] = info.reset_at.to_i.to_s
  return headers unless block?

  # Rounded up so a client that honours it does not retry before the reset.
  headers["Retry-After"] = [(info.reset_at - Time.now.utc).ceil, 0].max.to_s
  headers["RateLimit-ResetTime"] = info.reset_at.httpdate
  headers
end