pub struct RateLimiter<K, C>where
C: Clock,{ /* private fields */ }Expand description
A rate limiter that enforces different quotas per key using the GCRA algorithm.
This implementation allows setting different rate limits for different keys, with an optional default quota for keys that don’t have specific quotas.
Implementations§
Source§impl<K> RateLimiter<K, MonotonicClock>
impl<K> RateLimiter<K, MonotonicClock>
Source§impl<K, C> RateLimiter<K, C>
impl<K, C> RateLimiter<K, C>
Source§impl<K> RateLimiter<K, FakeRelativeClock>
impl<K> RateLimiter<K, FakeRelativeClock>
Sourcepub fn advance_clock(&self, by: Duration)
pub fn advance_clock(&self, by: Duration)
Advances the fake clock by the specified duration.
This is only available for testing with FakeRelativeClock.
Source§impl<K, C> RateLimiter<K, C>
impl<K, C> RateLimiter<K, C>
Sourcepub fn add_quota_for_key(&self, key: K, value: Quota)
pub fn add_quota_for_key(&self, key: K, value: Quota)
Adds or updates a quota for a specific key.
Sourcepub fn check_key(&self, key: &K) -> Result<(), NotUntil<C::Instant>>
pub fn check_key(&self, key: &K) -> Result<(), NotUntil<C::Instant>>
Checks if the given key is allowed under the rate limit.
§Errors
Returns Err(NotUntil) if the key is rate-limited, indicating when it will be allowed.
Sourcepub async fn until_key_ready(&self, key: &K)
pub async fn until_key_ready(&self, key: &K)
Waits until the specified key is ready (not rate-limited).
Sourcepub async fn await_keys_ready(&self, keys: Option<&[K]>)
pub async fn await_keys_ready(&self, keys: Option<&[K]>)
Waits until all specified keys are ready (not rate-limited).
If no keys are provided, this function returns immediately. Uses fast paths for 0-2 keys to avoid stream scheduling overhead.