ghutil

package
v0.51.78 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const RateLimitThreshold = 10

Variables

View Source
var ErrRateLimited = errors.New("github token rate limited")

ErrRateLimited is returned when a GitHub API token has hit its rate limit. Callers with a token pool should exhaust this token and try another.

Functions

func AbuseRetryAfter

func AbuseRetryAfter(err error) time.Duration

AbuseRetryAfter returns the retry-after duration if the error is a secondary (abuse) rate limit error. Returns 0 if the error is not an abuse rate limit.

func BackoffDuration added in v0.51.49

func BackoffDuration(attempt int, base, maxDelay time.Duration) time.Duration

BackoffDuration returns the sleep duration for retry attempt `attempt` (0-indexed) using bounded exponential backoff with jitter. Capped at max.

attempt 0 -> base + jitter
attempt 1 -> 2*base + jitter
attempt 2 -> 4*base + jitter
... capped at max.

func CheckRateLimit

func CheckRateLimit(ctx context.Context, resp *github.Response) error

func CheckTokenQuota added in v0.30.3

func CheckTokenQuota(ctx context.Context, token string) int

CheckTokenQuota calls the GitHub rate_limit API (free, no quota cost) and returns the remaining quota. Returns -1 on error (treat as usable).

func Deref

func Deref(s *string) string

func GetGitHubDeveloper

func GetGitHubDeveloper(ctx context.Context, client *http.Client, username string) (*data.Developer, error)

func GetLabels

func GetLabels(labels []*github.Label) []string

func GetUsernames

func GetUsernames(users ...*github.User) []string

func HasSufficientQuota added in v0.30.3

func HasSufficientQuota(ctx context.Context, token string) bool

HasSufficientQuota returns true if the token has enough remaining API calls.

func IsRateLimited added in v0.28.0

func IsRateLimited(err error) bool

IsRateLimited reports whether err (or any error in its chain) indicates a GitHub rate limit has been reached. Matches ErrRateLimited, github.RateLimitError, and github.AbuseRateLimitError.

func IsServerError added in v0.45.5

func IsServerError(err error) bool

IsServerError reports whether err (or any error in its chain) indicates a GitHub server error (5xx). These are transient and worth retrying.

func IsTransient added in v0.51.49

func IsTransient(err error) bool

IsTransient reports whether err looks like a transient network or upstream failure that may succeed on retry. Distinct from IsRateLimited: callers generally want to first WaitForRateReset (rate-limit handling) and then fall back to backoff for transient errors.

Returns true for: GitHub 5xx, io.EOF / io.ErrUnexpectedEOF, net.Error timeouts, and well-known transient strings ("connection reset by peer", "broken pipe", "EOF"). Returns false for context cancellation/deadline (those reflect a deliberate caller decision) and for non-network errors.

func MapUserToDeveloper

func MapUserToDeveloper(u *github.User) *data.Developer

func MinTokenQuota added in v0.30.3

func MinTokenQuota() int

MinTokenQuota returns the minimum remaining API calls required for a token to be included in the pool.

func ParseDate

func ParseDate(t *time.Time) string

func ParseUsers

func ParseUsers(body *string) []string

func RateInfo

func RateInfo(r *github.Rate) string

func SleepWithContext added in v0.51.49

func SleepWithContext(ctx context.Context, d time.Duration) error

SleepWithContext sleeps for d, returning early with ctx.Err() if the context is canceled. Returns nil on full sleep.

func Trim

func Trim(s *string) string

func WaitForRateReset added in v0.17.6

func WaitForRateReset(ctx context.Context, err error) bool

WaitForRateReset checks whether err is a GitHub primary or secondary rate limit error. If so it sleeps until the reset time (plus jitter) and returns true so the caller can retry. Returns false for non-rate-limit errors or if the wait would exceed maxWait.

Types

type TokenPool

type TokenPool struct {
	// contains filtered or unexported fields
}

TokenPool manages a pool of GitHub API tokens using round-robin selection. Safe for concurrent use. Works transparently with a single token. Tracks per-token usage counts for observability and supports marking individual tokens as exhausted (e.g. after hitting a rate limit).

Exhausted tokens automatically re-enter rotation after exhaustionWindow, so the pool degrades temporarily rather than monotonically.

func NewTokenPool

func NewTokenPool(tokens ...string) *TokenPool

NewTokenPool creates a pool from one or more tokens. Tokens can be passed individually or as a single comma-separated string.

func (*TokenPool) ActiveCount added in v0.28.0

func (p *TokenPool) ActiveCount() int

ActiveCount returns the number of non-exhausted tokens.

func (*TokenPool) Exhaust added in v0.28.0

func (p *TokenPool) Exhaust(token string)

Exhaust marks the given token as exhausted so Token() skips it for the configured exhaustion window. After the window elapses, the token is eligible for selection again automatically.

func (*TokenPool) Size

func (p *TokenPool) Size() int

Size returns the total number of tokens in the pool (including exhausted).

func (*TokenPool) Token

func (p *TokenPool) Token() string

Token returns the next non-exhausted token in the round-robin rotation. Returns "" when all tokens are exhausted or the pool is empty.

func (*TokenPool) UsageCounts added in v0.18.0

func (p *TokenPool) UsageCounts() []int

UsageCounts returns a copy of per-token call counts (indexed by pool position).

type TokenQuota added in v0.51.2

type TokenQuota struct {
	Limit     int
	Remaining int
}

TokenQuota holds the limit and remaining fields from the GitHub rate_limit API.

func CheckTokenQuotaFull added in v0.51.2

func CheckTokenQuotaFull(ctx context.Context, token string) *TokenQuota

CheckTokenQuotaFull calls the GitHub rate_limit API and returns both limit and remaining. Returns nil on error.

Jump to

Keyboard shortcuts

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