budget

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package budget provides role/tenant scoped execution budgets.

Index

Constants

This section is empty.

Variables

View Source
var ErrExceeded = errors.New("resource budget exceeded")

ErrExceeded is returned when a configured resource budget is exceeded.

Functions

This section is empty.

Types

type Lease

type Lease interface {
	Context() context.Context
	Limits() Limits
	Complete(Usage) error
}

Lease owns one concurrency slot.

type Limits

type Limits struct {
	MaxConcurrent           int
	MaxExecution            time.Duration
	MaxEstimatedScannedRows int64
	MaxReturnedRows         int64
	MaxReturnedBytes        int64
	MaxSessionCost          int64
}

Limits is the effective budget for a scope. Zero values are unlimited.

type Manager

type Manager interface {
	AcquireWithReservation(context.Context, Scope, Reservation) (Lease, error)
}

Manager acquires scoped budgets, atomically holding a pre-execution reservation until the lease completes.

type MemoryManager

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

MemoryManager is a process-local manager with role and tenant overrides. Budgets are layered: a session's state is its principal's (Role) in one tenant; a tenant's maxConcurrent bounds the calls of all its sessions together, and its other limits tighten its sessions' field by field (see limits).

func New

func New(roles, tenants map[string]Limits) *MemoryManager

New returns a manager. Empty maps mean no additional rejection.

func NewWithBounds

func NewWithBounds(roles, tenants map[string]Limits, ttl time.Duration, maxStates int) *MemoryManager

NewWithBounds returns a manager with bounded idle scope state.

func (*MemoryManager) Acquire

func (m *MemoryManager) Acquire(ctx context.Context, scope Scope) (Lease, error)

Acquire acquires a lease without a reservation.

func (*MemoryManager) AcquireWithReservation

func (m *MemoryManager) AcquireWithReservation(
	ctx context.Context,
	scope Scope,
	reservation Reservation,
) (Lease, error)

AcquireWithReservation atomically checks and holds estimated session cost until Complete reconciles it with actual usage.

func (*MemoryManager) CloseSession

func (m *MemoryManager) CloseSession(session string)

CloseSession discards idle accounting for session and marks active scopes for removal when their last lease completes.

func (*MemoryManager) ConfiguredLimits

func (m *MemoryManager) ConfiguredLimits() (map[string]Limits, map[string]Limits)

ConfiguredLimits returns copies of the current role and tenant limits.

func (*MemoryManager) UpdateLimits

func (m *MemoryManager) UpdateLimits(roles, tenants map[string]Limits)

UpdateLimits atomically replaces configured role and tenant limits while retaining all in-flight and accumulated session state.

type Reservation

type Reservation struct {
	Cost                 int64
	EstimatedScannedRows int64
}

Reservation is a pre-execution estimate held against a session until its lease is completed and reconciled with actual Usage.

type Scope

type Scope struct {
	Role    string
	Tenant  string
	Session string
}

Scope identifies a caller budget. Tenant may be empty.

type Usage

type Usage struct {
	// EstimatedScannedRows is a planner estimate, not an observed scan count.
	EstimatedScannedRows int64
	ReturnedRows         int64
	ReturnedBytes        int64
	Duration             time.Duration
	Cost                 int64
}

Usage captures one invocation's observable resource usage.

Jump to

Keyboard shortcuts

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