Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gofri/go-github-ratelimit
A GoLang HTTP RoundTripper that handles GitHub API secondary rate limits
https://github.com/gofri/go-github-ratelimit
Last synced: 3 months ago
JSON representation
A GoLang HTTP RoundTripper that handles GitHub API secondary rate limits
- Host: GitHub
- URL: https://github.com/gofri/go-github-ratelimit
- Owner: gofri
- License: mit
- Created: 2023-01-26T17:06:52.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-21T08:31:05.000Z (10 months ago)
- Last Synced: 2024-06-28T05:35:15.516Z (5 months ago)
- Language: Go
- Size: 54.7 KB
- Stars: 35
- Watchers: 1
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-github-ratelimit
[![Go Report Card](https://goreportcard.com/badge/github.com/gofri/go-github-ratelimit)](https://goreportcard.com/report/github.com/gofri/go-github-ratelimit)
Package `go-github-ratelimit` provides an http.RoundTripper implementation that handles [secondary rate limit](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits) for the GitHub API.
The RoundTripper waits for the secondary rate limit to finish in a blocking mode and then issues/retries requests.`go-github-ratelimit` can be used with any HTTP client communicating with GitHub API.
It is meant to complement [go-github](https://github.com/google/go-github), but there is no association between this repository and the go-github repository nor Google.
## Installation```go get github.com/gofri/go-github-ratelimit```
## Usage Example (with [go-github](https://github.com/google/go-github))
```go
import "github.com/google/go-github/v58/github"
import "github.com/gofri/go-github-ratelimit/github_ratelimit"func main() {
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil)
if err != nil {
panic(err)
}
client := github.NewClient(rateLimiter).WithAuthToken("your personal access token")// now use the client as you please
}
```## Client Options
The RoundTripper accepts a set of options to configure its behavior and set callbacks. nil callbacks are treated as no-op.
The options are:- `WithLimitDetectedCallback(callback)`: the callback is triggered before a sleep.
- `WithSingleSleepLimit(duration, callback)`: limit the sleep duration for a single secondary rate limit & trigger a callback when the limit is exceeded.
- `WithTotalSleepLimit(duration, callback)`: limit the accumulated sleep duration for all secondary rate limits & trigger a callback when the limit is exceeded.
_Note_: to detect secondary rate limits without sleeping, use `WithSingleSleepLimit(0, your_callback_or_nil)`.## Per-Request Options
Use `WithOverrideConfig(opts...)` to override the configuration for a specific request (using the request context).
Per-request overrides may be useful for special cases of user requests,
as well as fine-grained policy control (e.g., for a sophisticated pagination mechanism).## License
This package is distributed under the MIT license found in the LICENSE file.
Contribution and feedback is welcome.