Skip to content

Commit 0dd861e

Browse files
committed
secondary rate limit example and guidance
1 parent 2a48744 commit 0dd861e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ if _, ok := err.(*github.RateLimitError); ok {
186186
Learn more about GitHub rate limiting at
187187
https://docs.github.com/en/rest/rate-limit .
188188

189+
In addition to these rate limits, GitHub imposes a secondary rate limit on all API clients.
190+
This rate limit prevents clients from making too many concurrent requests.
191+
192+
To detect an API secondary rate limit error, you can check if its type is `*github.AbuseRateLimitError`:
193+
194+
```go
195+
repos, _, err := client.Repositories.List(ctx, "", nil)
196+
if _, ok := err.(*github.AbuseRateLimitError); ok {
197+
log.Println("hit secondary rate limit")
198+
}
199+
```
200+
201+
You can use [go-github-ratelimit](https://github.com/gofri/go-github-ratelimit) to handle
202+
secondary rate limit sleep-and-retry for you.
203+
204+
Learn more about GitHub secondary rate limiting at
205+
https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#secondary-rate-limits .
206+
189207
### Accepted Status ###
190208

191209
Some endpoints may return a 202 Accepted status code, meaning that the

example/ratelimit/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
// The simple command demonstrates a simple functionality which
7+
// prompts the user for a GitHub username and lists all the public
8+
// organization memberships of the specified username.
9+
package main
10+
11+
import (
12+
"context"
13+
"fmt"
14+
15+
"github.com/gofri/go-github-ratelimit/github_ratelimit"
16+
"github.com/google/go-github/v49/github"
17+
)
18+
19+
func main() {
20+
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil)
21+
if err != nil {
22+
fmt.Printf("Error: %v\n", err)
23+
return
24+
}
25+
26+
client := github.NewClient(rateLimiter)
27+
28+
// arbitrary usage of the client
29+
username := "some-username"
30+
organizations, _, err := client.Organizations.List(context.Background(), username, nil)
31+
if err != nil {
32+
fmt.Printf("Error: %v\n", err)
33+
return
34+
}
35+
36+
for i, organization := range organizations {
37+
fmt.Printf("%v. %v\n", i+1, organization.GetLogin())
38+
}
39+
}

0 commit comments

Comments
 (0)