From 46bafa5a330d78714d511a822e83533226c0a6af Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Fri, 3 Mar 2017 15:09:50 -0800 Subject: [PATCH 1/4] Return rate limit information immediately after `checkRateLimitBeforeDo` Fixes #571. Change-Id: Iefcc687d9fc56c40964d5e6f1cdbc54d020dae3c --- github/github.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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() From 0765ec68672d96712e818dbca54b6e7b714c3abc Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Fri, 3 Mar 2017 16:38:44 -0800 Subject: [PATCH 2/4] Update doc.go per review comment Change-Id: I09a8c2f3c841fb5f95539db7d2fb0b7e22e4956e --- github/doc.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/github/doc.go b/github/doc.go index 875f039648a..1f0b539258e 100644 --- a/github/doc.go +++ b/github/doc.go @@ -71,11 +71,8 @@ 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. +You can always call RateLimits() directly to get 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: From eb45bf984098dcd3517639699e978e65df8a4298 Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Mon, 10 Apr 2017 10:01:11 -0700 Subject: [PATCH 3/4] Address review feedback Change-Id: I1fbe502f4a4001fa2769483af01c8c11721c8aae --- github/doc.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/github/doc.go b/github/doc.go index 1f0b539258e..7e9a0814207 100644 --- a/github/doc.go +++ b/github/doc.go @@ -71,8 +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. -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: From 73e70c5fbed2882096ba5259d98b60c4810f8bed Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Mon, 10 Apr 2017 10:13:06 -0700 Subject: [PATCH 4/4] Address review feedback Change-Id: I1d66ec282fa35905c13982bdbcd94d78413465aa --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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`: