Skip to content

Commit fdee37d

Browse files
committed
Predict *RateLimitError, return immediately without network call.
When possible to do so reliably, i.e., the client knows that the rate limit is exceeded and reset time is in the future, immediately return *RateLimitError without making network calls to GitHub API. Add a unit test covering RateLimits method populating client.rateLimits. Remove commented out code. Helps google#152 and google#153.
1 parent 7da55f7 commit fdee37d

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎github/github.go‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,16 @@ func (c *Client) Rate() Rate {
333333
// JSON decoded and stored in the value pointed to by v, or returned as an
334334
// error if an API error has occurred. If v implements the io.Writer
335335
// interface, the raw response body will be written to v, without attempting to
336-
// first decode it.
336+
// first decode it. If rate limit is exceeded and reset time is in the future,
337+
// Do returns *RateLimitError immediately without making a network API call.
337338
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
338339
rateLimitCategory := category(req.URL.Path)
339340

341+
// If we've hit rate limit, don't make further requests before Reset time.
342+
if err := c.checkRateLimitBeforeDo(req, rateLimitCategory); err != nil {
343+
return nil, err
344+
}
345+
340346
resp, err := c.client.Do(req)
341347
if err != nil {
342348
return nil, err
@@ -376,6 +382,33 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
376382
return response, err
377383
}
378384

385+
// checkRateLimitBeforeDo does not make any network calls, but uses existing knowledge from
386+
// current client state in order to quickly check if *RateLimitError can be immediately returned
387+
// from Client.Do, and if so, returns it so that Client.Do can skip making a network API call unneccessarily.
388+
// Otherwise it returns nil, and Client.Do should proceed normally.
389+
func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rateLimitCategory) error {
390+
c.rateMu.Lock()
391+
rate := c.rateLimits[rateLimitCategory]
392+
c.rateMu.Unlock()
393+
if !rate.Reset.Time.IsZero() && rate.Remaining == 0 && time.Now().Before(rate.Reset.Time) {
394+
// Create a fake response.
395+
resp := &http.Response{
396+
Status: http.StatusText(http.StatusForbidden),
397+
StatusCode: http.StatusForbidden,
398+
Request: req,
399+
Header: make(http.Header),
400+
Body: ioutil.NopCloser(strings.NewReader("")),
401+
}
402+
return &RateLimitError{
403+
Rate: rate,
404+
Response: resp,
405+
Message: fmt.Sprintf("API rate limit of %v still exceeded until %v, not making remote request.", rate.Limit, rate.Reset.Time),
406+
}
407+
}
408+
409+
return nil
410+
}
411+
379412
/*
380413
An ErrorResponse reports one or more errors caused by an API request.
381414

‎github/github_test.go‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,60 @@ func TestDo_rateLimit_rateLimitError(t *testing.T) {
485485
}
486486
}
487487

488+
// Ensure a network call is not made when it's known that API rate limit is still exceeded.
489+
func TestDo_rateLimit_noNetworkCall(t *testing.T) {
490+
setup()
491+
defer teardown()
492+
493+
reset := time.Now().UTC().Round(time.Second).Add(time.Minute) // Rate reset is a minute from now, with 1 second precision.
494+
495+
mux.HandleFunc("/first", func(w http.ResponseWriter, r *http.Request) {
496+
w.Header().Add(headerRateLimit, "60")
497+
w.Header().Add(headerRateRemaining, "0")
498+
w.Header().Add(headerRateReset, fmt.Sprint(reset.Unix()))
499+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
500+
w.WriteHeader(http.StatusForbidden)
501+
fmt.Fprintln(w, `{
502+
"message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
503+
"documentation_url": "https://developer.github.com/v3/#rate-limiting"
504+
}`)
505+
})
506+
507+
madeNetworkCall := false
508+
mux.HandleFunc("/second", func(w http.ResponseWriter, r *http.Request) {
509+
madeNetworkCall = true
510+
})
511+
512+
// First request is made, and it makes the client aware of rate reset time being in the future.
513+
req, _ := client.NewRequest("GET", "/first", nil)
514+
client.Do(req, nil)
515+
516+
// Second request should not cause a network call to be made, since client can predict a rate limit error.
517+
req, _ = client.NewRequest("GET", "/second", nil)
518+
_, err := client.Do(req, nil)
519+
520+
if madeNetworkCall {
521+
t.Fatal("Network call was made, even though rate limit is known to still be exceeded.")
522+
}
523+
524+
if err == nil {
525+
t.Error("Expected error to be returned.")
526+
}
527+
rateLimitErr, ok := err.(*RateLimitError)
528+
if !ok {
529+
t.Fatalf("Expected a *RateLimitError error; got %#v.", err)
530+
}
531+
if got, want := rateLimitErr.Rate.Limit, 60; got != want {
532+
t.Errorf("rateLimitErr rate limit = %v, want %v", got, want)
533+
}
534+
if got, want := rateLimitErr.Rate.Remaining, 0; got != want {
535+
t.Errorf("rateLimitErr rate remaining = %v, want %v", got, want)
536+
}
537+
if rateLimitErr.Rate.Reset.UTC() != reset {
538+
t.Errorf("rateLimitErr rate reset = %v, want %v", rateLimitErr.Rate.Reset.UTC(), reset)
539+
}
540+
}
541+
488542
func TestDo_noContent(t *testing.T) {
489543
setup()
490544
defer teardown()
@@ -635,7 +689,6 @@ func TestRateLimit(t *testing.T) {
635689
if m := "GET"; m != r.Method {
636690
t.Errorf("Request method = %v, want %v", r.Method, m)
637691
}
638-
//fmt.Fprint(w, `{"resources":{"core": {"limit":2,"remaining":1,"reset":1372700873}}}`)
639692
fmt.Fprint(w, `{"resources":{
640693
"core": {"limit":2,"remaining":1,"reset":1372700873},
641694
"search": {"limit":3,"remaining":2,"reset":1372700874}
@@ -691,6 +744,13 @@ func TestRateLimits(t *testing.T) {
691744
if !reflect.DeepEqual(rate, want) {
692745
t.Errorf("RateLimits returned %+v, want %+v", rate, want)
693746
}
747+
748+
if got, want := client.rateLimits[coreCategory], *want.Core; got != want {
749+
t.Errorf("client.rateLimits[coreCategory] is %+v, want %+v", got, want)
750+
}
751+
if got, want := client.rateLimits[searchCategory], *want.Search; got != want {
752+
t.Errorf("client.rateLimits[searchCategory] is %+v, want %+v", got, want)
753+
}
694754
}
695755

696756
func TestUnauthenticatedRateLimitedTransport(t *testing.T) {

0 commit comments

Comments
 (0)