Exception: X::TooManyRequests
- Inherits:
-
ClientError
- Object
- StandardError
- Error
- HTTPError
- ClientError
- X::TooManyRequests
- Defined in:
- x-core/lib/x/core/errors/too_many_requests.rb
Overview
Error raised when rate limit is exceeded (HTTP 429)
Instance Method Summary collapse
-
#exhausted_rate_limits ⇒ Array<RateLimit>
The rate limits with no requests left, one of which refused the request.
-
#limiting_rate_limit ⇒ RateLimit?
The exhausted rate limit that resets last, which a request waits for.
-
#rate_limit ⇒ RateLimit?
The 15-minute rate limit of the endpoint, which nearly every response reports.
-
#rate_limits ⇒ Array<RateLimit>
The rate limits the response reports in its headers, as X::Response reports them.
-
#reset_at ⇒ Time?
Get the time when the rate limit resets.
-
#reset_in ⇒ Integer?
Get the seconds until the rate limit resets.
-
#retry_after ⇒ Integer?
The seconds to wait before retrying, as the response asks.
Constructor Details
This class inherits a constructor from X::HTTPError
Instance Method Details
#exhausted_rate_limits ⇒ Array<RateLimit>
The rate limits with no requests left, one of which refused the request
37 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 37 def exhausted_rate_limits = rate_limits.select(&:exhausted?) |
#limiting_rate_limit ⇒ RateLimit?
The exhausted rate limit that resets last, which a request waits for
45 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 45 def limiting_rate_limit = exhausted_rate_limits.max_by(&:reset_at) |
#rate_limit ⇒ RateLimit?
The 15-minute rate limit of the endpoint, which nearly every response reports
29 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 29 def rate_limit = rate_limits.find { |limit| limit.type.eql?(RateLimit::RATE_LIMIT_TYPE) } |
#rate_limits ⇒ Array<RateLimit>
The rate limits the response reports in its headers, as X::Response reports them
They are read once and frozen, since the other readers of the error, and the wait before a request is sent again, read them too, so that a caller changing them changes none of those.
19 20 21 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 19 def rate_limits @rate_limits ||= RateLimit.__send__(:all_from, http_response).freeze end |
#reset_at ⇒ Time?
Get the time when the rate limit resets
53 54 55 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 53 def reset_at limiting_rate_limit&.reset_at end |
#reset_in ⇒ Integer?
Get the seconds until the rate limit resets
63 64 65 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 63 def reset_in limiting_rate_limit&.reset_in end |
#retry_after ⇒ Integer?
The seconds to wait before retrying, as the response asks
A Retry-After header counts the seconds from when the response was sent, so it says the same thing however far this machine's clock is from the API's, where reset_in is off by the difference between the two. The header is preferred for that reason, and the limit that refused the request answers for a response that carries none. X recommends waiting a minute, doubling the wait for each retry after, when it says neither.
78 |
# File 'x-core/lib/x/core/errors/too_many_requests.rb', line 78 def retry_after = super || reset_in |