lua

package
v0.30.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 14 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountBudgetCrossing

func CountBudgetCrossing(rung limits.Rung, mode budget.Mode)

CountBudgetCrossing counts a budget reaching its cap in relay_limits_budget_crossings_total, for a limiter that sees the crossing outside these scripts.

func Rule

func Rule(c *limits.Counter) (pkgratelimit.Rule, error)

Rule renders c as the limiter rule that counts it, for a limiter that counts some counters outside these scripts in the same store keys.

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

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

func (l *Limiter) OnBudgetCrossed(fn func(*limits.Counter)) *Limiter

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

func (l *Limiter) Peek(ctx context.Context, cs []limits.Counter) ([]limits.Reading, error)

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

func (l *Limiter) PrepareAdmit(b *kv.Batch, cs []limits.Counter) (limits.PreparedAdmit, error)

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) Finish

func (NoWrites) Finish(context.Context)

func (NoWrites) Resend

func (NoWrites) Resend(context.Context)

func (NoWrites) TransportFailed

func (NoWrites) TransportFailed() bool

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

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.

Jump to

Keyboard shortcuts

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