Skip to content

docs: Update readme and examples for updated go-github-ratelimit and introduce go-github-pagination #3504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ repos, _, err := client.Repositories.List(context.WithValue(ctx, github.SleepUnt
```

You can use [gofri/go-github-ratelimit](https://github.com/gofri/go-github-ratelimit) to handle
secondary rate limit sleep-and-retry for you.
secondary rate limit sleep-and-retry for you, as well as primary rate limit abuse-prevention and callback triggering.

Learn more about GitHub secondary rate limiting in
["About secondary rate limits"](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits).
Expand Down Expand Up @@ -347,6 +347,13 @@ for repo := range repos.All() {

For complete usage of `enrichman/gh-iter`, see the full [package docs](https://github.com/enrichman/gh-iter).

#### Middleware ####

You can use [gofri/go-github-pagination](https://github.com/gofri/go-github-pagination) to handle
pagination for you. It supports both sync and async modes, as well as customizations.
By default, the middleware automatically paginates through all pages, aggregates results, and returns them as an array.
See `example/ratelimit/main.go` for usage.

### Webhooks ###

`go-github` provides structs for almost all [GitHub webhook events][] as well as functions to validate them and unmarshal JSON payloads from `http.Request` structs.
Expand Down
3 changes: 2 additions & 1 deletion example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.23.0
require (
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371
github.com/bradleyfalzon/ghinstallation/v2 v2.0.4
github.com/gofri/go-github-ratelimit v1.0.3
github.com/gofri/go-github-pagination v1.0.0
github.com/gofri/go-github-ratelimit/v2 v2.0.2
github.com/google/go-github/v69 v69.2.0
github.com/sigstore/sigstore-go v0.6.1
golang.org/x/crypto v0.32.0
Expand Down
6 changes: 4 additions & 2 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofri/go-github-ratelimit v1.0.3 h1:Ocs2jaYokZDzgvqaajX+g04dqFyVqL0JQzoO7d2wmlk=
github.com/gofri/go-github-ratelimit v1.0.3/go.mod h1:OnCi5gV+hAG/LMR7llGhU7yHt44se9sYgKPnafoL7RY=
github.com/gofri/go-github-pagination v1.0.0 h1:nnCi+1xT5ybqY/plctISgiQPWZOtfSciVQlbx/hM/Yw=
github.com/gofri/go-github-pagination v1.0.0/go.mod h1:Qij55Fb4fNPjam3SB+8cLnqp4pgR8RGMyIspYXcyHX0=
github.com/gofri/go-github-ratelimit/v2 v2.0.2 h1:gS8wAS1jTmlWGdTjAM7KIpsLjwY1S0S/gKK5hthfSXM=
github.com/gofri/go-github-ratelimit/v2 v2.0.2/go.mod h1:YBQt4gTbdcbMjJFT05YFEaECwH78P5b0IwrnbLiHGdE=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
Expand Down
32 changes: 21 additions & 11 deletions example/ratelimit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The ratelimit command demonstrates using the github_ratelimit.SecondaryRateLimitWaiter.
// The ratelimit command demonstrates using the github_ratelimit as well as github_pagination.
// By using the waiter, the client automatically sleeps and retry requests
// when it hits secondary rate limits.
// It also prevents the client from abusing the API in case of a primary rate limit.
package main

import (
"context"
"fmt"

"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/gofri/go-github-pagination/githubpagination"
"github.com/gofri/go-github-ratelimit/v2/github_ratelimit"
"github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit"
"github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit"
"github.com/google/go-github/v69/github"
)

Expand All @@ -21,22 +25,28 @@
fmt.Print("Enter GitHub username: ")
fmt.Scanf("%s", &username)

rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
rateLimiter := github_ratelimit.New(nil,
github_primary_ratelimit.WithLimitDetectedCallback(func(ctx *github_primary_ratelimit.CallbackContext) {
fmt.Printf("Primary rate limit detected: category %s, reset time: %v\n", ctx.Category, ctx.ResetTime)
}),
github_secondary_ratelimit.WithLimitDetectedCallback(func(ctx *github_secondary_ratelimit.CallbackContext) {
fmt.Printf("Secondary rate limit detected: reset time: %v, total sleep time: %v\n", ctx.ResetTime, ctx.TotalSleepTime)
}),

Check warning on line 34 in example/ratelimit/main.go

View check run for this annotation

Codecov / codecov/patch

example/ratelimit/main.go#L28-L34

Added lines #L28 - L34 were not covered by tests
)

client := github.NewClient(rateLimiter)
paginator := githubpagination.NewClient(rateLimiter,
githubpagination.WithPerPage(100), // default to 100 results per page
)
client := github.NewClient(paginator)

Check warning on line 40 in example/ratelimit/main.go

View check run for this annotation

Codecov / codecov/patch

example/ratelimit/main.go#L37-L40

Added lines #L37 - L40 were not covered by tests

// arbitrary usage of the client
organizations, _, err := client.Organizations.List(context.Background(), username, nil)
repos, _, err := client.Repositories.ListByUser(context.Background(), username, nil)

Check warning on line 43 in example/ratelimit/main.go

View check run for this annotation

Codecov / codecov/patch

example/ratelimit/main.go#L43

Added line #L43 was not covered by tests
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

for i, organization := range organizations {
fmt.Printf("%v. %v\n", i+1, organization.GetLogin())
for i, repo := range repos {
fmt.Printf("%v. %v\n", i+1, repo.GetName())

Check warning on line 50 in example/ratelimit/main.go

View check run for this annotation

Codecov / codecov/patch

example/ratelimit/main.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}
}
Loading