pub struct Budget {
pub deadline: Option<Time>,
pub poll_quota: u32,
pub cost_quota: Option<u64>,
pub priority: u8,
}Expand description
A budget constraining resource usage for a task or region.
Budgets form a product semiring for combination:
- Deadlines/quotas use min (tighter wins)
- Priority uses max (higher urgency wins)
Fields§
§deadline: Option<Time>Absolute deadline by which work must complete.
poll_quota: u32Maximum number of poll operations.
cost_quota: Option<u64>Abstract cost quota (for advanced scheduling).
priority: u8Scheduling priority (0 = lowest, 255 = highest).
Implementations§
Source§impl Budget
impl Budget
Sourcepub const MINIMAL: Budget
pub const MINIMAL: Budget
A minimal budget for cleanup operations.
This provides a small poll quota (100 polls) for cleanup and finalizer code to run, but no deadline or cost constraints. Used when requesting cancellation to allow tasks a bounded cleanup phase.
Sourcepub const fn new() -> Budget
pub const fn new() -> Budget
Creates a new budget with default values (priority 128, unlimited quotas).
Sourcepub const fn with_deadline_secs(secs: u64) -> Budget
pub const fn with_deadline_secs(secs: u64) -> Budget
Creates a budget with only an absolute deadline constraint (in seconds).
The value is a logical instant since the runtime epoch, not a timeout
duration. For a per-operation timeout, use
with_timeout with the current logical time.
§Example
let budget = Budget::with_deadline_secs(30);
assert_eq!(budget.deadline, Some(Time::from_secs(30)));Sourcepub const fn with_deadline_ns(nanos: u64) -> Budget
pub const fn with_deadline_ns(nanos: u64) -> Budget
Creates a budget with only an absolute deadline constraint (in nanoseconds).
The value is a logical instant since the runtime epoch, not a timeout
duration. For a per-operation timeout, use
with_timeout with the current logical time.
§Example
let budget = Budget::with_deadline_ns(30_000_000_000); // 30 seconds
assert_eq!(budget.deadline, Some(Time::from_nanos(30_000_000_000)));Sourcepub const fn with_deadline(self, deadline: Time) -> Budget
pub const fn with_deadline(self, deadline: Time) -> Budget
Sets the absolute logical deadline.
The deadline is an instant, not a duration. For example,
Time::from_secs(30) means the runtime/lab clock value 30s, not
“30 seconds from now”. For relative per-operation timeouts, use
with_timeout.
Sourcepub fn with_timeout(self, now: Time, timeout: Duration) -> Budget
pub fn with_timeout(self, now: Time, timeout: Duration) -> Budget
Sets the deadline to now + timeout.
Use this for request, RPC, database, and other per-operation timeout
budgets. The caller supplies now explicitly so production and lab
runtimes use the same deterministic time source.
§Example
let now = Time::from_secs(100);
let budget = Budget::new().with_timeout(now, Duration::from_secs(30));
assert_eq!(budget.deadline, Some(Time::from_secs(130)));Sourcepub const fn with_poll_quota(self, quota: u32) -> Budget
pub const fn with_poll_quota(self, quota: u32) -> Budget
Sets the poll quota.
Sourcepub const fn with_cost_quota(self, quota: u64) -> Budget
pub const fn with_cost_quota(self, quota: u64) -> Budget
Sets the cost quota.
Sourcepub const fn with_priority(self, priority: u8) -> Budget
pub const fn with_priority(self, priority: u8) -> Budget
Sets the priority.
Sourcepub const fn is_exhausted(&self) -> bool
pub const fn is_exhausted(&self) -> bool
Returns true if the budget has been exhausted.
This checks only poll and cost quotas, not deadline (which requires current time).
Sourcepub fn is_past_deadline(&self, now: Time) -> bool
pub fn is_past_deadline(&self, now: Time) -> bool
Returns true if the deadline has passed.
Sourcepub fn consume_poll(&mut self) -> Option<u32>
pub fn consume_poll(&mut self) -> Option<u32>
Decrements the poll quota by one, returning the old value.
Returns None if already at zero.
Sourcepub fn combine(self, other: Budget) -> Budget
pub fn combine(self, other: Budget) -> Budget
Combines two budgets using product semiring semantics.
- Deadlines: min (earlier wins)
- Quotas: min (tighter wins)
- Priority: max (higher urgency wins)
This is also known as the “meet” operation (∧) in lattice terminology.
See also: meet.
§Example
let outer = Budget::new()
.with_deadline(Time::from_secs(30))
.with_poll_quota(1000);
let inner = Budget::new()
.with_deadline(Time::from_secs(10)) // tighter
.with_poll_quota(5000); // looser
let combined = outer.combine(inner);
assert_eq!(combined.deadline, Some(Time::from_secs(10))); // min
assert_eq!(combined.poll_quota, 1000); // minSourcepub fn meet(self, other: Budget) -> Budget
pub fn meet(self, other: Budget) -> Budget
Meet operation (∧) - alias for combine.
Computes the tightest constraints from two budgets. This is the fundamental operation for nesting budget scopes.
§Example
let parent = Budget::with_deadline_secs(30);
let child = Budget::with_deadline_secs(10);
// Child deadline is tighter, so it wins
let effective = parent.meet(child);
assert_eq!(effective.deadline, Some(Time::from_secs(10)));Sourcepub fn consume_cost(&mut self, cost: u64) -> bool
pub fn consume_cost(&mut self, cost: u64) -> bool
Consumes cost quota, returning true if successful.
Returns false (and does not modify quota) if there isn’t enough
remaining cost quota. If no cost quota is set, always succeeds.
§Example
let mut budget = Budget::new().with_cost_quota(100);
assert!(budget.consume_cost(30)); // 70 remaining
assert!(budget.consume_cost(70)); // 0 remaining
assert!(!budget.consume_cost(1)); // fails, quota exhaustedSourcepub fn remaining_time(&self, now: Time) -> Option<Duration>
pub fn remaining_time(&self, now: Time) -> Option<Duration>
Returns the remaining time until the deadline, if any.
Returns None if there is no deadline or if the deadline has passed.
§Example
let budget = Budget::with_deadline_secs(30);
let now = Time::from_secs(10);
let remaining = budget.remaining_time(now);
assert_eq!(remaining, Some(Duration::from_secs(20)));Sourcepub const fn remaining_polls(&self) -> u32
pub const fn remaining_polls(&self) -> u32
Returns the remaining poll quota.
Returns the current poll quota value. A value of u32::MAX indicates
effectively unlimited polls.
§Example
let budget = Budget::new().with_poll_quota(100);
assert_eq!(budget.remaining_polls(), 100);Sourcepub const fn remaining_cost(&self) -> Option<u64>
pub const fn remaining_cost(&self) -> Option<u64>
Returns the remaining cost quota, if any.
Returns None if no cost quota is set (unlimited).
§Example
let budget = Budget::new().with_cost_quota(1000);
assert_eq!(budget.remaining_cost(), Some(1000));
let unlimited = Budget::unlimited();
assert_eq!(unlimited.remaining_cost(), None);Sourcepub fn to_timeout(&self, now: Time) -> Option<Duration>
pub fn to_timeout(&self, now: Time) -> Option<Duration>
Converts the deadline to a timeout duration from the given time.
Returns the same value as remaining_time.
This method is provided for API compatibility with timeout-based systems.
§Example
let budget = Budget::with_deadline_secs(30);
let now = Time::from_secs(5);
// 25 seconds remaining
let timeout = budget.to_timeout(now);
assert_eq!(timeout, Some(Duration::from_secs(25)));Trait Implementations§
Source§impl BudgetTimeExt for Budget
impl BudgetTimeExt for Budget
impl Copy for Budget
impl Eq for Budget
Source§impl LocalToDistributed for Budget
impl LocalToDistributed for Budget
Source§type Distributed = BudgetSnapshot
type Distributed = BudgetSnapshot
Source§fn to_distributed(&self) -> BudgetSnapshot
fn to_distributed(&self) -> BudgetSnapshot
impl StructuralPartialEq for Budget
Auto Trait Implementations§
impl Freeze for Budget
impl RefUnwindSafe for Budget
impl Send for Budget
impl Sync for Budget
impl Unpin for Budget
impl UnsafeUnpin for Budget
impl UnwindSafe for Budget
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more