ghquota

package
v4.26.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const DefaultShare = 0.5

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

func IsRateLimit(err error) bool

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 NewBudget

func NewBudget(opts ...Option) *Budget

NewBudget returns a budget that keeps DefaultShare of the limit.

func (*Budget) Exhausted

func (b *Budget) Exhausted() bool

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) Limit

func (b *Budget) Limit() (n int, known bool)

Limit reports the hourly limit, and whether GitHub actually told us.

func (*Budget) Observe

func (b *Budget) Observe(resp *github.Response)

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

func (b *Budget) ObserveHTTP(r *http.Response)

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

func (b *Budget) Reason() string

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) Remaining

func (b *Budget) Remaining() (n int, known bool)

Remaining reports the quota left, and whether GitHub actually told us.

func (*Budget) Seed

func (b *Budget) Seed(limit, remaining int)

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.

func (*Budget) Spend

func (b *Budget) Spend()

Spend counts a request whose response carried no rate-limit state the caller could read (one made through the gh CLI, say). It still counts toward Spent.

func (*Budget) Spent

func (b *Budget) Spent() int

Spent reports how many requests were made.

type Option

type Option func(*Budget)

Option configures a Budget.

func Hint

func Hint(h string) Option

Hint sets the advice Reason ends with.

func Keep

func Keep(share float64) Option

Keep sets the fraction of the hourly limit the budget refuses to spend. A caller whose requests matter less than the other users of the token keeps more.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL