Struct WorkBudget
pub struct WorkBudget { /* private fields */ }Expand description
Shared allowance consumed synchronously in the caller’s deterministic order.
Copies are independent snapshots, not handles to a shared quota. Share one mutable budget among systems to enforce a common limit.
Implementations§
§impl WorkBudget
impl WorkBudget
pub fn new(limit: u64) -> WorkBudget
pub fn new(limit: u64) -> WorkBudget
Start a fresh budget period. Zero and u64::MAX are valid limits.
pub fn try_consume(&mut self, units: u64) -> bool
pub fn try_consume(&mut self, units: u64) -> bool
Charge all units or return false without spending any allowance.
Failed requests contribute to the denial diagnostics; zero always succeeds.
pub fn try_reserve(
&mut self,
items: u64,
cost_per_item: u64,
) -> Result<u64, WorkBudgetError>
pub fn try_reserve( &mut self, items: u64, cost_per_item: u64, ) -> Result<u64, WorkBudgetError>
Prepay all items * cost_per_item units and return the charged cost.
Check multiplication before availability. Either zero operand requests
zero units and succeeds even when exhausted. Errors leave allowance
unchanged but record a denied request. No partial reservation is made.
Use Self::refund to return unused units; there is no automatic refund.
use ferravex_engine::work_budget::{WorkBudget, WorkBudgetError};
let mut budget = WorkBudget::new(7);
assert_eq!(budget.try_reserve(2, 3), Ok(6));
assert_eq!(budget.try_reserve(1, 2), Err(WorkBudgetError::Exhausted {
requested: 2, remaining: 1,
}));
assert_eq!(budget.try_reserve(u64::MAX, 2), Err(WorkBudgetError::CostOverflow {
items: u64::MAX, cost_per_item: 2,
}));
assert_eq!(budget.remaining(), 1);pub fn refund(&mut self, units: u64) -> Result<(), WorkBudgetError>
pub fn refund(&mut self, units: u64) -> Result<(), WorkBudgetError>
Return unused units to the current period’s pooled allowance.
Refunds cannot exceed Self::spent or increase remaining allowance above
the original limit. An invalid refund changes neither quota nor diagnostics.
Valid refunds decrease net consumption but do not erase denied requests.
The caller must return only unused units from this period: reservations
are not individually tracked, and reset/limit changes start a new period.
use ferravex_engine::work_budget::{WorkBudget, WorkBudgetError};
let mut budget = WorkBudget::new(8);
let reserved = budget.try_reserve(2, 3)?;
budget.refund(reserved)?; // Cancel both jobs before running them.
assert_eq!(budget.remaining(), 8);
assert_eq!(budget.refund(1), Err(WorkBudgetError::RefundExceedsSpent {
requested: 1, spent: 0,
}));pub fn grant_up_to(&mut self, requested: u64) -> u64
pub fn grant_up_to(&mut self, requested: u64) -> u64
Charge the largest grant no greater than requested or the allowance.
An ungranted remainder counts as one denied request and that many denied
units. A zero request succeeds without recording a denial.
pub fn diagnostics(&self) -> WorkBudgetDiagnostics
pub fn diagnostics(&self) -> WorkBudgetDiagnostics
Inspect exact net consumption and bounded denial counters without mutation.
use ferravex_engine::work_budget::WorkBudget;
let mut budget = WorkBudget::new(5);
assert_eq!(budget.grant_up_to(8), 5);
let report = budget.diagnostics();
assert_eq!(report.consumed, 5);
assert_eq!(report.denied_requests, 1);
assert_eq!(report.denied_units, 3);
assert_eq!(report.cost_overflows, 0);
assert!(!report.saturated);pub fn reset(&mut self)
pub fn reset(&mut self)
Restore the current limit and clear this period’s diagnostics.
Trait Implementations§
§impl Clone for WorkBudget
impl Clone for WorkBudget
§fn clone(&self) -> WorkBudget
fn clone(&self) -> WorkBudget
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more