Module work_budget
Expand description
See work_budget for the system contract and examples.
Deterministic shared work quotas for AI, streaming and incremental rebuilds.
Units are host-defined work items, not inferred CPU time. Requests never partially
consume unless the caller explicitly uses WorkBudget::grant_up_to.
A reservation prepays a checked items * cost_per_item in call order. Refund
unused units explicitly; the budget does not queue requests or track individual
reservations. Diagnostics cover the period since construction, reset or a limit
change. All operations use constant space and allocate no storage.
use ferravex_engine::work_budget::{WorkBudget, WorkBudgetError};
let mut budget = WorkBudget::new(10);
let reserved = budget.try_reserve(3, 2)?; // Three jobs, two units each.
assert_eq!(reserved, 6);
budget.refund(2)?; // One job did not need to run.
assert!(budget.try_consume(5)); // Later systems see the returned allowance.
assert!(!budget.try_consume(2));
let report = budget.diagnostics();
assert_eq!((report.limit, report.remaining, report.consumed), (10, 1, 9));
assert_eq!((report.denied_requests, report.denied_units), (1, 2));
budget.reset();
assert_eq!(budget.remaining(), budget.limit());
assert_eq!(budget.diagnostics().denied_requests, 0);Structs§
- Work
Budget - Shared allowance consumed synchronously in the caller’s deterministic order.
- Work
Budget Diagnostics - Snapshot of the current budget period, including legacy consumption methods.
Enums§
- Work
Budget Error - A checked reservation or refund could not be applied.