pub struct Budget<const N: usize> { /* private fields */ }Expand description
A budget of u32 units divided among N claimant slots.
Declared (as a pub static) by supervisor_graph!
for each divisible resource name, sized to the nodes and pool members that
declare it: one slot each, numbered in declaration order. The protocol:
- Something
provides the capacity —mainfor a fixed budget, or an allocator node that also names the slot inprovides:so the budget empties when it stops. A holder whose budget is still unprovided at its gate deadline faults withFaultKind::ResourceMissing, like any other slot. - Each holder’s shell receives a
Claimantbound to its slot and states what itwants. The allocator divides the capacity over the wants with aBudgetPolicy(rebalance), and each holder reads itsgrant, or parks onwait_grant_changeuntil it moves. - When a holder stops — cleanly, or by missing its shutdown ack — the
supervisor
releases its slot, so a dead session never strands its share. A parked (Pause) holder keeps its claim.
The budget never chooses a division itself: what “fair” means (equal,
proportional, ramped) is the policy’s, and when to re-divide is the
allocator’s — usually on wait_change, which fires on
every want, release and capacity change. Costs 4 + 8N bytes of atomics,
two Signals and N single-waker slots per budget: 28 + 16N bytes on a
32-bit target.
Implementations§
Source§impl<const N: usize> Budget<N>
impl<const N: usize> Budget<N>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
An unprovided budget (const — it lives in a static the macro emits).
Sourcepub fn provide(&self, capacity: u32)
pub fn provide(&self, capacity: u32)
Set the capacity and wake both the supervisor’s gate wait and the allocator.
Sourcepub const fn slots(&self) -> usize
pub const fn slots(&self) -> usize
The number of claimant slots: what the graph sized the budget to.
Sourcepub fn release(&self, slot: u8)
pub fn release(&self, slot: u8)
Drop slot slot’s demand and grant, and wake the allocator: what the
supervisor does when the slot’s holder stops.
Sourcepub fn total_granted(&self) -> u32
pub fn total_granted(&self) -> u32
The sum of every slot’s grant.
Sourcepub fn rebalance(
&self,
policy: &impl BudgetPolicy,
now: Instant,
) -> Option<Instant>
pub fn rebalance( &self, policy: &impl BudgetPolicy, now: Instant, ) -> Option<Instant>
Re-divide the capacity over the current wants with policy, publish
the new grants, and wake every holder whose grant moved.
Sourcepub async fn wait_change(&self)
pub async fn wait_change(&self)
The allocator’s wait: resolves after any want, release or capacity change since the last wait (latching, single waiter).
Trait Implementations§
Source§impl<const N: usize> ResourceGate for Budget<N>
impl<const N: usize> ResourceGate for Budget<N>
Source§fn clear(&self)
fn clear(&self)
Empty the budget: no capacity, no grants. Holders are woken so a loop parked on its grant sees the zero.
Source§fn filled_signal(&self) -> &Signal<CriticalSectionRawMutex, ()>
fn filled_signal(&self) -> &Signal<CriticalSectionRawMutex, ()>
Signal fired by provide/restore, for the supervisor’s
bounded pre-spawn wait (see Supervisor::start).