Skip to content

Commit

Permalink
Support getting initial stats for rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Sep 21, 2024
1 parent 4130dc9 commit 398e84e
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions publish/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,47 @@ async fn autopublish() {
unimplemented!()
}

async fn get_rate_limit() {
unimplemented!()
async fn get_rate_limit(
State(state): State<Arc<AppState>>,
Path(bucket): Path<String>,
) -> impl IntoResponse {
let stats = state.bucket_rate_limiter.lock().unwrap().get_stats();

if let Some(stats) = stats {
if let Some(current_open) = stats.per_bucket.get(&bucket) {
let max_open = state
.bucket_rate_limiter
.lock()
.unwrap()
.get_max_open(&bucket);
(
StatusCode::OK,
Json(
serde_json::to_value(&BucketRateLimit {
open: Some(*current_open),
max_open,
remaining: max_open.map(|max_open| max_open - *current_open),
})
.unwrap(),
),
)
} else {
(
StatusCode::NOT_FOUND,
Json(serde_json::json!({
"reason": "No such rate limit bucket",
"bucket": bucket,
})),
)
}
} else {
(
StatusCode::NOT_FOUND,
Json(serde_json::json!({
"reason": "No rate limit stats available",
})),
)
}
}

#[derive(serde::Serialize, serde::Deserialize)]
Expand Down

0 comments on commit 398e84e

Please sign in to comment.