Skip to content

Commit 415ab30

Browse files
authored
Return rate limit information immediately after checkRateLimitBeforeDo (google#572)
Return rate limit information immediately after checkRateLimitBeforeDo Fixes google#571.
1 parent cd662c8 commit 415ab30

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ limited to 60 requests per hour, while authenticated clients can make up to
8282
that are not issued on behalf of a user, use the
8383
`UnauthenticatedRateLimitedTransport`.
8484

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

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

github/doc.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ limited to 60 requests per hour, while authenticated clients can make up to
7171
that are not issued on behalf of a user, use the
7272
UnauthenticatedRateLimitedTransport.
7373
74-
The Rate method on a client returns the rate limit information based on the most
75-
recent API call. This is updated on every call, but may be out of date if it's
76-
been some time since the last API call and other clients have made subsequent
77-
requests since then. You can always call RateLimits() directly to get the most
78-
up-to-date rate limit data for the client.
74+
The returned Response.Rate value contains the rate limit information
75+
from the most recent API call. If a recent enough response isn't
76+
available, you can use RateLimits to fetch the most up-to-date rate
77+
limit data for the client.
7978
8079
To detect an API rate limit error, you can check if its type is *github.RateLimitError:
8180

github/github.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,10 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
396396

397397
// If we've hit rate limit, don't make further requests before Reset time.
398398
if err := c.checkRateLimitBeforeDo(req, rateLimitCategory); err != nil {
399-
return nil, err
399+
return &Response{
400+
Response: err.Response,
401+
Rate: err.Rate,
402+
}, err
400403
}
401404

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

0 commit comments

Comments
 (0)