This repository was archived by the owner on Sep 19, 2018. It is now read-only.
Conversation
z64
commented
Jul 26, 2017
|
|
||
| # @return [Integer] number of requests until we're rate limited | ||
| def remaining_requests | ||
| last_header(:x_rate_limit_remaining)&.to_i || -1 |
Contributor
Author
There was a problem hiding this comment.
To clarify this defaulting to -1:
last_header(:x_rate_limit_remaining)&.to_i returns [Integer, nil] which would fail the subsequent #zero? check in #will_be_rate_limited?. I opted just to return -1 as it isn't zero (it's unknown until we make our first request).
In contrast, if we default to zero, then #will_be_rate_limited? may return true on the first request and sleep for 0 seconds. This isn't a big deal either, but if we implement something like rate limit logging later, it may look pretty confusing. 1 may also be an acceptable default for this method, but it isn't true (we didn't get that from the API) ; so I think this justifies -1 as a special case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements a mutex around
API#requestthat protects against making rate limited requests.This works by:
:x_rate_limit_remaining)x_rate_limit_reset),sleepforx_rate_limit_reset_remainingmillisecondsMutex#synchronizeblock and is therefore thread-safe.Todo:
Example raw rate limit headers: