pub struct QuotaTracker<Id: Eq + Hash + Clone> { /* private fields */ }Expand description
Tracks rate/quota usage for a set of nodes.
Thread-safe — all methods take &self and synchronise internally via RwLock.
§Examples
use loadwise::quota::{QuotaTracker, QuotaConfig, QuotaDimension};
use std::time::Duration;
let tracker = QuotaTracker::<String>::new();
tracker.register(&"key-1".into(), QuotaConfig::builder()
.dimensions(vec![
QuotaDimension::builder()
.name("rpm".into())
.window(Duration::from_secs(60))
.limit(100)
.build(),
])
.build());
tracker.record_usage(&"key-1".into(), &[("rpm", 1)]);
assert_eq!(tracker.remaining(&"key-1".into(), "rpm"), 99);Implementations§
Source§impl<Id: Eq + Hash + Clone> QuotaTracker<Id>
impl<Id: Eq + Hash + Clone> QuotaTracker<Id>
Sourcepub fn register(&self, id: &Id, config: QuotaConfig)
pub fn register(&self, id: &Id, config: QuotaConfig)
Register a node with its quota configuration. If already registered, replaces config.
Sourcepub fn record_usage(&self, id: &Id, amounts: &[(&str, u64)]) -> bool
pub fn record_usage(&self, id: &Id, amounts: &[(&str, u64)]) -> bool
Record usage. amounts is a slice of (dimension_name, amount) pairs.
Unknown dimensions are silently ignored.
Returns true if the node was registered (usage recorded),
false if the node ID was not found.
Sourcepub fn mark_exhausted(&self, id: &Id, duration: Duration)
pub fn mark_exhausted(&self, id: &Id, duration: Duration)
Mark a node as exhausted for duration (e.g., from a 429 Retry-After header).
Sourcepub fn has_capacity(&self, id: &Id, estimated: &[(&str, u64)]) -> bool
pub fn has_capacity(&self, id: &Id, estimated: &[(&str, u64)]) -> bool
Whether the node has capacity for the estimated usage across ALL dimensions.
Returns false if the node is marked exhausted or any dimension would exceed its limit.
Returns true for unregistered nodes (unknown = no quota constraint).
Sourcepub fn remaining(&self, id: &Id, dimension: &str) -> u64
pub fn remaining(&self, id: &Id, dimension: &str) -> u64
Remaining capacity in a specific dimension. Returns u64::MAX for unknown nodes/dimensions.
Sourcepub fn pressure(&self, id: &Id) -> f64
pub fn pressure(&self, id: &Id) -> f64
Pressure score across all dimensions: 0.0 = fully idle, 1.0 = at least one dimension full.
Computed as max(usage_i / limit_i) across all dimensions.
Dimensions with limit == 0 are skipped (treated as unconstrained).
Returns 0.0 for unregistered nodes. Returns 1.0 if the node is marked exhausted.