diff --git a/README.md b/README.md index 8078e20d517..8ea0097aa33 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/github/doc.go b/github/doc.go index 875f039648a..7e9a0814207 100644 --- a/github/doc.go +++ b/github/doc.go @@ -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: diff --git a/github/github.go b/github/github.go index c37d170b31e..9aadae40c40 100644 --- a/github/github.go +++ b/github/github.go @@ -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, + Rate: err.Rate, + }, err } resp, err := c.client.Do(req) @@ -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()