safety

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 13 Imported by: 0

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

View Source
const ChunkSeconds = 12

ChunkSeconds is the length of one enterprise chunk: the enterprise endpoint bills one request per 12 seconds of audio.

View Source
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 Chunks

func Chunks(d time.Duration) int

Chunks is the number of 12-second chunks in d (at least 1).

func Dollars

func Dollars(v float64) string

Dollars shows cents, or tenths of a cent when that is all there is.

func FillAllowance

func FillAllowance(ctx context.Context, a *app.App, p *Plan)

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

func RequireLimit(enterprise bool, limitFlag string) (*int, error)

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

func RequireMaxFiles(batch bool, flag string) (*int, error)

RequireMaxFiles reads --max-files for batch runs: a positive number or "none". Missing → limit_required (exit 6). Single inputs return nil.

func ScannedChunks

func ScannedChunks(n int, skip, every *int) int

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

func BudgetFor(a *app.App) *Budget

BudgetFor returns the budget of the run's --max-requests or max_requests setting, whose messages name the one in use.

func NewBudget

func NewBudget(max int) *Budget

NewBudget returns a budget of max requests; 0 means no ceiling.

func (*Budget) Refund

func (b *Budget) Refund(n int)

Refund returns n reserved requests that were not spent (for example a cache hit discovered after reserving).

func (*Budget) Remaining

func (b *Budget) Remaining() int

Remaining is how many requests are left, or -1 without a ceiling.

func (*Budget) Spent

func (b *Budget) Spent() int

Spent is the number of requests taken so far.

func (*Budget) Take

func (b *Budget) Take(n int) error

Take reserves n requests, or returns max_requests_reached (exit 6) when that would go over the ceiling (and reserves nothing).

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

func (p *Plan) Cap(max int, name string)

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

func (p Plan) MarshalJSON() ([]byte, error)

MarshalJSON writes requests and cost_usd as null, with "unbounded": true, when the plan is unbounded.

func (Plan) Render

func (p Plan) Render(out *output.Printer)

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)."

func (Plan) String

func (p Plan) String() string

String is the one-line plan summary (see PlanText).

func (Plan) Unbounded

func (p Plan) Unbounded() bool

Unbounded reports whether nothing caps what the plan can use.

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.

func (PlanText) Capped

func (p PlanText) Capped() bool

Capped reports whether the ceiling stops the run before the plan is done.

func (PlanText) String

func (p PlanText) String() string

String is the plan as one sentence without the "Plan: " lead or the final period: "142 files, 142 requests ($0.71 at the pay-as-you-go price of $5 per 1,000 requests); 30 already cached (free)". "≈" marks an approximate plan.

Jump to

Keyboard shortcuts

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