ExAgent. UsageLimits. Budget. Postgres
(ExAgent v3.0.0)
View Source
A shared spend account on the host's PostgreSQL Ecto Repo.
Stability — Stable.
Apply version 1 of ExAgent.Migration on the Repo's search path. Construct
a handle per tenant with the same initial ceiling in cents and window length
across replicas. The ceiling rounds down to a whole microcent.
The first reservation binds the policy for that account. Use
update_limits/2 to change the ceiling. A handle with a different policy
receives :spend_budget_policy_mismatch during admission. PostgreSQL's
clock determines fixed windows aligned to the Unix epoch.
A settled charge counts in the window in which it was reserved. Pending
holds count in every window until evidence closes them. An empty window
can therefore still refuse requests with :unknown for the wait.
A settlement that fits its hold only locks the receipt row. Reservations
and settlements that can increase the balance use the account lock.
Within each VM, Budget writes queue before checking out a connection.
A caller that already holds a checkout bypasses that queue, as do
SQL Sandbox Repos, whose ownership can outlive Budget's transaction.
Writes inside an explicit Repo transaction return an error immediately.
Budget needs its own commit to acknowledge a write.
A separate Budget Repo can also isolate ledger traffic from the C7 Store.
Use statement/2 to read receipts, pending/2 to locate holds and
ExAgent.UsageLimits.Budget.reconcile/3 to apply verified amounts.
The schema retains closed receipts for idempotence. A host retention policy
may purge old closed rows, ending that guarantee for the purged IDs.
Set the candidate :thresholds option to distinct whole percentages such as [80, 100] on
each replica. After a confirmed positive charge, a background query can
emit a threshold event for that charge's original window. It claims each
reached threshold once in PostgreSQL, so replicas cannot emit duplicates.
Delivery is best effort if the query or VM fails. Thresholds default to
an empty list.
Summary
Types
An open or closed receipt with its original window and candidate admission labels.
A pending reservation. Timestamps are Unix milliseconds.
Functions
Builds a scoped handle with a positive limit and a fixed window in milliseconds.
Lists pending reservations by ID within this account. Options include
:limit (1–1,000, default 100), :after (the last page's final ID) and
:timeout (5,000 ms by default). An empty list means this page has no
pending reservations. The host authorizes account selection.
Lists this account's open and closed receipts in ascending reservation ID
order. Pass the returned :cursor as :after for the next page. The
cursor is the last reservation ID, or nil when there are no more items. Options
include :limit (1–1,000, default 100) and :timeout (default 5,000 ms).
Each page reflects committed rows when it is read.
Update the ceiling of an existing account. The host authorizes the plan
change and passes :limit_cents and optionally :timeout (5,000 ms by
default). Returns a handle with the new policy. Use it for new admissions.
Types
@type entry() :: %{ id: String.t(), request_id: String.t(), reserved_at: non_neg_integer(), window_start: non_neg_integer(), held_cents: ExAgent.UsageLimits.Budget.cents(), charged_cents: ExAgent.UsageLimits.Budget.cents() | nil, labels: ExAgent.UsageLimits.Budget.Adapter.labels() }
An open or closed receipt with its original window and candidate admission labels.
@type pending() :: %{ id: String.t(), request_id: String.t(), held_cents: ExAgent.UsageLimits.Budget.cents(), reserved_at: non_neg_integer() }
A pending reservation. Timestamps are Unix milliseconds.
@opaque t()
Functions
Builds a scoped handle with a positive limit and a fixed window in milliseconds.
@spec pending(t(), keyword()) :: {:ok, [pending()]} | {:error, ExAgent.UsageLimits.Budget.unavailable()}
Lists pending reservations by ID within this account. Options include
:limit (1–1,000, default 100), :after (the last page's final ID) and
:timeout (5,000 ms by default). An empty list means this page has no
pending reservations. The host authorizes account selection.
@spec statement(t(), keyword()) :: {:ok, %{items: [entry()], cursor: String.t() | nil}} | {:error, ExAgent.UsageLimits.Budget.unavailable()}
Lists this account's open and closed receipts in ascending reservation ID
order. Pass the returned :cursor as :after for the next page. The
cursor is the last reservation ID, or nil when there are no more items. Options
include :limit (1–1,000, default 100) and :timeout (default 5,000 ms).
Each page reflects committed rows when it is read.
Amounts are in cents. held_cents records the latest hold, including on
a closed row. Only open rows (with charged_cents: nil) still hold that
amount against the account. The candidate :labels field is empty on
rows without attribution.
Window starts and reservation timestamps are Unix milliseconds.
The caller authorizes account selection.
@spec update_limits(t(), keyword()) :: {:ok, t()} | {:error, :budget_account_not_found | :spend_budget_policy_mismatch | :result_unconfirmed}
Update the ceiling of an existing account. The host authorizes the plan
change and passes :limit_cents and optionally :timeout (5,000 ms by
default). Returns a handle with the new policy. Use it for new admissions.
The account lock serializes this operation with reservations. Charges and open holds continue to count in the existing billing window. Older handles can settle existing holds. The update compares the old policy in the handle with the active policy. If the desired policy is already active, the update succeeds, including after a lost acknowledgement.