Documentation
¶
Overview ¶
Package limits implements the state behind routing limits: live in-flight counters per upstream account/service, sliding usage windows, and the all-or-nothing permits that tie both to a request's lifetime. It knows nothing about routing, gin or the database; see .design/routing-limits.md.
Index ¶
- type Lease
- type Permit
- type QuotaFunc
- type Reason
- type Registry
- func (r *Registry) Assess(subjects []Subject) Verdict
- func (r *Registry) Changed() <-chan struct{}
- func (r *Registry) Keys(prefix string) []string
- func (r *Registry) Reap() int
- func (r *Registry) Record(subjects []Subject, tokens int64)
- func (r *Registry) Seed(key string, l ai.Limits, events []UsageEvent)
- func (r *Registry) SetMaxHold(d time.Duration)
- func (r *Registry) SetQuotaFunc(fn QuotaFunc)
- func (r *Registry) StartSweeper(ctx context.Context)
- func (r *Registry) Stat(key string) Stat
- func (r *Registry) TryAcquire(subjects []Subject, force bool) (*Permit, Verdict)
- func (r *Registry) Utilization(subjects []Subject) Utilization
- func (r *Registry) WindowEmpty(key string, l ai.Limits) bool
- type Scope
- type Stat
- type State
- type Subject
- type UsageEvent
- type Utilization
- type Verdict
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lease ¶
type Lease struct {
// contains filtered or unexported fields
}
Lease owns the permits of one client request. It lives in the request context; a middleware calls ReleaseAll when the handler returns, which covers streaming completion, client cancellation and panics. Failover swaps the permit of the failed attempt for the next one.
func (*Lease) Hold ¶
Hold registers an attempt's permit under its service ID. If the lease is already closed the permit is released immediately (a late arrival must not leak).
func (*Lease) ReleaseAll ¶
func (l *Lease) ReleaseAll()
ReleaseAll frees everything and closes the lease.
type Permit ¶
type Permit struct {
// contains filtered or unexported fields
}
Permit is one attempt's hold on a set of subjects.
type QuotaFunc ¶
QuotaFunc returns the upstream quota used percentage (0-100) for a provider; ok=false means unknown, which never blocks.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds all counters. The zero value is not usable; use NewRegistry.
func (*Registry) Changed ¶
func (r *Registry) Changed() <-chan struct{}
Changed returns a channel closed on the next release; waiters re-assess then.
func (*Registry) Reap ¶
Reap force-releases permits held longer than the leak-guard hold time and drops idle counters. Returns the number of permits reaped.
func (*Registry) Seed ¶
func (r *Registry) Seed(key string, l ai.Limits, events []UsageEvent)
Seed adds historical usage (warm start after a restart) to a key's window.
func (*Registry) SetMaxHold ¶
SetMaxHold overrides the leak-guard hold time (tests).
func (*Registry) SetQuotaFunc ¶
SetQuotaFunc wires upstream quota lookups (optional). Lookups are cached for a few seconds so the request path never pays a store read per candidate.
func (*Registry) StartSweeper ¶
StartSweeper runs Reap periodically until ctx is done.
func (*Registry) TryAcquire ¶
TryAcquire atomically re-checks the subjects and, if none is blocked (or force is set), counts the request: it takes one in-flight slot on every subject and one request in every usage window. The returned Permit must be released exactly once per attempt (Release is idempotent).
func (*Registry) Utilization ¶
func (r *Registry) Utilization(subjects []Subject) Utilization
Utilization reports the tightest (highest) load per dimension across subjects.
type Scope ¶
type Scope uint8
Scope says which kind of entity a Subject is, so a blocked verdict can name who is exhausted (the person, the team, the upstream).
type Subject ¶
type Subject struct {
Key string
Scope Scope
Limits ai.Limits
// Quota is the provider UUID whose upstream quota Limits.QuotaPercent is
// checked against. Empty = no quota check.
Quota string
}
Subject is one counted entity (an account, one rule's share of it, a model, a person or a team) and the limits that apply to it.
type UsageEvent ¶
UsageEvent is a historical usage sample used by Seed.
type Utilization ¶
Utilization is the per-dimension load of a set of subjects in percent (not capped: 100 = at the hard limit). Has* is false when no subject configures that dimension, so callers can tell "unlimited" from "idle".
type Verdict ¶
type Verdict struct {
State State
Reasons Reason
// Scopes is the set of subject scopes that are blocked (hard limit hit).
Scopes Scope
// Pressure is the largest used/limit ratio across all dimensions
// (1.0 = at the hard limit; 0 when nothing is limited).
Pressure float64
// RetryAfter is a hint for how long until a blocked usage budget frees up
// (or a short default for pure concurrency).
RetryAfter time.Duration
}
Verdict is the result of assessing a set of subjects.
func (Verdict) ConcurrencyOnly ¶
ConcurrencyOnly reports a blocked verdict that waiting could resolve within seconds: only in-flight caps are hit.