-
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
- Loading branch information
1 parent
236bac4
commit 4937bd3
Showing
6 changed files
with
114 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "rs621-ratelimit" | ||
edition = "2021" | ||
description = "Request rate limiting library for rs621." | ||
version.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
authors.workspace = true | ||
readme.workspace = true | ||
|
||
[target.'cfg(target_family = "wasm")'.dependencies] | ||
gloo-timers = { version = "0.3", features = ["futures"] } | ||
futures = { workspace = true, features = ["std", "alloc"] } | ||
web-time = "1.1.0" | ||
|
||
[target.'cfg(not(target_family = "wasm"))'.dependencies] | ||
tokio = { version = "1", features = ["time", "sync"] } |
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 crate::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#[cfg(not(target_family = "wasm"))] | ||
#[path = "tokio.rs"] | ||
mod platform; | ||
|
||
#[cfg(target_family = "wasm")] | ||
#[path = "gloo.rs"] | ||
mod platform; | ||
|
||
#[doc(inline)] | ||
pub use self::platform::*; | ||
|
||
use std::time::Duration; | ||
|
||
/// Forced cool down duration performed at every request. E621 allows at most 2 requests per second, | ||
/// so the lowest safe value we can have here is 500 ms. | ||
const REQ_COOLDOWN_DURATION: Duration = Duration::from_millis(600); |
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