Documentation
¶
Index ¶
- Constants
- Variables
- func AbuseRetryAfter(err error) time.Duration
- func BackoffDuration(attempt int, base, maxDelay time.Duration) time.Duration
- func CheckRateLimit(ctx context.Context, resp *github.Response) error
- func CheckTokenQuota(ctx context.Context, token string) int
- func Deref(s *string) string
- func GetGitHubDeveloper(ctx context.Context, client *http.Client, username string) (*data.Developer, error)
- func GetLabels(labels []*github.Label) []string
- func GetUsernames(users ...*github.User) []string
- func HasSufficientQuota(ctx context.Context, token string) bool
- func IsRateLimited(err error) bool
- func IsServerError(err error) bool
- func IsTransient(err error) bool
- func MapUserToDeveloper(u *github.User) *data.Developer
- func MinTokenQuota() int
- func ParseDate(t *time.Time) string
- func ParseUsers(body *string) []string
- func RateInfo(r *github.Rate) string
- func SleepWithContext(ctx context.Context, d time.Duration) error
- func Trim(s *string) string
- func WaitForRateReset(ctx context.Context, err error) bool
- type TokenPool
- type TokenQuota
Constants ¶
const RateLimitThreshold = 10
Variables ¶
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 ¶
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
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 CheckTokenQuota ¶ added in v0.30.3
CheckTokenQuota calls the GitHub rate_limit API (free, no quota cost) and returns the remaining quota. Returns -1 on error (treat as usable).
func GetGitHubDeveloper ¶
func GetUsernames ¶
func HasSufficientQuota ¶ added in v0.30.3
HasSufficientQuota returns true if the token has enough remaining API calls.
func IsRateLimited ¶ added in v0.28.0
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
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
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 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 ParseUsers ¶
func SleepWithContext ¶ added in v0.51.49
SleepWithContext sleeps for d, returning early with ctx.Err() if the context is canceled. Returns nil on full sleep.
func WaitForRateReset ¶ added in v0.17.6
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 ¶
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
ActiveCount returns the number of non-exhausted tokens.
func (*TokenPool) Exhaust ¶ added in v0.28.0
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) Token ¶
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
UsageCounts returns a copy of per-token call counts (indexed by pool position).
type TokenQuota ¶ added in v0.51.2
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.