Help us shape the future of Go! Take the 2026 Go Developer Survey

quota

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package quota is the storage quota framework (ADR-0292): one decision point, Checker, that every capped storage write asks before it stores, as every PEP asks the one Cedar PDP (internal/auth). Book implements it over the Sources each store kind registers; None admits everything and is every enforcement point's default.

Index

Constants

View Source
const Unknown int64 = -1

Unknown is the Bytes of a write whose size is known only once it ran, a SQL Execute: it needs room left under the limit, and once stored on a capped subject it starts a measure.

Variables

This section is empty.

Functions

This section is empty.

Types

type Book

type Book struct {
	// contains filtered or unexported fields
}

Book is the Checker over the registered Sources. Usage is the last measure plus the bytes settled since and the bytes reserved in flight, so for a write of known size it never counts less than the store holds.

func NewBook

func NewBook(clk clock.Clock, logger *slog.Logger) *Book

NewBook returns an empty Book; nil logger is slog.Default.

func (*Book) Admit

func (b *Book) Admit(ctx context.Context, r Request) (Settle, error)

Admit implements Checker (ADR-0292 Decisions 3 and 4).

func (*Book) Refresh

func (b *Book) Refresh(ctx context.Context)

Refresh reads every subject's limit and usage again, as Run does each refreshInterval, and drops those unused for idleAfter.

func (*Book) Register

func (b *Book) Register(kind v1.Kind, src Source)

Register sets the Source of kind.

func (*Book) Run

func (b *Book) Run(ctx context.Context)

Run refreshes every subject each refreshInterval until ctx ends, and drops those unused for idleAfter.

type Checker

type Checker interface {
	// Admit reserves r.Bytes, or refuses with fault.InsufficientStorage when the write would pass the limit.
	Admit(ctx context.Context, r Request) (Settle, error)
}

Checker is the quota counterpart of auth.Authorizer: Admit before a write, the returned Settle after it.

var None Checker = none{}

None admits every write.

type Floor added in v0.34.0

type Floor struct {
	// contains filtered or unexported fields
}

Floor is the free-space floor of the data filesystem (ADR-0296): it refuses a write that the last reading, less the bytes admitted since, cannot hold above minFree. A reading that fails admits writes and is logged.

func NewFloor added in v0.34.0

func NewFloor(free FreeFunc, minFreeBytes int64, clk clock.Clock, logger *slog.Logger) *Floor

NewFloor returns the floor of minFreeBytes over free; nil logger is slog.Default.

func (*Floor) Check added in v0.34.0

func (f *Floor) Check(_ context.Context, bytes int64) error

Check admits a write of bytes, or Unknown, while the filesystem holds it above the floor; else it returns fault.InsufficientStorage. An admitted size counts against the reading until the next one.

func (*Floor) Guard added in v0.34.0

func (f *Floor) Guard(next Checker) Checker

Guard returns the Checker that asks f with a write's size before next, for every Request but a delete, at the subject without a prefix: a Bucket prefix's write is asked again at its Bucket or the zero Subject (blob.Quoted), so the floor counts each write once.

type FreeFunc added in v0.34.0

type FreeFunc func() (int64, error)

FreeFunc returns the bytes a filesystem can still allocate.

func DiskFree added in v0.34.0

func DiskFree(path string) FreeFunc

DiskFree is the FreeFunc of the filesystem holding path: statfs's available blocks times their size.

type Request

type Request struct {
	Subject Subject
	// Bytes is the write's size, or Unknown.
	Bytes int64
	// Replaces is the size the write overwrites, asked only before a refusal; nil is 0. It is credited to the
	// decision, never to the count, since a concurrent delete can make it wrong.
	Replaces func(ctx context.Context) (int64, error)
	// Deletes marks a write that only deletes: always admitted, never measured for; once stored, the subject's next
	// refusal waits on a fresh measure, so the room it made counts at once.
	Deletes bool
}

Request is one write a Checker decides.

type Settle

type Settle func(stored bool)

Settle ends an admitted write: stored turns its reservation into settled bytes, false releases it. Call it once.

type Source

type Source struct {
	Limit   func(ctx context.Context, s Subject) (limit int64, ok bool, err error)
	Measure func(ctx context.Context, s Subject) (int64, error)
}

Source reads one kind's quotas: Limit is a subject's cap (ok=false: none), Measure what it holds.

type Subject

type Subject struct {
	Kind      v1.Kind
	Namespace v1.NamespaceName
	Name      v1.ObjectName
	Prefix    string
}

Subject is what a quota caps: a store, or a prefix of a Bucket.

func (Subject) String

func (s Subject) String() string

Jump to

Keyboard shortcuts

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