-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rate_limit): implement user rate limiting in tabby-webserver
- Added `rate_limit` module to `ee/tabby-webserver/src/lib.rs`. - Updated `crates/http-api-bindings/Cargo.toml` and `Cargo.toml` to include `ratelimit` dependency. - Added `rate_limit.rs` to `ee/tabby-webserver/src/` with implementation for user rate limiting. - Configured rate limiters to allow 200 requests per minute per user.
- Loading branch information
Showing
7 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ mod path; | |
mod routes; | ||
mod service; | ||
mod webserver; | ||
mod rate_limit; | ||
|
||
#[cfg(test)] | ||
pub use service::*; | ||
|
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,39 @@ | ||
use std::time::Duration; | ||
|
||
use cached::{Cached, TimedCache}; | ||
use tokio::sync::Mutex; | ||
|
||
pub struct UserRateLimiter { | ||
/// Mapping from user ID to rate limiter. | ||
rate_limiters: Mutex<TimedCache<String, ratelimit::Ratelimiter>>, | ||
} | ||
|
||
static USER_REQUEST_LIMIT_PER_MINUTE: u64 = 200; | ||
|
||
impl Default for UserRateLimiter { | ||
fn default() -> Self { | ||
Self { | ||
// User rate limiter is hardcoded to 200 requests per minute, thus the timespan is 60 seconds. | ||
rate_limiters: Mutex::new(TimedCache::with_lifespan(60)), | ||
} | ||
} | ||
} | ||
|
||
impl UserRateLimiter { | ||
pub async fn is_allowed(&self, user_id: &str) -> bool { | ||
let mut rate_limiters = self.rate_limiters.lock().await; | ||
let rate_limiter = rate_limiters.cache_get_or_set_with(user_id.to_string(), || { | ||
// Create a new rate limiter for this user. | ||
ratelimit::Ratelimiter::builder(USER_REQUEST_LIMIT_PER_MINUTE, Duration::from_secs(60)) | ||
.build() | ||
.expect("Failed to create rate limiter") | ||
}); | ||
if let Err(_sleep) = rate_limiter.try_wait() { | ||
// If the rate limiter is full, we return false. | ||
false | ||
} else { | ||
// If the rate limiter is not full, we return true. | ||
true | ||
} | ||
} | ||
} |
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