Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rate_limit): add rate limiting exemption for health check endpo… #3591

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ee/tabby-webserver/src/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ impl Default for UserRateLimiter {
}

impl UserRateLimiter {
pub async fn is_allowed(&self, user_id: &str) -> bool {
pub async fn is_allowed(&self, uri: &axum::http::Uri, user_id: &str) -> bool {
// Do not limit health check requests.
if uri.path().ends_with("/v1/health") || uri.path().ends_with("/v1beta/health") {
return true;
}

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.
Expand Down Expand Up @@ -50,12 +55,18 @@ mod tests {
let user_id = "test_user";
let rate_limiter = UserRateLimiter::default();

let uri: axum::http::Uri = "/v1/completions".parse().unwrap();
let healthcheck_uri: axum::http::Uri = "/v1/health".parse().unwrap();

// Test that the first `USER_REQUEST_LIMIT_PER_MINUTE` requests are allowed
for _ in 0..USER_REQUEST_LIMIT_PER_MINUTE {
assert!(rate_limiter.is_allowed(user_id).await);
assert!(rate_limiter.is_allowed(&uri, user_id).await);
}

// Test that the 201st request is not allowed
assert!(!rate_limiter.is_allowed(user_id).await);
assert!(!rate_limiter.is_allowed(&uri, user_id).await);

// Test that health check requests are not limited
assert!(rate_limiter.is_allowed(&healthcheck_uri, user_id).await);
}
}
6 changes: 5 additions & 1 deletion ee/tabby-webserver/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@

if let Some(user) = user {
// Apply rate limiting when `user` is not none.
if !self.user_rate_limiter.is_allowed(&user).await {
if !self
.user_rate_limiter
.is_allowed(request.uri(), &user)
.await

Check warning on line 236 in ee/tabby-webserver/src/service/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/mod.rs#L233-L236

Added lines #L233 - L236 were not covered by tests
{
return axum::response::Response::builder()
.status(StatusCode::TOO_MANY_REQUESTS)
.body(Body::empty())
Expand Down
Loading