selector

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package selector implements GitHub App selection based on rate limit headroom.

Index

Constants

View Source
const (
	// DefaultRetrySeconds is the default retry-after value when no reset time is known.
	DefaultRetrySeconds = 60
	// MinRetrySeconds is the minimum retry-after value returned to clients.
	MinRetrySeconds = 1
)

Variables

View Source
var (
	// ErrInvalidRepository is returned when the repository format is invalid.
	ErrInvalidRepository = errors.New("invalid repository format: expected owner/repo")

	// ErrNoApps is returned when no GitHub Apps are configured.
	ErrNoApps = errors.New("apps cannot be empty")

	// ErrNilStore is returned when a nil store is provided.
	ErrNilStore = errors.New("store cannot be nil")

	// ErrStateNotFound is returned when no rate limit state exists for an app.
	ErrStateNotFound = errors.New("rate limit state not found")

	// ErrNoMatchingApp is returned when no apps are configured for the target organization.
	ErrNoMatchingApp = errors.New("no GitHub App configured for organization")
)

Functions

This section is empty.

Types

type App

type App struct {
	ClientID     string
	Organization string
}

App represents a GitHub App available for token issuance.

type ExhaustedError

type ExhaustedError struct {
	RetryAfter int
}

ExhaustedError is returned when all GitHub Apps are rate-limited.

func (*ExhaustedError) Error

func (e *ExhaustedError) Error() string

Error returns the formatted error message with retry-after duration.

type RateLimitState

type RateLimitState struct {
	Remaining   int
	ResetAt     time.Time
	LastUpdated time.Time
}

RateLimitState tracks the GitHub API rate limit for a single App.

func (*RateLimitState) HasCapacity

func (s *RateLimitState) HasCapacity() bool

HasCapacity returns true when there are remaining API calls available.

func (*RateLimitState) IsExpired

func (s *RateLimitState) IsExpired() bool

IsExpired returns true when the rate limit window has reset.

func (*RateLimitState) IsFresherThan

func (s *RateLimitState) IsFresherThan(other *RateLimitState) bool

IsFresherThan reports whether s represents a more recent observation than other. A state is fresher when it belongs to a newer rate limit window, or to the same window but with lower remaining (more usage observed).

type Selector

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

Selector chooses among GitHub Apps based on rate limit headroom.

func NewSelector

func NewSelector(apps []App, store Store) (*Selector, error)

NewSelector creates a Selector. Returns an error if apps is empty or store is nil.

func (*Selector) RecordUsage

func (s *Selector) RecordUsage(ctx context.Context, clientID string, remaining int, resetAt time.Time) error

RecordUsage updates the rate limit state for an app after an API call.

func (*Selector) RetryAfter

func (s *Selector) RetryAfter(ctx context.Context) int

RetryAfter returns the seconds until the earliest app window resets. Clamped to [MinRetrySeconds, DefaultRetrySeconds].

func (*Selector) SelectApp

func (s *Selector) SelectApp(ctx context.Context, repository string) (*App, error)

SelectApp returns an app with capacity for the given repository. Repository must be in "owner/repo" format. Returns *ExhaustedError when all matching apps are rate-limited.

type Store

type Store interface {
	// GetState returns the rate limit state for the app, or ErrStateNotFound if absent.
	GetState(ctx context.Context, clientID string) (*RateLimitState, error)
	// SetState updates the state for the app; stale writes may be ignored by the implementation.
	SetState(ctx context.Context, clientID string, state *RateLimitState) error
	// GetAllStates returns the current state for all known apps.
	GetAllStates(ctx context.Context) (map[string]*RateLimitState, error)
	// Close releases resources held by the store.
	Close() error
}

Store persists and retrieves GitHub App rate limit states. Implementations must be safe for concurrent use.

Directories

Path Synopsis
Package backends provides selector store implementations.
Package backends provides selector store implementations.

Jump to

Keyboard shortcuts

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