Skip to content

Commit

Permalink
refactor: use result instead of waitresult enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed May 25, 2024
1 parent 8d2751c commit fb3b467
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ use std::{

use parking_lot::Mutex;

pub enum WaitResult {
Ready,
Pending(Duration),
}

#[derive(Debug, Clone)]
pub struct RateLimiter {
rate: u64,
Expand All @@ -33,11 +28,11 @@ impl RateLimiter {
}
}

pub fn try_wait(&self) -> WaitResult {
pub fn try_wait(&self) -> Result<(), Duration> {
let mut completed = self.completed.lock();
if let Some(new_completed) = completed.checked_sub(1) {
*completed = new_completed;
return WaitResult::Ready;
return Ok(());
}

*completed = self.rate;
Expand All @@ -49,10 +44,10 @@ impl RateLimiter {

if let Some(new_remaining) = remaining.checked_sub(delta) {
*remaining = new_remaining;
WaitResult::Pending(new_remaining)
Err(new_remaining)
} else {
*remaining = self.reset;
WaitResult::Ready
Ok(())
}
}

Expand Down Expand Up @@ -97,8 +92,8 @@ impl Future for RateLimitWait {
*self.waker.lock() = Some(cx.waker().clone());

match self.limiter.try_wait() {
WaitResult::Ready => Poll::Ready(()),
WaitResult::Pending(_) => Poll::Pending,
Ok(()) => Poll::Ready(()),
Err(_) => Poll::Pending,
}
}
}

0 comments on commit fb3b467

Please sign in to comment.