pub struct RateLimiter { /* private fields */ }Expand description
Per-provider token-bucket rate limiter.
Maintains a separate TokenBucket for each provider key. Providers not
explicitly configured get a bucket with the default capacity and rate.
§Example
use quantrs2_device::security::rate_limit::RateLimiter;
let mut limiter = RateLimiter::with_cloud_defaults();
// Fast path — tokens available
if limiter.try_consume("aws") {
// submit request
} else {
let delay = limiter.wait_time("aws");
// sleep(delay), then retry
}Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(default_capacity: f64, default_rate_per_second: f64) -> Self
pub fn new(default_capacity: f64, default_rate_per_second: f64) -> Self
Create a rate limiter with the given defaults for unconfigured providers.
§Arguments
default_capacity— burst limit for unknown providersdefault_rate_per_second— sustained rate for unknown providers
Sourcepub fn with_provider(
self,
provider: impl Into<String>,
capacity: f64,
rate: f64,
) -> Self
pub fn with_provider( self, provider: impl Into<String>, capacity: f64, rate: f64, ) -> Self
Register a provider-specific bucket, overriding the defaults.
Sourcepub fn try_consume(&mut self, provider: &str) -> bool
pub fn try_consume(&mut self, provider: &str) -> bool
Try to consume one token for the given provider key.
Creates a default bucket for the provider if it has not been seen before.
Returns true if the request can proceed immediately.
Sourcepub fn wait_time(&mut self, provider: &str) -> Duration
pub fn wait_time(&mut self, provider: &str) -> Duration
Estimate the wait time before a token is available for the given provider.
Returns Duration::ZERO if a token is available immediately.
Sourcepub fn available_tokens(&mut self, provider: &str) -> f64
pub fn available_tokens(&mut self, provider: &str) -> f64
Return the number of available tokens for a provider (after refill).
Creates a default bucket if the provider has not been seen before.
Sourcepub fn with_cloud_defaults() -> Self
pub fn with_cloud_defaults() -> Self
Pre-configured limiter with typical cloud provider limits:
| Provider | Burst | Sustained |
|---|---|---|
| IBM | 5 | 5 / 60s |
| AWS | 10 | 10/s |
| Azure | 10 | 10/s |
Unknown providers get a 10-token bucket at 1 token/second.
Sourcepub fn tracked_providers(&self) -> Vec<&str>
pub fn tracked_providers(&self) -> Vec<&str>
Return the list of currently tracked provider keys
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.