quota

package
v0.0.44 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 9 Imported by: ? 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Name  string
	Key   string
	Value any
	// contains filtered or unexported fields
}

Account is the public view of one routed account.

func (*Account) InFlight

func (a *Account) InFlight() int

InFlight reports the current in-flight count.

type AccountConfig

type AccountConfig struct {
	Name        string
	Key         string
	Disabled    bool
	MaxInFlight int
	Selected    bool
	Value       any
}

AccountConfig is one credential a pool may route to. Value is opaque account data owned by the provider adapter (a client, a token file descriptor, ...).

type AccountStat

type AccountStat struct {
	Name        string
	Key         string
	Available   bool
	Reason      string
	Enabled     bool
	MaxInFlight int
	Selected    bool
	InFlight    int
	Sessions    int
	Windows     []agentquota.Window
	Balance     *agentquota.Balance
	RefreshedAt time.Time
	LastError   string
}

AccountStat is one row of the pool's view.

type Candidate

type Candidate struct {
	Name     string
	Snapshot agentquota.Snapshot
	InFlight int
	Order    int
}

Candidate is the state a policy compares. InFlight and Order are supplied by the pool; the policy never reads account storage.

type CommandCodePolicy

type CommandCodePolicy struct {
	// LowBalance is the balance at or below which an "insufficient credits"
	// refusal parks the account. A healthy account refused for one oversized
	// request keeps serving smaller ones.
	LowBalance float64
}

CommandCodePolicy is the routing strategy for Command Code accounts: weekly is the balance that decides long-term spread, 5h breaks ties, and a refusal only parks the account when its credit balance is low (or unknown).

func NewCommandCodePolicy

func NewCommandCodePolicy() *CommandCodePolicy

NewCommandCodePolicy returns the Command Code strategy.

func (*CommandCodePolicy) ClassifyRefusal

func (p *CommandCodePolicy) ClassifyRefusal(status int, body []byte, snapshot agentquota.Snapshot) Refusal

ClassifyRefusal implements Policy.

func (*CommandCodePolicy) Compare

func (p *CommandCodePolicy) Compare(a, b Candidate) int

Compare implements Policy.

func (*CommandCodePolicy) Exhausted

func (p *CommandCodePolicy) Exhausted(snapshot agentquota.Snapshot) (bool, string)

Exhausted implements Policy.

func (*CommandCodePolicy) ShouldUnpark

func (p *CommandCodePolicy) ShouldUnpark(parked, fresh agentquota.Snapshot) bool

ShouldUnpark implements Policy: only a balance that grew is evidence of a refill. A window moving is not enough for a zero-credit account.

type OpenCodeGoPolicy

type OpenCodeGoPolicy struct{}

OpenCodeGoPolicy is the routing strategy for OpenCode Go accounts: monthly is the long-term allowance that decides spread, weekly and 5h break ties, and an allowance refusal always parks the account.

func NewOpenCodeGoPolicy

func NewOpenCodeGoPolicy() *OpenCodeGoPolicy

NewOpenCodeGoPolicy returns the OpenCode Go strategy.

func (*OpenCodeGoPolicy) ClassifyRefusal

func (p *OpenCodeGoPolicy) ClassifyRefusal(status int, body []byte, _ agentquota.Snapshot) Refusal

ClassifyRefusal implements Policy.

func (*OpenCodeGoPolicy) Compare

func (p *OpenCodeGoPolicy) Compare(a, b Candidate) int

Compare implements Policy.

func (*OpenCodeGoPolicy) Exhausted

func (p *OpenCodeGoPolicy) Exhausted(snapshot agentquota.Snapshot) (bool, string)

Exhausted implements Policy.

func (*OpenCodeGoPolicy) ShouldUnpark

func (p *OpenCodeGoPolicy) ShouldUnpark(parked, fresh agentquota.Snapshot) bool

ShouldUnpark implements Policy: any window that was at its cap must have fallen below it.

type Options

type Options struct {
	Source         agentquota.Source
	Policy         Policy
	Settings       Settings
	Now            func() time.Time
	DisableRefresh bool
	WaitForSlot    time.Duration
}

Options configures a pool. The source only reads; the policy only decides.

type Policy

type Policy interface {
	// Compare orders two candidates. Negative means a is preferred.
	Compare(a, b Candidate) int
	// Exhausted reports whether the snapshot itself means the account cannot
	// serve, with the reason to show.
	Exhausted(snapshot agentquota.Snapshot) (bool, string)
	// ShouldUnpark reports whether a parked account may serve again.
	ShouldUnpark(parked, fresh agentquota.Snapshot) bool
	// ClassifyRefusal maps an upstream answer to a routing decision. It sees the
	// account's current snapshot so a low-credit rule stays in the policy.
	ClassifyRefusal(status int, body []byte, snapshot agentquota.Snapshot) Refusal
}

Policy is the provider-specific routing strategy. A pool is constructed with exactly one Policy; the pool never branches on the source id.

type Pool

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

Pool owns one provider's accounts, quotas, in-flight counts and session bindings. It never sees another provider's pool.

func NewPool

func NewPool(configs []AccountConfig, opts Options) (*Pool, error)

NewPool builds one provider's pool and starts its refresher unless disabled.

func (*Pool) AccountParked

func (p *Pool) AccountParked(name string) bool

AccountParked reports whether one account is parked for quota right now.

func (*Pool) AccountSnapshot

func (p *Pool) AccountSnapshot(name string) (agentquota.Snapshot, bool)

AccountSnapshot returns one account's current quota.

func (*Pool) Acquire

func (p *Pool) Acquire(name string) bool

Acquire reserves one in-flight slot on the named account.

func (*Pool) AcquireSlot

func (p *Pool) AcquireSlot(ctx context.Context, preferred string, exclude map[string]bool) *Account

AcquireSlot picks an account and reserves a slot, waiting up to the pool's WaitForSlot for a busy pool to drain.

func (*Pool) Bind

func (p *Pool) Bind(sessionID, accountName string)

Bind records that a conversation is served by an account.

func (*Pool) BlockRefusal

func (p *Pool) BlockRefusal(name string, refusal Refusal) bool

BlockRefusal reacts to one classified refusal. Park parks the account; otherwise the account only cools down. This is the whole rule: no balance shape is read here.

func (*Pool) ClassifyRefusal

func (p *Pool) ClassifyRefusal(name string, status int, body []byte) Refusal

ClassifyRefusal lets the pool's policy classify one upstream answer against the account's current snapshot.

func (*Pool) Close

func (p *Pool) Close()

Close stops the refresher.

func (*Pool) Configure

func (p *Pool) Configure(configs []AccountConfig, settings Settings)

Configure reconciles the pool with its provider's current credentials.

func (*Pool) Cooldown

func (p *Pool) Cooldown(name, reason string, d time.Duration)

Cooldown takes an account out of rotation for a while.

func (*Pool) HasCandidate

func (p *Pool) HasCandidate(preferred string, exclude map[string]bool) bool

HasCandidate reports whether any account could take a request.

func (*Pool) Len

func (p *Pool) Len() int

Len reports the number of accounts.

func (*Pool) LiveWindow

func (p *Pool) LiveWindow() time.Duration

LiveWindow returns the pool's live-count window.

func (*Pool) LoadBalance

func (p *Pool) LoadBalance() bool

LoadBalance reports whether the pool routes across accounts.

func (*Pool) Names

func (p *Pool) Names() []string

Names lists the configured accounts in order.

func (*Pool) OutOfQuota

func (p *Pool) OutOfQuota() bool

OutOfQuota reports whether every routable account is parked.

func (*Pool) Preferred

func (p *Pool) Preferred(sessionID string) string

Preferred returns the account a conversation is bound to, while the binding is still within SessionTTL.

func (*Pool) Refresh

func (p *Pool) Refresh(ctx context.Context)

Refresh polls every account once. A failure keeps the previous availability; an auth failure marks the account unavailable until a later success.

func (*Pool) RefreshAccount

func (p *Pool) RefreshAccount(ctx context.Context, name string)

RefreshAccount polls one account. Concurrent refusals for the same account share one fetch.

func (*Pool) Release

func (p *Pool) Release(name string)

Release returns an in-flight slot.

func (*Pool) RouteName

func (p *Pool) RouteName(preferred string, exclude map[string]bool) (string, bool)

RouteName reports which account would serve a request.

func (*Pool) Select

func (p *Pool) Select(preferred string, exclude map[string]bool) *Account

Select returns the account to serve one request.

func (*Pool) SessionTTL

func (p *Pool) SessionTTL() time.Duration

SessionTTL returns the pool's stickiness window.

func (*Pool) Snapshot

func (p *Pool) Snapshot() []AccountStat

Snapshot copies the current state for the stats endpoints.

type Refusal

type Refusal struct {
	Kind    RefusalKind
	Message string
	Detail  string
	Park    bool
}

Refusal is a classified upstream answer. Park is the policy's decision that the account itself, not this request, is the problem. The pool only reads Park: true parks, false cools down.

type RefusalKind

type RefusalKind string

RefusalKind is the routing meaning of one upstream answer.

const (
	// RefusalRequest is a request-level refusal: another account would answer
	// the same, so the request is passed through.
	RefusalRequest RefusalKind = "request"
	// RefusalAllowance means the account has no allowance left.
	RefusalAllowance RefusalKind = "allowance"
	// RefusalAuth means the credential itself was rejected.
	RefusalAuth RefusalKind = "auth"
	// RefusalRateLimit is a short-window rate limit.
	RefusalRateLimit RefusalKind = "rate_limit"
	// RefusalTransient is a 5xx or another retryable upstream failure.
	RefusalTransient RefusalKind = "transient"
)

type Settings

type Settings struct {
	LoadBalance  bool
	Selected     string
	RefreshEvery time.Duration
	SessionTTL   time.Duration
	LiveWindow   time.Duration
}

Settings are the per-pool knobs. SessionTTL is how long a conversation stays bound to one account; LiveWindow is how recently a conversation must have been seen to count as live. They are deliberately separate.

Jump to

Keyboard shortcuts

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