Documentation
¶
Overview ¶
Package limits declares what admission counts and the interface that counts it. A Counter describes one rate-limit rule or one budget a request must fit; plans build the list per route when they are reconciled (app/plan), and a Limiter admits a request against that list and settles it after the response.
Scope: the descriptor, its identity and atomicity group, and the Limiter and Reservation interfaces. Out of scope: every limiter implementation, store key formats, window and budget arithmetic, and deciding which counters a request touches.
Index ¶
- type BatchAdmitter
- type BatchSettler
- type Counter
- func BudgetCounter(rung Rung, b *budget.Budget, policyID, subjectID, group string) Counter
- func CallerRule(rung Rung, rl *ratelimit.RateLimit, r ratelimit.Rule, ...) Counter
- func SystemRule(rl *ratelimit.RateLimit, r ratelimit.Rule, subject string) Counter
- func UpstreamRule(rung Rung, rl *ratelimit.RateLimit, r ratelimit.Rule, ...) Counter
- type Kind
- type Limiter
- type PreparedAdmit
- type PreparedSettle
- type Reading
- type Refused
- type Reservation
- type Rung
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BatchAdmitter ¶
type BatchAdmitter interface {
// PrepareAdmit queues Admit's store calls for cs on b, which must be on the store the limiter admits on; the caller runs b, then calls Finish. On an error b must not run: it may hold part of the admission.
PrepareAdmit(b *kv.Batch, cs []Counter) (PreparedAdmit, error)
}
BatchAdmitter is a Limiter that can queue an admission on a kv batch the caller runs, so the caller's own store calls share its round trip.
type BatchSettler ¶
type BatchSettler interface {
// PrepareSettle does Settle's in-memory part now and queues its store writes on b, which must be on the store the limiter settles on; the caller runs b, then calls Finish on the result.
PrepareSettle(b *kv.Batch, actual Usage) PreparedSettle
}
BatchSettler is a Reservation that can queue its Settle on a kv batch the caller runs, so the settles of many requests share a round trip.
type Counter ¶
type Counter struct {
Kind Kind
Rung Rung
PolicyID string
// SubjectID is the account the policy is bound to, as "account:<id>".
SubjectID string
// Lane is set on a counter linked as each (not shared): the lane account it counts for, as "account:<id>".
Lane string
// ModelID is set when an upstream rule is scoped to models; such a rule counts once per matched model.
ModelID string
RateLimitID string
Rule ratelimit.Rule // KindRule
Budget *budget.Budget // KindBudget
// ID is the counter's stable identity: equal IDs count in one bucket. It survives edits that do not touch the counter (adding a model to the policy, renaming anything).
ID string
// Group is the atomicity group: counters sharing it may be updated in one store call.
Group string
}
Counter is one rate-limit rule or one budget a request must fit. Plans build it once; the request path hands it to a Limiter unchanged, so ID and Group are rendered here rather than per request.
func BudgetCounter ¶
BudgetCounter describes a budget linked by a policy at any rung. Its identity is (budget, bound account, period): an amount or mode edit keeps the spent count, a period change starts a new one.
func CallerRule ¶
func CallerRule(rung Rung, rl *ratelimit.RateLimit, r ratelimit.Rule, policyID, subjectID, group string) Counter
CallerRule describes a rule of a caller-side policy. Its identity is (rule, policy, bound account): a policy reused by two accounts gives each its own bucket, and rebinding an account to another policy starts a fresh one.
func SystemRule ¶
SystemRule describes a rule of a system rate limit counted per subject (a key hash, a client IP). It counts in a group of its own, apart from any caller's.
func UpstreamRule ¶
func UpstreamRule(rung Rung, rl *ratelimit.RateLimit, r ratelimit.Rule, policyID, subjectID, modelID, group string) Counter
UpstreamRule describes a rule of a provider account's policy. Its identity is (rule, account, model) without the policy, so the provider's ceiling keeps its count when the account is rebound to another tier policy that links the same rate limit, and every caller reaching the account shares it.
type Limiter ¶
type Limiter interface {
// Admit checks every counter (rules reserve the estimate; budgets compare spent with the cap) and either reserves all of them or refuses with a *Refused naming the first counter that refused; on refusal nothing stays reserved. A store failure refuses: limits fail closed.
Admit(ctx context.Context, cs []Counter, est Usage) (Reservation, error)
// Peek reads every counter's current count, one Reading per counter in order, without counting anything: no store write and no TTL change. For admin reads, never the request path.
Peek(ctx context.Context, cs []Counter) ([]Reading, error)
}
Limiter admits a request against its counters and settles it after the response. Implementations are swappable; the plans never count.
type PreparedAdmit ¶
type PreparedAdmit interface {
// Finish returns what Admit would have, once the batch has run.
Finish(ctx context.Context) (Reservation, error)
}
PreparedAdmit is an admission queued on a kv batch.
type PreparedSettle ¶
type PreparedSettle interface {
// TransportFailed reports whether a queued write failed in transport rather than with its own error reply: whether it ran is unknown.
TransportFailed() bool
// Finish reads the outcome once the batch has run.
Finish(ctx context.Context)
// Resend writes the settle again on a round trip of its own; a commit that already ran is a no-op.
Resend(ctx context.Context)
}
PreparedSettle is a Settle queued on a kv batch.
type Reading ¶
Reading is a counter's count as Admit would compare it with the cap, before a new request: requests, tokens or concurrency slots for a rule, USD nanos spent this period for a budget. Fractional counts round up.
type Reservation ¶
type Reservation interface {
// Settle charges the real usage; called once, after the response, off the request path.
Settle(actual Usage)
// Release undoes the reservation when the request never reached the upstream.
Release()
}
Reservation is one admitted request's hold on its counters.
type Rung ¶
type Rung string
Rung is the level of the policy ladder a counter's policy is bound at.
const ( // RungAccount is a provider account, the upstream side. RungAccount Rung = "account" RungTeam Rung = "team" RungProject Rung = "project" // RungLane is the account a request runs in: a personal, member or service account. RungLane Rung = "lane" // RungSystem marks a relay-wide ceiling no policy binds, such as the proxy-mode limit. RungSystem Rung = "system" )
Directories
¶
| Path | Synopsis |
|---|---|
|
Package hybrid implements limits.Limiter by counting in pod memory, synced with the shared store once per interval, and by counting through the Lua limiter (app/limits/lua) the counters a stale count could push past their cap.
|
Package hybrid implements limits.Limiter by counting in pod memory, synced with the shared store once per interval, and by counting through the Lua limiter (app/limits/lua) the counters a stale count could push past their cap. |
|
Package lua implements limits.Limiter over the Lua rate limiter in pkg/ratelimit: each atomicity group of a request's counters is reserved by one script whose keys share the group's hash tag, and every group's script goes out in one pipelined round trip.
|
Package lua implements limits.Limiter over the Lua rate limiter in pkg/ratelimit: each atomicity group of a request's counters is reserved by one script whose keys share the group's hash tag, and every group's script goes out in one pipelined round trip. |