Documentation
¶
Overview ¶
Package apilimit combines shared request budgets with bounded local work.
Index ¶
- Variables
- type Decision
- type Options
- type Scope
- type Service
- func (s *Service) Admit(ctx context.Context, scope Scope) (Decision, error)
- func (s *Service) Authentication(ctx context.Context) (Decision, error)
- func (s *Service) Enter(ctx context.Context) (func(), error)
- func (s *Service) EnterExpensive(ctx context.Context, principal string) (func(), error)
- func (s *Service) EnterRecovery(ctx context.Context) (func(), Decision, error)
- func (s *Service) Options() Options
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalid = errors.New("api limit: invalid policy") ErrBusy = errors.New("api limit: concurrent work capacity exhausted") )
Functions ¶
This section is empty.
Types ¶
type Decision ¶
type Decision struct {
Allowed bool
Policy string
Limit int64
Remaining int64
Reset time.Time
ResetAfter time.Duration
RetryAfter time.Duration
}
Decision exposes only the caller's quota, not other principals' shared usage.
type Options ¶
type Options struct {
Enabled bool `yaml:"enabled"`
AuthenticationPerMinute int64 `yaml:"authentication_per_minute"`
GlobalPerMinute int64 `yaml:"global_per_minute"`
PrincipalPerMinute int64 `yaml:"principal_per_minute"`
ProjectPerMinute int64 `yaml:"project_per_minute"`
RepositoryPerMinute int64 `yaml:"repository_per_minute"`
ExpensivePerMinute int64 `yaml:"expensive_per_minute"`
ExpensivePrincipalPerMinute int64 `yaml:"expensive_principal_per_minute"`
MaxConcurrent int `yaml:"max_concurrent"`
ExpensiveConcurrent int `yaml:"expensive_concurrent"`
ExpensivePrincipalConcurrent int `yaml:"expensive_principal_concurrent"`
MaxKeys int `yaml:"max_keys"`
BudgetTimeout time.Duration `yaml:"budget_timeout"`
ExpensiveTimeout time.Duration `yaml:"expensive_timeout"`
}
Options is immutable after constructing a Service. Rates are shared across replicas; concurrency limits apply to one process and have no waiting queue.
type Scope ¶
Scope carries authenticated and resolved identities, never arbitrary client labels. Empty resource scopes mean the request is not bound to one resource.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service owns one process's permits and shares rates through metadata. It starts no goroutines. Release must run after the handler actually returns, including when its context is canceled; never release early while work runs.
func (*Service) Admit ¶
Admit charges the accepted request's global, principal, cost and resolved resource scopes atomically. Rejections cannot consume another user's budget.
func (*Service) Authentication ¶
Authentication charges a shared coarse budget before credential verification.
func (*Service) EnterExpensive ¶
EnterExpensive refuses excess work immediately; no caller can fill a pending queue, and one principal cannot occupy every expensive permit by default.
func (*Service) EnterRecovery ¶
EnterRecovery reserves two requests and sixty authentication attempts per minute in this process for the narrowly mounted administrative recovery API. Its constant-size state is independent of metadata budget capacity/failure. Recovery still requires valid administrator credentials and a working store.