Skip to main content

QuotaTracker

Struct QuotaTracker 

Source
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>

Source

pub fn new() -> Self

Creates a new, empty tracker.

Source

pub fn register(&self, id: &Id, config: QuotaConfig)

Register a node with its quota configuration. If already registered, replaces config.

Source

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.

Source

pub fn mark_exhausted(&self, id: &Id, duration: Duration)

Mark a node as exhausted for duration (e.g., from a 429 Retry-After header).

Source

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).

Source

pub fn remaining(&self, id: &Id, dimension: &str) -> u64

Remaining capacity in a specific dimension. Returns u64::MAX for unknown nodes/dimensions.

Source

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.

Trait Implementations§

Source§

impl<Id: Eq + Hash + Clone> Debug for QuotaTracker<Id>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<Id: Eq + Hash + Clone> Default for QuotaTracker<Id>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Id> !Freeze for QuotaTracker<Id>

§

impl<Id> RefUnwindSafe for QuotaTracker<Id>

§

impl<Id> Send for QuotaTracker<Id>
where Id: Send,

§

impl<Id> Sync for QuotaTracker<Id>
where Id: Send + Sync,

§

impl<Id> Unpin for QuotaTracker<Id>
where Id: Unpin,

§

impl<Id> UnsafeUnpin for QuotaTracker<Id>

§

impl<Id> UnwindSafe for QuotaTracker<Id>

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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V