Documentation
¶
Overview ¶
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. A refusal or store failure in any group releases the groups that reserved, so a refused request holds nothing once the release lands: until then, about one round trip, its other groups stay counted, concurrency leases included, and may refuse a concurrent request. A release that fails is sent once more on a fresh deadline. When several groups refuse, the first in order (caller groups, then upstream groups, each in list order) is the one reported. Budgets are spend meters in the same scripts: a hard budget is checked at admission (refused once spent ≥ cap, retry-after at the next period start), an alert budget is not, and both are charged the real cost at Settle.
Scope: grouping, ordering and rollback across groups, mapping counters to limiter rules and refusals to counters, settlement, and reporting a budget's first crossing per period. Out of scope: which counters a request touches (app/plan), key and window arithmetic (pkg/ratelimit), and batching settles across requests.
Expected kv ops per request:
- Admit: one round trip: a pipeline of one script per group that checks something, or a single script call when the request has one group; a group of alert budgets alone queues none, so a request of alert budgets only makes no call. A refusal or failure in any group while others reserved adds one batched round trip, on the post-flight store, releasing them, and one more when that release fails.
- PrepareAdmit: the same scripts queued on the caller's kv.Batch, no round trip of its own; a refusal adds the same release.
- Settle: one batched round trip committing the groups that settle something (a concurrency lease, a token meter, a spend charge); none when nothing does.
- PrepareSettle: the same commits queued on the caller's kv.Batch (app/settle shares it across requests), no round trip of its own.
- Release: one batched round trip.
- Peek (admin reads, off the request path): one batched round trip of read-only scripts, one per group.
The admission estimate is unused: token meters are checked against what earlier requests used and charged at Settle, and budgets reserve nothing. A rollback or Release refunds every strategy's request count, so a refused or unsent request consumes no quota.
Index ¶
- func CountBudgetCrossing(rung limits.Rung, mode budget.Mode)
- func Rule(c *limits.Counter) (pkgratelimit.Rule, error)
- type Limiter
- func (l *Limiter) Admit(ctx context.Context, cs []limits.Counter, _ limits.Usage) (limits.Reservation, error)
- func (l *Limiter) OnBudgetCrossed(fn func(*limits.Counter)) *Limiter
- func (l *Limiter) Peek(ctx context.Context, cs []limits.Counter) ([]limits.Reading, error)
- func (l *Limiter) PrepareAdmit(b *kv.Batch, cs []limits.Counter) (limits.PreparedAdmit, error)
- func (l *Limiter) WithPostFlight(post *pkgratelimit.Limiter) *Limiter
- type NoWrites
- type PreparedAdmit
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountBudgetCrossing ¶
CountBudgetCrossing counts a budget reaching its cap in relay_limits_budget_crossings_total, for a limiter that sees the crossing outside these scripts.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter admits requests on one pkg/ratelimit.Limiter and settles them on another, which may be the same.
func New ¶
func New(lim *pkgratelimit.Limiter) *Limiter
New returns a Limiter that admits and settles on lim.
func (*Limiter) Admit ¶
func (l *Limiter) Admit(ctx context.Context, cs []limits.Counter, _ limits.Usage) (limits.Reservation, error)
Admit reserves every group of cs in one round trip. With a caller group the call sheds while the store's connection queue is full, as inbound admission does; upstream groups alone wait for a connection.
func (*Limiter) OnBudgetCrossed ¶
OnBudgetCrossed calls fn from Settle with each budget counter whose spend that settle took to its cap, once per budget and period. fn must not block. Call before serving.
func (*Limiter) Peek ¶
Peek reads cs from the store in one batched round trip, a read-only script per group, through the keys Admit reserves.
func (*Limiter) PrepareAdmit ¶
PrepareAdmit queues the reserve of every group of cs on b, as Admit does on a batch of its own; the caller runs b, then calls Finish. A group is reserved even when an earlier one refuses, and Finish releases it. When cs holds a caller group, b sheds while the store's connection queue is full.
func (*Limiter) WithPostFlight ¶
func (l *Limiter) WithPostFlight(post *pkgratelimit.Limiter) *Limiter
WithPostFlight runs rollbacks, Settle and Release on post instead. Call before serving.
type NoWrites ¶
type NoWrites struct{}
NoWrites is the prepared settle of a reservation that writes nothing.
func (NoWrites) TransportFailed ¶
type PreparedAdmit ¶
type PreparedAdmit struct {
// contains filtered or unexported fields
}
PreparedAdmit is an Admit queued on a kv batch: every group's reserve rides the batch, so the groups cost one round trip together and the caller may queue writes of its own ahead of them.
func (*PreparedAdmit) Finish ¶
func (p *PreparedAdmit) Finish(ctx context.Context) (limits.Reservation, error)
Finish returns what Admit would have once the batch has run: the reservation, or the refusal of the first group in order that refused, with every other group released.