Skip to content

Commit 71c0f9b

Browse files
committed
limit
1 parent 40d88c3 commit 71c0f9b

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

lib/bencher_json/src/system/config/plus/rate_limiting.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
66
#[cfg_attr(feature = "schema", derive(JsonSchema))]
77
pub struct JsonRateLimiting {
88
pub window: Option<u32>,
9+
pub user_limit: Option<u32>,
910
pub unclaimed_limit: Option<u32>,
1011
pub claimed_limit: Option<u32>,
1112
}

lib/bencher_schema/src/context/rate_limiting.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ use crate::{
1616
use super::DbConnection;
1717

1818
const DAY: Duration = Duration::from_secs(24 * 60 * 60);
19-
const UNCLAIMED_RATE_LIMIT: u32 = u8::MAX as u32;
20-
const CLAIMED_RATE_LIMIT: u32 = u16::MAX as u32;
19+
const USER_LIMIT: u32 = u8::MAX as u32;
20+
const UNCLAIMED_LIMIT: u32 = u8::MAX as u32;
21+
const CLAIMED_LIMIT: u32 = u16::MAX as u32;
2122

2223
pub struct RateLimiting {
2324
pub window: Duration,
25+
pub user_limit: u32,
2426
pub unclaimed_limit: u32,
2527
pub claimed_limit: u32,
2628
}
@@ -69,13 +71,15 @@ impl From<JsonRateLimiting> for RateLimiting {
6971
fn from(json: JsonRateLimiting) -> Self {
7072
let JsonRateLimiting {
7173
window,
74+
user_limit,
7275
unclaimed_limit,
7376
claimed_limit,
7477
} = json;
7578
Self {
7679
window: window.map(u64::from).map_or(DAY, Duration::from_secs),
77-
unclaimed_limit: unclaimed_limit.unwrap_or(UNCLAIMED_RATE_LIMIT),
78-
claimed_limit: claimed_limit.unwrap_or(CLAIMED_RATE_LIMIT),
80+
user_limit: user_limit.unwrap_or(USER_LIMIT),
81+
unclaimed_limit: unclaimed_limit.unwrap_or(UNCLAIMED_LIMIT),
82+
claimed_limit: claimed_limit.unwrap_or(CLAIMED_LIMIT),
7983
}
8084
}
8185
}
@@ -84,8 +88,9 @@ impl Default for RateLimiting {
8488
fn default() -> Self {
8589
Self {
8690
window: DAY,
87-
unclaimed_limit: UNCLAIMED_RATE_LIMIT,
88-
claimed_limit: CLAIMED_RATE_LIMIT,
91+
user_limit: USER_LIMIT,
92+
unclaimed_limit: UNCLAIMED_LIMIT,
93+
claimed_limit: CLAIMED_LIMIT,
8994
}
9095
}
9196
}

lib/bencher_schema/src/model/organization/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl InsertOrganization {
327327
)}
328328
)?;
329329

330-
let rate_limit = context.rate_limiting.unclaimed_limit;
330+
let rate_limit = context.rate_limiting.user_limit;
331331
if creation_count >= rate_limit {
332332
Err(crate::error::too_many_requests(RateLimitingError::User {
333333
user: query_user.clone(),

lib/bencher_schema/src/model/project/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ impl InsertProject {
505505
) -> Result<(), HttpError> {
506506
use crate::context::RateLimitingError;
507507

508+
let is_claimed = query_organization.is_claimed(conn_lock!(context))?;
509+
508510
let resource = BencherResource::Project;
509511
let (start_time, end_time) = context.rate_limiting.window();
510512
let creation_count: u32 = schema::project::table
@@ -523,7 +525,11 @@ impl InsertProject {
523525
)}
524526
)?;
525527

526-
let rate_limit = context.rate_limiting.unclaimed_limit;
528+
let rate_limit = if is_claimed {
529+
context.rate_limiting.claimed_limit
530+
} else {
531+
context.rate_limiting.unclaimed_limit
532+
};
527533
if creation_count >= rate_limit {
528534
Err(crate::error::too_many_requests(
529535
RateLimitingError::Organization {

lib/bencher_schema/src/model/user/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl InsertToken {
118118
)}
119119
)?;
120120

121-
let rate_limit = context.rate_limiting.unclaimed_limit;
121+
let rate_limit = context.rate_limiting.user_limit;
122122
if creation_count >= rate_limit {
123123
Err(crate::error::too_many_requests(RateLimitingError::User {
124124
user: query_user.clone(),

services/api/openapi.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10651,6 +10651,12 @@
1065110651
"format": "uint32",
1065210652
"minimum": 0
1065310653
},
10654+
"user_limit": {
10655+
"nullable": true,
10656+
"type": "integer",
10657+
"format": "uint32",
10658+
"minimum": 0
10659+
},
1065410660
"window": {
1065510661
"nullable": true,
1065610662
"type": "integer",

0 commit comments

Comments
 (0)