Documentation
¶
Overview ¶
Package initcap is a person's optional cap on what init spends, in the unit they pay in: an amount of money, a share of their plan's usage as a ceiling, or weighted tokens. Its guard starts no session that would pass the cap: once one is refused, init starts nothing more, sessions already running finish, and the guard says what was spent in the cap's unit and how to continue. Its only I/O is the agent's own usage reading and the lines it prints for init to the writer it is given.
Index ¶
- func Again(words []string, recovering bool) string
- func Sum(rows []initrecord.Spent) spend.Spend
- type Cap
- type Guard
- func (g *Guard) Check(ctx context.Context) error
- func (g *Guard) End() string
- func (g *Guard) Finish(w io.Writer, code int, again string) int
- func (g *Guard) Name() string
- func (g *Guard) Reached(continueLine string) string
- func (g *Guard) Start(ctx context.Context, spec adapter.Spec) (adapter.Session, error)
- func (g *Guard) StartLine() string
- func (g *Guard) Stopped() bool
- func (g *Guard) Warnings() []string
- func (g *Guard) Window() int
- type Kind
- type Run
- type Spending
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Again ¶
Again is the line that continues a run the cap stopped, from the words typed after init: "physa init" and those words without --cap, or, when recovering was stopped in a bare run, "physa init --recover" and the words of the bare run --recover takes (--agent, --write, --push, --pr, --hooks and --json).
Types ¶
type Cap ¶
type Cap struct {
Kind Kind
// MicroUSD is a Money cap, in millionths of a dollar.
MicroUSD int64
// Percent is a Share cap, above 0 and at most 100.
Percent float64
// Tokens is a Tokens cap, in weighted tokens.
Tokens int64
}
Cap is what a person caps init's spend at. The zero Cap is none.
func Parse ¶
Parse reads a cap as a person gives it: "" is none; "$5" or "5usd" (any case, to the cent) is money; "80%" (above 0, at most 100) is a share of the plan's usage; anything else is weighted tokens, read as an appetite is (200k, 1.5m, 50000). Every amount is above zero.
func Read ¶
Read is Parse for init's line: a cap with a phase that starts no session (starts false, as --place) is refused too.
type Guard ¶
type Guard struct {
// Cap is what init's spend is capped at.
Cap Cap
// Agent is what sessions start on: the metered agent.
Agent adapter.Adapter
// Raw is the agent a share's usage is read from; nil means Agent.
Raw adapter.Adapter
// Cache prices a session's budget for a money cap; nil prices nothing.
Cache *spend.Cache
// AgentName is the agent whose default model a budget is priced at.
AgentName string
// Base is what init's record had spent before this run.
Base spend.Spend
// Spent is what this run has spent so far; nil means Agent's, when Agent
// is a *spend.Meter, else nothing.
Spent func() spend.Spend
// contains filtered or unexported fields
}
Guard is an adapter that starts a session on Agent only when the session's budget would not pass Cap. Once it refuses a start it refuses every later one, so a stopped run does not skip ahead to smaller sessions; sessions already running are never cancelled, so a run can end past its cap by what they spend over their budgets. It is safe for concurrent use.
func Start ¶
Start holds a capped run's sessions to its cap from here on and checks it before anything starts. It returns the guard, nil when the agent cannot be resolved (the phase then says why), and the code to exit with, 0 to go on: 2 when the agent cannot report the usage a share needs, saying why, or init's record cannot be read; 1 when the cap is already reached, saying how to continue.
func (*Guard) Check ¶
Check runs before anything starts. For a share it reads the plan's usage once: an agent that cannot report it gives an error wrapping adapter.ErrNoUsage that says why, and usage already at or over the cap stops the guard and wraps adapter.ErrCapped. For money and tokens, init's record already at the cap stops the guard the same way.
func (*Guard) End ¶
End is what initrun prints after "physa init: " when a capped run ended without being stopped: what init has spent, in the cap's unit, against the cap.
func (*Guard) Finish ¶
Finish says on w, after "physa init: ", how a capped run with code ended against its cap and returns its code: when the guard stopped it, Reached with again and a code of at least 1; otherwise End and code. A nil guard says nothing.
func (*Guard) Reached ¶
Reached is what initrun prints after "physa init: " when the guard stopped a run: the cap was reached, nothing more was started, sessions running finished, what init has spent in the cap's unit (or the plan's usage), and how to continue with continueLine.
func (*Guard) Start ¶
Start starts spec on Agent unless its budget would pass the cap: for money and tokens, when what init has spent, the budgets of sessions running and this one's together pass it; for a share, when the plan's usage, read now, is at or over it, or cannot be read. A refused start returns an error wrapping adapter.ErrCapped, and so does every start after it.
func (*Guard) StartLine ¶
StartLine is what initrun prints once before the first session when a cap is set, after "physa init: ": the cap, that init stops before a session that would pass it, and what init's record has spent so far in the cap's unit, or, for a share, the plan's usage as read. A nil guard, a run with no cap, says init runs to the end.
func (*Guard) Stopped ¶
Stopped reports whether the guard has refused a start; a nil guard never has.
type Run ¶
type Run struct {
// Cap is the run's cap.
Cap Cap
// Agent resolves the agent sessions start on: the metered agent.
Agent func() (adapter.Adapter, error)
// Cache prices budgets.
Cache *spend.Cache
// Record is init's record, whose spend so far is the guard's Base unless
// Fresh: a run that gathers anew clears it.
Record Spending
Fresh bool
// Again is the line that continues the run when the cap is already reached.
Again string
// Say prints the cap's StartLine; a bare run says it in its own first line.
Say bool
// Out is where Start prints, each line after "physa init: ".
Out io.Writer
}
Run is what Start needs of a capped run of init.