admission

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package admission provides request admission controls for existing middleware hooks. Applications own budgets, grouping, placement and transport mappings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RateLimitUnavailableError added in v1.12.0

func RateLimitUnavailableError() error

RateLimitUnavailableError returns the safe temporary *loom.ServiceError named "rate_limit_unavailable" that rate admission reports for grouping failures, backend errors, backend timeouts and malformed decisions. Its message never includes backend error text or grouping keys.

func RateLimitedError added in v1.12.0

func RateLimitedError() error

RateLimitedError returns the safe temporary *loom.ServiceError named "rate_limited" that transport adapters report for quota denial when the application declares no other denial error.

Types

type Concurrency

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

Concurrency bounds simultaneous endpoint invocations. Construct it with NewConcurrency and share the pointer across the endpoints in one budget. It must not be copied. Completion, including panic unwinding, releases the permit; cancellation alone does not. It is unsuitable for streams or work that continues after the wrapped endpoint returns.

func NewConcurrency

func NewConcurrency(capacity int) (*Concurrency, error)

NewConcurrency constructs an admission budget. Capacity must be positive; invalid configuration returns an error before any middleware is constructed. No queues, worker goroutines, retries or cross-process coordination are used.

func (*Concurrency) Middleware

func (c *Concurrency) Middleware(next loom.Endpoint) loom.Endpoint

Middleware wraps a unary endpoint with immediate admission. Saturation returns a *loom.ServiceError named "overloaded" with Temporary set. Declare that error in the design and map it explicitly, for example to HTTP 503. No retry occurs. A context canceled at admission returns ctx.Err() instead, even when saturated. After admission the original context and request are passed through unchanged; the endpoint's result, error and panic propagate unchanged.

Install through generated Endpoints.Use or a protected per-method constructor to retain validation and authorization before admission. This does not protect transport decoding or authentication. It panics for an uninitialized budget or nil endpoint, before requests can be served.

func (*Concurrency) SetCapacity

func (c *Concurrency) SetCapacity(capacity int) error

SetCapacity atomically changes the local limit without changing permits or counters. Nonpositive values return an error and leave the prior limit intact. Reductions affect new admissions only; existing invocations finish normally.

func (*Concurrency) Snapshot

func (c *Concurrency) Snapshot() ConcurrencySnapshot

Snapshot returns the effective capacity and accounting in one synchronized read. Applications may export these bounded counters and gauges to their metrics system.

type ConcurrencySnapshot

type ConcurrencySnapshot struct {
	// Capacity is the current positive admission limit.
	Capacity int
	// InFlight counts invocations that have acquired a permit and not exited.
	// It may exceed Capacity after a reduction.
	InFlight int
	// Admitted counts acquired permits, including calls that later fail or panic.
	Admitted uint64
	// Rejected counts calls denied because capacity was unavailable.
	Rejected uint64
	// Canceled counts calls denied because their context was already canceled
	// or expired at the admission decision. These are not also Rejected.
	Canceled uint64
}

ConcurrencySnapshot is a coherent local observation with no request labels. Counters cover calls reaching this admission boundary since construction.

type KeyedRate added in v1.12.0

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

KeyedRate keeps a separate process-local token bucket for each key, all sharing one RatePolicy, and stores at most a fixed number of keys. Construct it with NewKeyedRate; it must not be copied. Its Allow method is a RateCheck, so NewRateAdmission(keyed.Allow, timeout) admits requests by key. Applications still own trusted grouping: keys should be short, stable identifiers from a source that is costly to rotate, such as an authenticated tenant or a trusted peer address.

A bucket is evicted only when it has refilled to the full burst. A new bucket for the same key also starts full, so eviction never resets spent quota. When every stored bucket is partly spent and a new key arrives, the new key is denied with a RetryAfter that ends when the first stored bucket becomes full and can be evicted. Capacity pressure never admits a request. A spent bucket holds its slot for up to Burst/PerSecond after its last request, so size maxKeys for the peak number of distinct keys active within that time. Buckets are ordered by the time they become full, so Allow does O(log maxKeys) work even while keys rotate.

func NewKeyedRate added in v1.12.0

func NewKeyedRate(policy RatePolicy, maxKeys int) (*KeyedRate, error)

NewKeyedRate validates policy and a positive maxKeys and constructs an empty keyed budget at version 1. Each key's bucket starts full on first use.

func (*KeyedRate) Allow added in v1.12.0

func (k *KeyedRate) Allow(ctx context.Context, key string) (RateDecision, error)

Allow consumes one token from key's bucket if available, with the same semantics as Rate.Allow. A canceled context consumes nothing and stores no key. All keys share one mutex.

func (*KeyedRate) Len added in v1.12.0

func (k *KeyedRate) Len() int

Len returns the number of stored keys, which never exceeds the key limit.

func (*KeyedRate) Policy added in v1.12.0

func (k *KeyedRate) Policy() (RatePolicy, uint64)

Policy returns both effective settings and their version in one coherent read.

func (*KeyedRate) SetPolicy added in v1.12.0

func (k *KeyedRate) SetPolicy(policy RatePolicy) error

SetPolicy atomically applies both settings to every stored bucket and to buckets created later, with the same accounting rules as Rate.SetPolicy. Reapplying identical settings does not reset accounting or advance the version. Invalid updates leave all state unchanged.

type Rate

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

Rate wraps a maintained token bucket with atomic policy updates. Construct it with NewRate; it must not be copied. A Rate is one process-local budget, not a keyed store. Use KeyedRate for bounded per-key local budgets, or supply a shared backend to NewRateAdmission.

func NewRate

func NewRate(policy RatePolicy) (*Rate, error)

NewRate validates policy and constructs a full local bucket at version 1.

func (*Rate) Allow

func (r *Rate) Allow(ctx context.Context) (RateDecision, error)

Allow consumes one token if available, without waiting or reserving future allowance. A canceled context consumes nothing. Consumed tokens are not refunded if later validation, service execution or request cancellation fails. RetryAfter is advisory: other calls and policy changes can alter availability.

func (*Rate) Policy

func (r *Rate) Policy() (RatePolicy, uint64)

Policy returns both effective settings and their version in one coherent read.

func (*Rate) SetPolicy

func (r *Rate) SetPolicy(policy RatePolicy) error

SetPolicy atomically applies both settings. Accrue allowance under the old rate up to the update, retain that allowance (clamped to the new burst), then use the new rate for future refill. Increasing burst adds refill headroom, not immediate allowance. Reapplying identical settings does not reset accounting or advance the version. Invalid updates leave all state unchanged.

type RateAdmission

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

RateAdmission bounds calls to a quota backend and rejects its errors or invalid decisions. Construct it with NewRateAdmission. It creates no background goroutines, retries, queues or key storage. Its timeout bounds cooperative backends; it cannot forcibly stop a callback that ignores ctx.

func NewRateAdmission

func NewRateAdmission(check RateCheck, timeout time.Duration) (*RateAdmission, error)

NewRateAdmission validates the checker and its positive per-check time budget. Applications must configure network/storage timeouts in their checker too.

func (*RateAdmission) Check

func (a *RateAdmission) Check(ctx context.Context, key string) (RateDecision, error)

Check runs one backend check with a child deadline, retaining the caller's values and earlier deadline. Caller cancellation returns ctx.Err(). Backend errors, backend timeouts, malformed decisions and an uninitialized admission return a safe *loom.ServiceError named "rate_limit_unavailable". Backend error text and grouping keys are never included. No error permits execution.

func (*RateAdmission) Middleware

func (a *RateAdmission) Middleware(key func(context.Context, any) (string, error), denied func(RateDecision) error) func(loom.Endpoint) loom.Endpoint

Middleware admits unary endpoint calls using an application-owned grouping function and declared denial error. Install it through generated Endpoints.Use or protected constructors so grouping uses authenticated identity. A key error becomes "rate_limit_unavailable" without exposing its text. Denial invokes denied with the decision; it must return an error matching the design, including any known RetryAfter mapping. A nil denial error fails closed.

The next endpoint receives the original context, not the bounded backend context. Callback and next panics propagate. Invalid construction dependencies panic before serving. Streams and per-message quotas are outside this helper.

type RateCheck

type RateCheck func(ctx context.Context, key string) (RateDecision, error)

RateCheck checks and accounts for one request in an application-selected group. It must be safe for concurrent use and return promptly when ctx ends. A backend owns coherent policy/accounting and the scope of its quota. Keys must not be included in returned error messages, logs or metric labels.

type RateDecision

type RateDecision struct {
	// Allowed permits the request to proceed.
	Allowed bool
	// RetryAfter is a positive advisory delay until allowance may be available.
	// Zero means unknown. A successful decision must leave this field zero.
	RetryAfter time.Duration
	// Version identifies the coherent local policy used for this decision.
	// External checkers may use zero when they do not expose a version.
	Version uint64
}

RateDecision reports one admission decision. A denied decision with no error means quota exhaustion; backend failures are returned separately as errors.

func (RateDecision) RetryAfterSeconds

func (d RateDecision) RetryAfterSeconds() int64

RetryAfterSeconds rounds a known positive delay up to whole seconds without overflowing. It returns zero for an unknown delay. HTTP adapters can publish a positive value as Retry-After; unknown delays must omit that header.

type RatePolicy

type RatePolicy struct {
	// PerSecond is the number of tokens replenished per second.
	PerSecond float64
	// Burst is the maximum token allowance, initially available in full.
	Burst int
}

RatePolicy configures one local token bucket. Both fields must be positive, and PerSecond must be below math.MaxFloat64 (the dependency's unlimited sentinel). One admitted request consumes one token.

Jump to

Keyboard shortcuts

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