Skip to content

Commit ad32dc4

Browse files
committed
fixup! feat: add DisableRateLimitCheck option to client
1 parent 4bc5ba2 commit ad32dc4

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ To detect a primary API rate limit error, you can check if the error is a
215215
repos, _, err := client.Repositories.List(ctx, "", nil)
216216
var rateErr *github.RateLimitError
217217
if errors.As(err, &rateError) {
218-
log.Printf("hit primary rate limit, used %d of %d\n", rateErr.Rate.Used, rateErr.rate.Limit)
218+
log.Printf("hit primary rate limit, used %d of %d\n", rateErr.Rate.Used, rateErr.rate.Limit)
219219
}
220220
```
221221

@@ -225,12 +225,12 @@ To detect an API secondary rate limit error, you can check if the error is an
225225
```go
226226
repos, _, err := client.Repositories.List(ctx, "", nil)
227227
var rateErr *github.AbuseRateLimitError
228-
if errors.As(err, &rateError) {
229-
log.Printf("hit secondary rate limit, retry after %v\n", rateErr.RetryAfter)
228+
if errors.As(err, &rateErr) {
229+
log.Printf("hit secondary rate limit, retry after %v\n", rateErr.RetryAfter)
230230
}
231231
```
232232

233-
If you hit the primary rate limit, you can use the `SleepUntilPrimaryRateLimitReset`
233+
If you hit the primary rate limit, you can use the `SleepUntilPrimaryRateLimitResetWhenRateLimited`
234234
method to block until the rate limit is reset.
235235

236236
```go

github/github_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ func testNewRequestAndDoFailureCategory(t *testing.T, methodName string, client
251251
client.BaseURL.Path = "/api-v3/"
252252
client.rateLimits[category].Reset.Time = time.Now().Add(10 * time.Minute)
253253
resp, err = f()
254+
if client.DisableRateLimitCheck {
255+
return
256+
}
254257
if bypass := resp.Request.Context().Value(BypassRateLimitCheck); bypass != nil {
255258
return
256259
}
@@ -1912,6 +1915,79 @@ func TestDo_rateLimit_abuseRateLimitError_maxDuration(t *testing.T) {
19121915
}
19131916
}
19141917

1918+
// Make network call if client has disabled the rate limit check.
1919+
func TestDo_rateLimit_disableRateLimitCheck(t *testing.T) {
1920+
t.Parallel()
1921+
client, mux, _ := setup(t)
1922+
client.DisableRateLimitCheck = true
1923+
1924+
reset := time.Now().UTC().Add(60 * time.Second)
1925+
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
1926+
requestCount := 0
1927+
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1928+
requestCount++
1929+
w.Header().Set(headerRateLimit, "5000")
1930+
w.Header().Set(headerRateRemaining, "5000")
1931+
w.Header().Set(headerRateUsed, "0")
1932+
w.Header().Set(headerRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
1933+
w.Header().Set(headerRateResource, "core")
1934+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
1935+
w.WriteHeader(http.StatusOK)
1936+
fmt.Fprintln(w, `{}`)
1937+
})
1938+
req, _ := client.NewRequest("GET", ".", nil)
1939+
ctx := context.Background()
1940+
resp, err := client.Do(ctx, req, nil)
1941+
if err != nil {
1942+
t.Errorf("Do returned unexpected error: %v", err)
1943+
}
1944+
if got, want := resp.StatusCode, http.StatusOK; got != want {
1945+
t.Errorf("Response status code = %v, want %v", got, want)
1946+
}
1947+
if got, want := requestCount, 1; got != want {
1948+
t.Errorf("Expected 1 request, got %d", got)
1949+
}
1950+
if got, want := client.rateLimits[CoreCategory].Remaining, 0; got != want {
1951+
t.Errorf("Expected 0 requests remaining, got %d", got)
1952+
}
1953+
}
1954+
1955+
// Make network call if client has bypassed the rate limit check.
1956+
func TestDo_rateLimit_bypassRateLimitCheck(t *testing.T) {
1957+
t.Parallel()
1958+
client, mux, _ := setup(t)
1959+
1960+
reset := time.Now().UTC().Add(60 * time.Second)
1961+
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
1962+
requestCount := 0
1963+
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1964+
requestCount++
1965+
w.Header().Set(headerRateLimit, "5000")
1966+
w.Header().Set(headerRateRemaining, "5000")
1967+
w.Header().Set(headerRateUsed, "0")
1968+
w.Header().Set(headerRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
1969+
w.Header().Set(headerRateResource, "core")
1970+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
1971+
w.WriteHeader(http.StatusOK)
1972+
fmt.Fprintln(w, `{}`)
1973+
})
1974+
req, _ := client.NewRequest("GET", ".", nil)
1975+
ctx := context.Background()
1976+
resp, err := client.Do(context.WithValue(ctx, BypassRateLimitCheck, true), req, nil)
1977+
if err != nil {
1978+
t.Errorf("Do returned unexpected error: %v", err)
1979+
}
1980+
if got, want := resp.StatusCode, http.StatusOK; got != want {
1981+
t.Errorf("Response status code = %v, want %v", got, want)
1982+
}
1983+
if got, want := requestCount, 1; got != want {
1984+
t.Errorf("Expected 1 request, got %d", got)
1985+
}
1986+
if got, want := client.rateLimits[CoreCategory].Remaining, 5000; got != want {
1987+
t.Errorf("Expected 5000 requests remaining, got %d", got)
1988+
}
1989+
}
1990+
19151991
func TestDo_noContent(t *testing.T) {
19161992
t.Parallel()
19171993
client, mux, _ := setup(t)

0 commit comments

Comments
 (0)