pub struct DistributedRateLimiter { /* private fields */ }Expand description
Ergonomic handle for cluster-wide rate limiting against a backend.
Pairs a DistributedRateLimitBackend with a limiter key and a
RateLimitConfig. The algorithm (token bucket vs sliding window) is taken
from the config, exactly as with the in-process limiters in
crate::rate_limit.
Cloning is cheap: the backend is held behind an Arc and shared.
Implementations§
Source§impl DistributedRateLimiter
impl DistributedRateLimiter
Sourcepub fn new(
backend: Arc<dyn DistributedRateLimitBackend>,
key: impl Into<String>,
config: RateLimitConfig,
) -> Self
pub fn new( backend: Arc<dyn DistributedRateLimitBackend>, key: impl Into<String>, config: RateLimitConfig, ) -> Self
Create a new distributed rate limiter.
§Arguments
backend- Shared store implementingDistributedRateLimitBackend.key- Logical limiter name (e.g. a task name or queue).config- Rate limit configuration (selects the algorithm).
Sourcepub fn config(&self) -> &RateLimitConfig
pub fn config(&self) -> &RateLimitConfig
The active configuration.
Sourcepub fn algorithm(&self) -> DistributedAlgorithm
pub fn algorithm(&self) -> DistributedAlgorithm
The active algorithm.
Sourcepub async fn try_acquire(&self) -> Result<bool>
pub async fn try_acquire(&self) -> Result<bool>
Try to acquire a single permit cluster-wide.
Returns Ok(true) if granted, Ok(false) if rate limited.
§Errors
Returns an error if the backend store is unavailable.
Sourcepub async fn try_acquire_n(&self, cost: f64) -> Result<AcquireOutcome>
pub async fn try_acquire_n(&self, cost: f64) -> Result<AcquireOutcome>
Try to acquire cost permits atomically.
Returns the full AcquireOutcome so callers can inspect the remaining
permits and suggested retry delay.
§Errors
Returns an error if the backend store is unavailable.
Sourcepub async fn acquire(&self, max_sleep: Duration) -> Result<Duration>
pub async fn acquire(&self, max_sleep: Duration) -> Result<Duration>
Acquire a single permit, awaiting (with bounded sleeps) until granted.
Returns the total time waited. Each retry sleeps for the backend’s
suggested retry_after, clamped to max_sleep to remain responsive
under contention from other workers.
§Errors
Returns an error if the backend store is unavailable.
Sourcepub async fn available(&self) -> Result<f64>
pub async fn available(&self) -> Result<f64>
Available permits for this limiter (after refill).
§Errors
Returns an error if the backend store is unavailable.
Sourcepub async fn time_until_available(&self) -> Result<Duration>
pub async fn time_until_available(&self) -> Result<Duration>
Estimated time until a single permit is available.
§Errors
Returns an error if the backend store is unavailable.