Skip to main content

budget_context/
snapshot.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::{BudgetId, Resource};
5
6/// Effective remaining capacity for a resource.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum Remaining {
10    /// No node in the lineage limits the resource.
11    Unlimited,
12    /// The minimum remaining capacity across limited lineage nodes.
13    Limited(u64),
14}
15
16/// A consistent read-only view of one resource at a budget node.
17#[derive(Clone, Debug, Eq, PartialEq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct ResourceSnapshot {
20    /// The resource category.
21    pub resource: Resource,
22    /// The limit configured directly on the observed node.
23    pub local_limit: Option<u64>,
24    /// Consumed usage for the complete subtree rooted at the observed node.
25    pub consumed: u64,
26    /// Reserved usage for the complete subtree rooted at the observed node.
27    pub reserved: u64,
28    /// Effective capacity after applying every ancestor limit.
29    pub effective_remaining: Remaining,
30}
31
32/// A consistent read-only view of a budget node.
33#[derive(Clone, Debug, Eq, PartialEq)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35pub struct BudgetSnapshot {
36    /// Process-local node identifier.
37    pub id: BudgetId,
38    /// Optional diagnostic name.
39    pub name: Option<Arc<str>>,
40    /// Resources limited in the lineage or observed in this subtree.
41    pub resources: Vec<ResourceSnapshot>,
42    /// Saturating duration until the effective deadline.
43    pub deadline_remaining: Option<Duration>,
44    /// Whether cancellation has been requested for this node.
45    #[cfg(feature = "tokio")]
46    pub cancelled: bool,
47}