Skip to content

Return rate limit information immediately after checkRateLimitBeforeDo #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ limited to 60 requests per hour, while authenticated clients can make up to
that are not issued on behalf of a user, use the
`UnauthenticatedRateLimitedTransport`.

The `Rate` method on a client returns the rate limit information based on the most
recent API call. This is updated on every call, but may be out of date if it's
been some time since the last API call and other clients have made subsequent
requests since then. You can always call `RateLimits()` directly to get the most
up-to-date rate limit data for the client.
The returned `Response.Rate` value contains the rate limit information
from the most recent API call. If a recent enough response isn't
available, you can use `RateLimits` to fetch the most up-to-date rate
limit data for the client.

To detect an API rate limit error, you can check if its type is `*github.RateLimitError`:

Expand Down
9 changes: 4 additions & 5 deletions github/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ limited to 60 requests per hour, while authenticated clients can make up to
that are not issued on behalf of a user, use the
UnauthenticatedRateLimitedTransport.

The Rate method on a client returns the rate limit information based on the most
recent API call. This is updated on every call, but may be out of date if it's
been some time since the last API call and other clients have made subsequent
requests since then. You can always call RateLimits() directly to get the most
up-to-date rate limit data for the client.
The returned Response.Rate value contains the rate limit information
from the most recent API call. If a recent enough response isn't
available, you can use RateLimits to fetch the most up-to-date rate
limit data for the client.

To detect an API rate limit error, you can check if its type is *github.RateLimitError:

Expand Down
7 changes: 5 additions & 2 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,10 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res

// If we've hit rate limit, don't make further requests before Reset time.
if err := c.checkRateLimitBeforeDo(req, rateLimitCategory); err != nil {
return nil, err
return &Response{
Response: err.Response,
Copy link
Member

@dmitshur dmitshur Mar 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one potential concern about this change.

I think it's somewhat unfortunate that we ended up being forced to create a fake/mock *http.Response even in situations where no remote HTTP request was actually made. I am referring to this.

It was originally done in #347 because we (or I) didn't want to break clients that assumed *http.Response was never nil, since it used to never be nil in prior to that change.

So I think it's somewhat disadvantageous that we would now be propagating the fake *http.Response in more situations.

However, even if we had control over this, I'm not sure what's better. If a remote API request is not made (because we've predicted it'll fail due to exceeded rate limit), should the client be able to tell it apart from when a remote API request was made? It wasn't documented in the past, but one could've done that by checking if resp was nil. This changes that.

I'm pointing out the above because I wanted to think it through. But I don't think it's a reason to avoid going with this change. This change actually increases consistency (in that we return resp and resp.Rate in more cases).

So, I think I'm okay with doing this, but I want to take another day to think it over. Meanwhile, I'd love to hear your perspective on the above @gmlewis.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You make excellent points and I have mixed feelings too. Please know that I am completely comfortable with the decision to simply revert the code changes and keep the updates to the documentation.

There is no urgency here so we can let this sit for some time in case others have opinions.

Rate: err.Rate,
}, err
}

resp, err := c.client.Do(req)
Expand Down Expand Up @@ -457,7 +460,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
// current client state in order to quickly check if *RateLimitError can be immediately returned
// from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unnecessarily.
// Otherwise it returns nil, and Client.Do should proceed normally.
func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rateLimitCategory) error {
func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rateLimitCategory) *RateLimitError {
c.rateMu.Lock()
rate := c.rateLimits[rateLimitCategory]
c.rateMu.Unlock()
Expand Down