Documentation
¶
Overview ¶
Package budget holds each gateway key to its own limit (#585): what a key has used in its window is summed from usage.jsonl, the records the Usage page shows, so it survives a restart; a request is let in only while the key has something left after what its requests in flight are expected to use, and those are settled with what the call recorded.
The rules:
- A call counts in the window it started in (its record's time). Windows are calendar ones in local time, or cycles of N days from when the limit was set (access.Limit.Window). A reset (#1509) starts the window again from then: what came before it doesn't count, tokens and cost alike.
- Tokens are a call's uncached input, output and cache writes, and its cache reads when the limit says so; cost is an estimate at the Usage page's prices, and a call with no known price adds none.
- A request refused before any upstream (no model, a refusal like this one) uses nothing; a failed call counts the tokens it recorded.
- A streamed reply is settled when it ends, with the usage its vendor reported; one cut off before that counts what was recorded.
- While a request is in flight it holds a reservation: its body's size in tokens (4 bytes a token) plus the key's mean output per call this window. A request is let in while used + reserved is under the cap, so parallel requests overshoot by about one call.
- A key can have several limits, each in its own window (#2227: 5 hours and a week, as a subscription's): a request is let in only while every one has something left, and its reservation is held in each window.
- Only requests that came with a gateway key are limited: from another computer one is required; on this one a request without one (or with a token that is no key) isn't limited.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Limiter ¶ added in v0.1.1255
type Limiter struct{}
Limiter is Reserve as the gateway's budget point asks it (gwbudget.Limiter); plugins/budget serves it.
type Refusal ¶
Refusal is why a key's request was turned away.
func Reserve ¶
func Reserve(who access.Identity, body int64, model string, now time.Time) (release func(), refused *Refusal)
Reserve lets a request of who's in, holding a reservation for it until the release it returns is called, or refuses it. body is the request's size in bytes (-1 unknown) and model the model it named, for a cost cap before the key has a price of its own this window.
func (*Refusal) Error ¶
Error says it as a client is told: the key, what it used of which cap, and when it resets.
type Status ¶
type Status struct {
Period string `json:"period"`
// Days is a "days" period's cycle, Hours an "hours" one's
Days int `json:"days,omitempty"`
Hours int `json:"hours,omitempty"`
Start time.Time `json:"start"`
Reset time.Time `json:"reset"`
Calls int `json:"calls"`
Tokens int64 `json:"tokens"`
Cost float64 `json:"cost"`
Unpriced int `json:"unpriced,omitempty"`
// InFlight requests, and the tokens and cost they are reserved at
InFlight int `json:"inFlight,omitempty"`
Reserved int64 `json:"reserved,omitempty"`
ReservedCost float64 `json:"reservedCost,omitempty"`
TokenLimit int64 `json:"tokenLimit,omitempty"`
CostLimit float64 `json:"costLimit,omitempty"`
CacheReads bool `json:"cacheReads,omitempty"`
// TokensLeft and CostLeft are what is left of each cap, 0 at most
TokensLeft int64 `json:"tokensLeft"`
CostLeft float64 `json:"costLeft"`
// Spent: the key is refused until Reset
Spent bool `json:"spent"`
}
Status is a key's limit and what it has used of it.
func All ¶ added in v0.1.1242
All is key k's status now of each of its limits (#2227), in the order k keeps them; none for a key with no limit. A window two limits share is read once.