apilimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package apilimit combines shared request budgets with bounded local work.

Index

Constants

This section is empty.

Variables

View Source
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.

func Default

func Default() Options

Default returns the initial operational policy, including optional resource scopes once the HTTP boundary has resolved their existence.

func (Options) Validate

func (o Options) Validate() error

Validate rejects unsafe bounds even while disabled, so enabling a saved configuration does not expose a latent unbounded policy.

type Scope

type Scope struct {
	Principal, Project, Repository string
	Expensive                      bool
}

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 New

func New(st store.Store, options Options, now func() time.Time) (*Service, error)

func (*Service) Admit

func (s *Service) Admit(ctx context.Context, scope Scope) (Decision, error)

Admit charges the accepted request's global, principal, cost and resolved resource scopes atomically. Rejections cannot consume another user's budget.

func (*Service) Authentication

func (s *Service) Authentication(ctx context.Context) (Decision, error)

Authentication charges a shared coarse budget before credential verification.

func (*Service) Enter

func (s *Service) Enter(ctx context.Context) (func(), error)

Enter bounds all request work in this process, including authentication.

func (*Service) EnterExpensive

func (s *Service) EnterExpensive(ctx context.Context, principal string) (func(), error)

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

func (s *Service) EnterRecovery(ctx context.Context) (func(), Decision, error)

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.

func (*Service) Options

func (s *Service) Options() Options

Options returns a copy of the configured policy.

Jump to

Keyboard shortcuts

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