Class: Dinie::Internal::RateLimitTracker
- Inherits:
-
Object
- Object
- Dinie::Internal::RateLimitTracker
- Defined in:
- lib/dinie/runtime/rate_limit.rb
Overview
Parses the three ‘X-RateLimit-*` headers off the latest response and holds the most recent RateLimit snapshot for HttpClient (architecture §10). Mirrors `sdk-js` `rate-limit.ts` (`parseRateLimit` + `RateLimitTracker`).
**All-or-nothing (decision):** RateLimit has non-nullable fields, so RateLimitTracker.parse returns the whole object or ‘nil` — never a partial. A missing/garbage header makes the parse `nil`, and #update then keeps the previous snapshot rather than clobbering a good value with a header-less response (such as the token call).
Constant Summary collapse
- LIMIT_HEADER =
Response header carrying the window ceiling.
"x-ratelimit-limit"- REMAINING_HEADER =
Response header carrying the requests remaining in the window.
"x-ratelimit-remaining"- RESET_HEADER =
Response header carrying the window reset time (epoch seconds or delta seconds).
"x-ratelimit-reset"- EPOCH_THRESHOLD_SECONDS =
‘X-RateLimit-Reset` values at or above this (seconds) are read as an absolute Unix epoch; anything below as a delta in seconds from now. `1e9` seconds is ~2001-09 — far above any plausible “seconds until reset” window yet below every real epoch.
1_000_000_000
Class Method Summary collapse
-
.parse(headers) ⇒ Dinie::RateLimit?
Parse the three ‘X-RateLimit-*` headers into a RateLimit, or `nil` when any is absent or unparseable.
Instance Method Summary collapse
-
#snapshot ⇒ Dinie::RateLimit?
Latest snapshot, or ‘nil` before any valid headers.
-
#update(headers) ⇒ void
Fold a response’s headers into the snapshot.
Class Method Details
.parse(headers) ⇒ Dinie::RateLimit?
Parse the three ‘X-RateLimit-*` headers into a RateLimit, or `nil` when any is absent or unparseable. Never raises.
103 104 105 106 107 108 109 110 |
# File 'lib/dinie/runtime/rate_limit.rb', line 103 def self.parse(headers) limit = parse_count(header_value(headers, LIMIT_HEADER)) remaining = parse_count(header_value(headers, REMAINING_HEADER)) reset_at = parse_reset(header_value(headers, RESET_HEADER)) return nil if limit.nil? || remaining.nil? || reset_at.nil? Dinie::RateLimit.new(limit: limit, remaining: remaining, reset_at: reset_at) end |
Instance Method Details
#snapshot ⇒ Dinie::RateLimit?
Returns latest snapshot, or ‘nil` before any valid headers.
84 85 86 |
# File 'lib/dinie/runtime/rate_limit.rb', line 84 def snapshot @current end |
#update(headers) ⇒ void
This method returns an undefined value.
Fold a response’s headers into the snapshot. A header-less or garbage response leaves the previous snapshot untouched (does not reset it to ‘nil`).
93 94 95 96 |
# File 'lib/dinie/runtime/rate_limit.rb', line 93 def update(headers) parsed = self.class.parse(headers) @current = parsed unless parsed.nil? end |