Documentation
¶
Overview ¶
Package ghquota is the GitHub API request budget every caller of the API in this CLI shares the design of: analyze's forge pass and the licence lookups.
GitHub's rate limit is the single most common way a tool like this produces a wrong answer. There are three ways to get it wrong and all three are in the prior art:
- issue-metrics catches the rate-limit exception and calls sys.exit(1). The whole run is lost, including the work already done.
- Several tools sleep until the reset, which for a core-quota exhaustion is up to an hour. A CI job does not have an hour.
- The worst option, and the tempting one: catch the error, return what you have, and say nothing. The report then quietly understates every count it touched, and looks exactly like a report from a repository that simply has less going on.
So: the budget stops the caller *before* the quota runs out, and what was not fetched is declared. A metric that ran out of budget is `truncated` with an `omittedCount` and a reason. It is not a failure, and it is not a silence.
Inside GitHub Actions the job token's quota belongs to the whole repository, so the remaining count a response reports is the same number in every job of the run. A floor on it is therefore a floor for the run, not just for this process: every job that stops at it stops at the same level, however many run at once.
Index ¶
- Constants
- func IsRateLimit(err error) bool
- type Budget
- func (b *Budget) Exhausted() bool
- func (b *Budget) Limit() (n int, known bool)
- func (b *Budget) Observe(resp *github.Response)
- func (b *Budget) ObserveHTTP(r *http.Response)
- func (b *Budget) Reason() string
- func (b *Budget) Refused()
- func (b *Budget) Remaining() (n int, known bool)
- func (b *Budget) Seed(limit, remaining int)
- func (b *Budget) Spend()
- func (b *Budget) Spent() int
- type Option
Constants ¶
const DefaultHint = "Re-run when the quota resets, or narrow --forge-window-days."
DefaultHint is the advice Reason ends with unless the caller gives its own.
DefaultShare is the fraction of the hourly limit a budget refuses to spend, when it is more than reserve. A fixed 100 was not enough headroom: inside GitHub Actions the job token's quota (1,000 an hour, 5,000 for an organisation's installation) belongs to the whole repository, and a forge pass on every push to the default branch spent it down to 100. CodeQL's SARIF upload and the release dispatch then failed with "API rate limit exceeded for installation". Keeping half the limit bounds what one run can take, whatever else runs beside it.
Variables ¶
This section is empty.
Functions ¶
func IsRateLimit ¶
IsRateLimit reports whether an error is GitHub telling us to stop. Distinguishing it from a real failure matters: one means "come back later", the other means something is broken, and treating them the same makes both harder to diagnose.
Types ¶
type Budget ¶
type Budget struct {
// contains filtered or unexported fields
}
Budget bounds a caller's GitHub API requests. It is safe for concurrent use.
func (*Budget) Exhausted ¶
Exhausted reports whether the caller should stop. A caller that ignores this and keeps going will get a 403 and lose the run; a caller that respects it keeps everything it has already gathered.
func (*Budget) Observe ¶
Observe records the rate-limit state GitHub reported on a response. Every call the caller makes should pass its response through here.
func (*Budget) ObserveHTTP ¶
ObserveHTTP is Observe for a response read with net/http rather than go-github. A response that is GitHub refusing the request for its rate limit, primary or secondary, exhausts the budget whatever the headers say.
func (*Budget) Reason ¶
Reason explains, in the words that will end up in the report's truncationReason, why the caller stopped early.
func (*Budget) Refused ¶
func (b *Budget) Refused()
Refused records that GitHub answered with a rate limit. Nothing more is asked: every further request would be refused the same way.
func (*Budget) Seed ¶
Seed records the quota GitHub reported before any request was spent (GET /rate_limit, which does not count against it). A caller that starts below the floor spends nothing.