limits

package
v0.16.521 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 modules, 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 computes the state of the limit rules of an organization and the effective limit of each person (docs/hub/limits.md): every rule that applies to a call counts it, the tightest one wins. The hub shows the result and, unless HUB_LIMITS_ENFORCE=off, sends it to the machines (push.go).

Index

Constants

View Source
const (
	OK   = hubproto.BudgetOK
	Warn = hubproto.BudgetWarn
	Stop = hubproto.BudgetStop

	// DefaultWarnPct is the share at which a rule warns unless set.
	DefaultWarnPct = 0.8
)
View Source
const DefaultTZ = "Europe/Moscow"

DefaultTZ is the time zone of an organization that has none (an implicit one).

Variables

This section is empty.

Functions

func Length

func Length(period string) time.Duration

Length is a rolling period's length.

func Location

func Location(ctx context.Context, st store.Store, orgKey string) (*time.Location, error)

Location is the time zone an organization cuts its months in.

func ModelMatches

func ModelMatches(pattern, model string) bool

ModelMatches says whether a rule's model pattern covers a call's model: the same name, a family key of the price list, or a part of the name («opus-4-1» covers claude-opus-4-1-20250805).

func Worse

func Worse(a, b string) string

Worse is the worse of two statuses.

Types

type Book

type Book struct {
	OrgKey   string
	Loc      *time.Location
	Now      time.Time
	States   []State
	Accounts map[string]store.Account
	// contains filtered or unexported fields
}

Book is the rules of one organization evaluated at one instant.

func Evaluate

func Evaluate(ctx context.Context, st store.Store, orgKey string, loc *time.Location, now time.Time) (*Book, error)

Evaluate computes every rule of the organization (orgKey: store.OrgKey) at now, months cut in loc.

func (*Book) AccountLabel

func (b *Book) AccountLabel(id string) string

AccountLabel is the name of an account of the book.

func (*Book) Applies

func (b *Book) Applies(p Person, r store.LimitRule) bool

Applies says whether a rule binds the person.

func (*Book) Effective

func (b *Book) Effective(p Person) []Effective

Effective is the person's effective limits. One entry stands for every account and model (when any rule binds the person), and one more for each account or model whose own rule is the binding one there: what the machine needs to tell a stop through «Claude Max №2» from a warn anywhere else.

func (*Book) MachineEntries

func (b *Book) MachineEntries(ctx context.Context, st store.Store, spaceID string) ([]hubproto.BudgetEntry, error)

MachineEntries is the list a machine of the space holds: every limit of its people that is not ok.

func (*Book) PersonOf

func (b *Book) PersonOf(ctx context.Context, st store.Store, userID string) (Person, error)

PersonOf is where the person is in the book's organization.

func (*Book) Rules

func (b *Book) Rules(p Person) []State

Rules is the states of the rules that bind the person.

type Caller

type Caller interface {
	DoInto(ctx context.Context, machineID, op string, params, out any, timeout time.Duration) error
	Online() []string
}

Caller sends an operation to a machine over its link (link.Registry).

type Effective

type Effective struct {
	AccountID    string
	ModelPattern string
	Status       string
	Pct          float64
	Binding      string
	RuleIDs      []string // the binding one first
	Reason       string
	ResetsAt     *time.Time
}

Effective is the limit of a person for one account ("" any) and one model ("" any): the worst rule that applies.

type Person

type Person struct {
	UserID  string
	Tenants map[string]bool
	Teams   map[string]bool
}

Person is where a person is in the organization: the tenants and the teams whose rules bind them.

type PushDeps

type PushDeps struct {
	DB   *store.DB
	Reg  Caller
	Mode string // hubproto.EnforceWarn or EnforceStop
	Log  *slog.Logger
	Now  func() time.Time
	// Every is how often every machine's list is checked; 0 means 60 s (the contract's bound).
	Every time.Duration
}

PushDeps are what the pusher is built from.

type Pusher

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

Pusher keeps the budget lists of the machines current: it computes each machine's list, and sends budget.set when the list changed or the machine holds an older version than the hub sent.

func NewPusher

func NewPusher(d PushDeps) *Pusher

NewPusher starts the pusher.

func (*Pusher) Close

func (p *Pusher) Close()

Close stops the pusher and waits for the pass running.

func (*Pusher) Connected

func (p *Pusher) Connected(machineID string, hello hubproto.Hello)

Connected is called after a machine's hello: an agent that applies budgets gets its list now if it is behind.

func (*Pusher) Kick

func (p *Pusher) Kick()

Kick asks for a pass soon (a rule changed, a machine came).

func (*Pusher) Push

func (p *Pusher) Push(ctx context.Context) error

Push checks every online machine that applies budgets and sends the lists that changed.

type State

type State struct {
	Rule      store.LimitRule
	Used      float64 // in the rule's unit: dollars, tokens or percent of the window
	Pct       float64 // Used ÷ Amount; 1 for a forbid rule
	Status    string
	ResetsAt  *time.Time
	Estimated bool
	Reason    string
}

State is a rule with what it counts now.

Jump to

Keyboard shortcuts

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