Skip to content

Commit

Permalink
better readability in TokenBucket.rs ++
Browse files Browse the repository at this point in the history
  • Loading branch information
fixxxedpoint committed Aug 16, 2024
1 parent 18aa9e1 commit 21b4b73
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions rate-limiter/src/token_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ where
}
}

fn calculate_delay(&self) -> Option<Instant> {
fn calculate_delay(&self) -> Option<Option<Instant>> {
if self.rate_per_second == 0 {
return Some(None);
}
if self.requested <= self.rate_per_second {
return None;
}
let delay_micros = (self.requested - self.rate_per_second)
.saturating_mul(1_000_000)
.saturating_div(self.rate_per_second);
Some(self.last_update + Duration::from_micros(delay_micros.try_into().unwrap_or(u64::MAX)))
Some(Some(
self.last_update + Duration::from_micros(delay_micros.try_into().unwrap_or(u64::MAX)),
))
}

fn update_units(&mut self) {
Expand All @@ -109,12 +114,8 @@ where
self.rate_per_second.saturating_sub(self.requested)
}

/// Accounts newly requested data.
/// Returns `true` if the user has requested more tokens than are currently available.
fn account_requested(&mut self, requested: usize) -> bool {
let available = self.available();
fn account_requested(&mut self, requested: usize) {
self.requested = self.requested.saturating_add(requested);
available < requested
}

/// Calculates [Duration](time::Duration) by which we should delay next call to some governed resource in order to satisfy
Expand All @@ -129,8 +130,8 @@ where
if self.available() < requested {
self.update_units();
}
self.account_requested(requested)
.then(|| self.calculate_delay())
self.account_requested(requested);
self.calculate_delay()
}
}

Expand Down

0 comments on commit 21b4b73

Please sign in to comment.