limits

package
v0.30.0 Latest Latest
Warning

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

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

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

func BudgetCounter(rung Rung, b *budget.Budget, policyID, subjectID, group string) Counter

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

func SystemRule(rl *ratelimit.RateLimit, r ratelimit.Rule, subject string) Counter

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.

func (Counter) ForLane

func (c Counter) ForLane(lane string) Counter

ForLane is c counted once per lane account rather than once for the bound account: a link with shared off.

type Kind

type Kind uint8

Kind tells a rule counter from a budget counter.

const (
	KindRule Kind = iota + 1
	KindBudget
)

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

type Reading struct {
	Used int64
	// Amount is the cap in the same unit.
	Amount int64
}

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.

func (Reading) Remaining

func (r Reading) Remaining() int64

Remaining is what is left under the cap, never below zero.

type Refused

type Refused struct {
	Counter    *Counter
	RetryAfter time.Duration
}

Refused is the error Admit returns when a counter has no room.

func (*Refused) Error

func (r *Refused) Error() string

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

func (Rung) Upstream

func (r Rung) Upstream() bool

Upstream reports whether the rung is on the provider side of the ladder, where counters are shared by every caller that reaches the rung object.

type Usage

type Usage struct {
	Tokens    usage.Tokens
	CostNanos int64 // USD nanos
}

Usage is what a request spends: tokens by kind and the priced cost. An estimate at admission, the real figures at settlement.

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.

Jump to

Keyboard shortcuts

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