Skip to content

Commit

Permalink
Re-added acquire_with_duration in rate_limiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed May 8, 2024
1 parent f62e425 commit 30e0e24
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions quickwit/quickwit-common/src/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ impl RateLimiter {
}
}

/// Acquires some permits from the rate limiter.
/// If the permits are not available, returns the duration to wait before trying again.
///
/// This method is currently only used in simian.
pub fn acquire_with_duration(&mut self, num_permits: u64) -> Result<(), Duration> {
if self.acquire_inner(num_permits) {
return Ok(());
}
self.refill(Instant::now());
if self.acquire_inner(num_permits) {
return Ok(());
}
let missing = num_permits - self.available_permits;
let wait = Duration::from_micros(missing * self.refill_period_micros / self.refill_amount);
Err(wait)
}

/// Acquires some permits expressed in bytes from the rate limiter. Returns whether the permits
/// were acquired.
pub fn acquire_bytes(&mut self, bytes: ByteSize) -> bool {
Expand Down

0 comments on commit 30e0e24

Please sign in to comment.