budget

package
v0.10.0 Latest Latest
Warning

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

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

Documentation

Overview

Package budget provides operation budgets with stop/pause/degrade behaviors.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExhausted = errors.New("budget: exhausted")
	ErrPaused    = errors.New("budget: paused")
	ErrClosed    = errors.New("budget: closed")
)

Functions

This section is empty.

Types

type Behavior

type Behavior string

Behavior selects what happens when a budget limit is hit.

const (
	BehaviorStop    Behavior = "stop"
	BehaviorPause   Behavior = "pause"
	BehaviorDegrade Behavior = "degrade"
)

type Budget

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

Budget tracks usage against configured limits.

func New

func New(cfg Config) *Budget

New creates a budget. Zero limits mean unlimited for that dimension.

func (*Budget) AddBytes

func (b *Budget) AddBytes(n int64) error

AddBytes records transferred/processed bytes.

func (*Budget) AddRetry

func (b *Budget) AddRetry() error

AddRetry records a retry attempt.

func (*Budget) Allow

func (b *Budget) Allow() error

Allow checks whether work may proceed. Returns ErrPaused/ErrExhausted when blocked.

func (*Budget) Close

func (b *Budget) Close()

Close prevents further use.

func (*Budget) IsDegraded

func (b *Budget) IsDegraded() bool

IsDegraded reports degrade behavior is active.

func (*Budget) IsPaused

func (b *Budget) IsPaused() bool

IsPaused reports pause state.

func (*Budget) Resume

func (b *Budget) Resume()

Resume clears pause state (does not reset counters).

func (*Budget) Snapshot

func (b *Budget) Snapshot() State

Snapshot returns sanitized usage (no secrets).

type Config

type Config struct {
	Name        string
	MaxBytes    int64
	MaxDuration time.Duration
	MaxRetries  int
	OnExhausted Behavior
}

Config defines budget limits.

type State

type State struct {
	Name        string   `json:"name"`
	Bytes       int64    `json:"bytes"`
	MaxBytes    int64    `json:"max_bytes,omitempty"`
	Retries     int      `json:"retries"`
	MaxRetries  int      `json:"max_retries,omitempty"`
	Elapsed     string   `json:"elapsed,omitempty"`
	MaxDuration string   `json:"max_duration,omitempty"`
	Paused      bool     `json:"paused"`
	Degraded    bool     `json:"degraded"`
	Exhausted   bool     `json:"exhausted"`
	Behavior    Behavior `json:"behavior"`
}

State is a sanitized snapshot of budget usage.

Jump to

Keyboard shortcuts

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