ratelimit

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ratelimit keeps a GitHub API client from spending requests to rediscover that it has none left.

GitHub answers an exhausted allowance with a 403 whose body reads like a permissions failure, so a client that does not inspect the headers reports the wrong cause and a retry loop spends a request per attempt learning the same thing. Transport turns that response into a typed error naming when the allowance returns, and refuses further requests to that pool until it does.

The pools are separate allowances rather than slices of one, so being out of core does not stop a GraphQL query. Pair this with a proactive read of what is left (forge/github.Budgets) to decide whether a burst is affordable before starting it.

Index

Constants

View Source
const (
	HeaderRemaining  = "X-RateLimit-Remaining"
	HeaderReset      = "X-RateLimit-Reset"
	HeaderLimit      = "X-RateLimit-Limit"
	HeaderResource   = "X-RateLimit-Resource"
	HeaderRetryAfter = "Retry-After"
)

Header names GitHub sets on every API response.

View Source
const (
	PoolCore    = "core"
	PoolGraphQL = "graphql"
	PoolSearch  = "search"
)

Pool names one of GitHub's separate hourly allowances.

Variables

This section is empty.

Functions

func PoolFor

func PoolFor(path string) string

PoolFor names the allowance a request path spends.

func Transport

func Transport(base http.RoundTripper) http.RoundTripper

Transport wraps base so an exhausted allowance surfaces as *Error and later requests to that pool fail immediately until it resets. Requests to a pool with allowance left are unaffected.

Types

type Error

type Error struct {
	RetryAt  time.Time
	Resource string
	Limit    int
}

Error reports an exhausted allowance and when it returns. Retrying before RetryAt fails without spending a request, so a caller should show the time rather than offering an immediate retry.

func Exhausted

func Exhausted(resp *http.Response, now time.Time) *Error

Exhausted returns an error only when resp says the allowance is spent, and nil otherwise. A 403 for permissions carries no remaining-count header and must reach the caller unchanged rather than be reported as a rate limit.

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

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