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

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

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>

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>

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

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 limit(&self) -> u64

Maximum allowance in the current period.

pub fn remaining(&self) -> u64

Units available to subsequent requests.

pub fn spent(&self) -> u64

Net units charged in the current period, after refunds.

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)

Restore the current limit and clear this period’s diagnostics.

pub fn set_limit(&mut self, limit: u64)

Start a fresh period with a new limit and cleared diagnostics.

Trait Implementations§

§

impl Clone for WorkBudget

§

fn clone(&self) -> WorkBudget

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for WorkBudget

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl PartialEq for WorkBudget

§

fn eq(&self, other: &WorkBudget) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Copy for WorkBudget

§

impl Eq for WorkBudget

§

impl StructuralPartialEq for WorkBudget

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.