-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make a browser compatible rate limiter (#11)
- Loading branch information
1 parent
299494b
commit 0924c2c
Showing
4 changed files
with
79 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::REQ_COOLDOWN_DURATION; | ||
|
||
use futures::lock::{Mutex, MutexGuard}; | ||
|
||
use std::future::Future; | ||
use std::sync::Arc; | ||
|
||
use web_time::Instant; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct RateLimit { | ||
// Use a `futures` `Mutex` because ~500ms is crazy long to block an async task. | ||
deadline: Arc<Mutex<Option<Instant>>>, | ||
} | ||
|
||
struct Guard<'a>(MutexGuard<'a, Option<Instant>>); | ||
|
||
impl<'a> Drop for Guard<'a> { | ||
fn drop(&mut self) { | ||
// Use a `Drop` impl so that updating the deadline is panic-safe. | ||
*self.0 = Some(Instant::now() + REQ_COOLDOWN_DURATION); | ||
} | ||
} | ||
|
||
impl RateLimit { | ||
async fn lock(&self) -> Guard { | ||
loop { | ||
let now = Instant::now(); | ||
|
||
let deadline = { | ||
let guard = self.deadline.lock().await; | ||
|
||
match &*guard { | ||
None => return Guard(guard), | ||
Some(deadline) if now >= *deadline => return Guard(guard), | ||
Some(deadline) => *deadline, | ||
} | ||
}; | ||
|
||
gloo_timers::future::sleep(deadline - now).await; | ||
} | ||
} | ||
|
||
pub async fn check<F, R>(self, fut: F) -> R | ||
where | ||
F: Future<Output = R>, | ||
{ | ||
let guard = self.lock().await; | ||
let result = fut.await; | ||
drop(guard); | ||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters