Documentation
¶
Overview ¶
Package safety keeps billable work bounded: it requires explicit limits, estimates what a run will cost before it starts, and enforces the --max-requests ceiling.
Index ¶
- Constants
- func Chunks(d time.Duration) int
- func Dollars(v float64) string
- func FillAllowance(ctx context.Context, a *app.App, p *Plan)
- func RequireLimit(enterprise bool, limitFlag string) (*int, error)
- func RequireMaxFiles(batch bool, flag string) (*int, error)
- func ScannedChunks(n int, skip, every *int) int
- type Budget
- type Plan
- type PlanText
Constants ¶
const ChunkSeconds = 12
ChunkSeconds is the length of one enterprise chunk: the enterprise endpoint bills one request per 12 seconds of audio.
const PricePerRequestUSD = 0.005
PricePerRequestUSD is the pay-as-you-go price used for estimates ($5 per 1,000 requests).
Variables ¶
This section is empty.
Functions ¶
func FillAllowance ¶
FillAllowance adds the account's remaining requests to the plan when the person is logged in. It never fails: without a login it does nothing.
func RequireLimit ¶
RequireLimit reads --limit for enterprise runs: a positive number of chunks per file, or "none". Missing → limit_required (exit 6). For standard runs the flag is not used and nil is returned.
func RequireMaxFiles ¶
RequireMaxFiles reads --max-files for batch runs: a positive number or "none". Missing → limit_required (exit 6). Single inputs return nil.
func ScannedChunks ¶
ScannedChunks applies the enterprise skip/every parameters to n chunks: scan `every` chunks in a row, then skip `skip` chunks.
Types ¶
type Budget ¶
type Budget struct {
// contains filtered or unexported fields
}
Budget enforces --max-requests across a run. It is safe for concurrent use.
func BudgetFor ¶
BudgetFor returns the budget of the run's --max-requests or max_requests setting, whose messages name the one in use.
func (*Budget) Refund ¶
Refund returns n reserved requests that were not spent (for example a cache hit discovered after reserving).
type Plan ¶
type Plan struct {
Files int `json:"files"`
CachedFiles int `json:"cached_files"`
Requests int `json:"requests"`
Approximate bool `json:"approximate"`
CostUSD float64 `json:"cost_usd"`
RemainingAllowance *int `json:"remaining_allowance,omitempty"`
UnknownLengthFiles int `json:"unknown_length_files,omitempty"`
// SizeEstimated is set when a local file's length was guessed from its
// size (ffprobe would give the exact length).
SizeEstimated bool `json:"-"`
// Enterprise is set for a plan on the enterprise endpoint.
Enterprise bool `json:"-"`
// MaxRequests is the --max-requests ceiling when it stops the run
// before the plan is done (see Cap).
MaxRequests int `json:"-"`
MaxRequestsName string `json:"-"`
}
Plan is what a run is expected to spend.
UnknownLengthFiles counts enterprise inputs of unknown length (URLs) sent without --limit: nothing caps what they can use, so the plan's request count and cost are unknown (null in JSON, with "unbounded": true).
func Estimate ¶
func Estimate(files []media.Input, enterprise bool, limit *int, skip, every *int, c *cache.Cache, durations func(path string) (time.Duration, bool), params ...map[string]string) Plan
Estimate counts files and requests. Cached files cost nothing. Standard recognition is one request per file. Enterprise is one request per 12 s chunk (after skip/every), capped by limit; durations come from durations (ffprobe) when it knows the file, else from the file size, which makes the plan approximate. params are the request parameters used in cache keys (see cache.KeyForInput); leave them out when there are none.
func (*Plan) Cap ¶
Cap applies a --max-requests ceiling (max > 0) named name: when it stops the run before the plan is done, the plan says so.
func (Plan) MarshalJSON ¶
MarshalJSON writes requests and cost_usd as null, with "unbounded": true, when the plan is unbounded.
func (Plan) Render ¶
Render prints the plan on stderr, for example "Plan: 142 files, ≈ 142 requests (≈ $0.71 at the pay-as-you-go price of $5 per 1,000 requests); 30 already cached (free)."
type PlanText ¶
type PlanText struct {
Files, CachedFiles, Requests int
CostUSD float64
Approximate bool
// SizeEstimated: some lengths were guessed from file sizes.
SizeEstimated bool
UnknownLengthFiles int
TooLargeFiles int
Enterprise bool
RemainingAllowance *int
// MaxRequests is the --max-requests ceiling (0: none), and
// MaxRequestsName how to name it ("--max-requests 5").
MaxRequests int
MaxRequestsName string
}
PlanText is what a plan says, for one input and for a batch alike.