Skip to content

Commit 3ab10db

Browse files
committed
rate_limiting
1 parent fdae24e commit 3ab10db

File tree

14 files changed

+79
-77
lines changed

14 files changed

+79
-77
lines changed

lib/bencher_config/src/config_tx.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bencher_json::{
1313
use bencher_rbac::init_rbac;
1414
use bencher_schema::context::{ApiContext, Database, DbConnection};
1515
#[cfg(feature = "plus")]
16-
use bencher_schema::{context::RateLimit, model::server::QueryServer};
16+
use bencher_schema::{context::RateLimiting, model::server::QueryServer};
1717
use bencher_token::TokenKey;
1818
#[cfg(feature = "plus")]
1919
use diesel::connection::SimpleConnection;
@@ -53,7 +53,7 @@ pub enum ConfigTxError {
5353
#[error("Failed to parse data store: {0}")]
5454
DataStore(bencher_schema::context::DataStoreError),
5555
#[error("Failed to configure rate limits: {0}")]
56-
RateLimit(Box<bencher_schema::context::RateLimitError>),
56+
RateLimit(Box<bencher_schema::context::RateLimitingError>),
5757
#[error("Failed to register endpoint: {0}")]
5858
Register(dropshot::ApiDescriptionRegisterError),
5959
#[error("Failed to create server: {0}")]
@@ -206,7 +206,7 @@ async fn into_context(
206206
);
207207

208208
#[cfg(feature = "plus")]
209-
let rate_limit = plus.as_ref().and_then(|plus| plus.rate_limit);
209+
let rate_limiting = plus.as_ref().and_then(|plus| plus.rate_limiting);
210210

211211
info!(&log, "Configuring Bencher Plus");
212212
#[cfg(feature = "plus")]
@@ -222,12 +222,12 @@ async fn into_context(
222222
let is_bencher_cloud = bencher_json::is_bencher_cloud(&console_url) && biller.is_some();
223223

224224
#[cfg(feature = "plus")]
225-
let rate_limit = RateLimit::new(
225+
let rate_limiting = RateLimiting::new(
226226
log,
227227
&database.connection,
228228
&licensor,
229229
is_bencher_cloud,
230-
rate_limit,
230+
rate_limiting,
231231
)
232232
.await
233233
.map_err(Box::new)
@@ -242,7 +242,7 @@ async fn into_context(
242242
database,
243243
restart_tx,
244244
#[cfg(feature = "plus")]
245-
rate_limit,
245+
rate_limiting,
246246
#[cfg(feature = "plus")]
247247
github,
248248
#[cfg(feature = "plus")]

lib/bencher_json/src/system/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use plus::{
2222
JsonCloud,
2323
},
2424
litestream::{JsonLitestream, JsonReplica},
25-
rate_limit::JsonRateLimit,
25+
rate_limiting::JsonRateLimiting,
2626
stats::JsonStats,
2727
JsonPlus,
2828
};

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ use serde::{Deserialize, Serialize};
88
pub mod cloud;
99
pub mod github;
1010
pub mod litestream;
11-
pub mod rate_limit;
11+
pub mod rate_limiting;
1212
pub mod stats;
1313

1414
pub use cloud::JsonCloud;
1515
pub use github::JsonGitHub;
1616
pub use litestream::JsonLitestream;
17-
pub use rate_limit::JsonRateLimit;
17+
pub use rate_limiting::JsonRateLimiting;
1818
pub use stats::JsonStats;
1919

2020
#[derive(Debug, Clone, Serialize, Deserialize)]
2121
#[cfg_attr(feature = "schema", derive(JsonSchema))]
2222
pub struct JsonPlus {
2323
#[serde(skip_serializing_if = "Option::is_none")]
24-
pub rate_limit: Option<JsonRateLimit>,
24+
pub rate_limiting: Option<JsonRateLimiting>,
2525
#[serde(skip_serializing_if = "Option::is_none")]
2626
pub github: Option<JsonGitHub>,
2727
#[serde(alias = "disaster_recovery", skip_serializing_if = "Option::is_none")]

lib/bencher_json/src/system/config/plus/rate_limit.rs renamed to lib/bencher_json/src/system/config/plus/rate_limiting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
44

55
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
66
#[cfg_attr(feature = "schema", derive(JsonSchema))]
7-
pub struct JsonRateLimit {
7+
pub struct JsonRateLimiting {
88
pub window: Option<u32>,
9-
pub unclaimed: Option<u32>,
10-
pub claimed: Option<u32>,
9+
pub unclaimed_limit: Option<u32>,
10+
pub claimed_limit: Option<u32>,
1111
}

lib/bencher_schema/src/context/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod database;
1515
mod indexer;
1616
mod messenger;
1717
#[cfg(feature = "plus")]
18-
mod rate_limit;
18+
mod rate_limiting;
1919
mod rbac;
2020
#[cfg(feature = "plus")]
2121
mod stats;
@@ -27,7 +27,7 @@ pub use indexer::{IndexError, Indexer};
2727
pub use messenger::ServerStatsBody;
2828
pub use messenger::{Body, ButtonBody, Email, Message, Messenger, NewUserBody};
2929
#[cfg(feature = "plus")]
30-
pub use rate_limit::{RateLimit, RateLimitError};
30+
pub use rate_limiting::{RateLimiting, RateLimitingError};
3131
pub use rbac::{Rbac, RbacError};
3232
#[cfg(feature = "plus")]
3333
pub use stats::StatsSettings;
@@ -40,7 +40,7 @@ pub struct ApiContext {
4040
pub database: Database,
4141
pub restart_tx: Sender<()>,
4242
#[cfg(feature = "plus")]
43-
pub rate_limit: RateLimit,
43+
pub rate_limiting: RateLimiting,
4444
#[cfg(feature = "plus")]
4545
pub github: Option<GitHub>,
4646
#[cfg(feature = "plus")]

lib/bencher_schema/src/context/rate_limit.rs renamed to lib/bencher_schema/src/context/rate_limiting.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::Duration;
22

3-
use bencher_json::{system::config::JsonRateLimit, DateTime, PlanLevel};
3+
use bencher_json::{system::config::JsonRateLimiting, DateTime, PlanLevel};
44
use bencher_license::Licensor;
55
use slog::Logger;
66

@@ -19,14 +19,14 @@ const DAY: Duration = Duration::from_secs(24 * 60 * 60);
1919
const UNCLAIMED_RATE_LIMIT: u32 = u8::MAX as u32;
2020
const CLAIMED_RATE_LIMIT: u32 = u16::MAX as u32;
2121

22-
pub struct RateLimit {
22+
pub struct RateLimiting {
2323
pub window: Duration,
24-
pub unclaimed: u32,
25-
pub claimed: u32,
24+
pub unclaimed_limit: u32,
25+
pub claimed_limit: u32,
2626
}
2727

2828
#[derive(Debug, thiserror::Error)]
29-
pub enum RateLimitError {
29+
pub enum RateLimitingError {
3030
#[error("User ({uuid}) has exceeded the daily rate limit ({rate_limit}) for {resource} creation. Please, reduce your daily usage.", uuid = user.uuid)]
3131
User {
3232
user: QueryUser,
@@ -65,40 +65,40 @@ pub enum RateLimitError {
6565
},
6666
}
6767

68-
impl From<JsonRateLimit> for RateLimit {
69-
fn from(json: JsonRateLimit) -> Self {
70-
let JsonRateLimit {
68+
impl From<JsonRateLimiting> for RateLimiting {
69+
fn from(json: JsonRateLimiting) -> Self {
70+
let JsonRateLimiting {
7171
window,
72-
unclaimed,
73-
claimed,
72+
unclaimed_limit,
73+
claimed_limit,
7474
} = json;
7575
Self {
7676
window: window.map(u64::from).map_or(DAY, Duration::from_secs),
77-
unclaimed: unclaimed.unwrap_or(UNCLAIMED_RATE_LIMIT),
78-
claimed: claimed.unwrap_or(CLAIMED_RATE_LIMIT),
77+
unclaimed_limit: unclaimed_limit.unwrap_or(UNCLAIMED_RATE_LIMIT),
78+
claimed_limit: claimed_limit.unwrap_or(CLAIMED_RATE_LIMIT),
7979
}
8080
}
8181
}
8282

83-
impl Default for RateLimit {
83+
impl Default for RateLimiting {
8484
fn default() -> Self {
8585
Self {
8686
window: DAY,
87-
unclaimed: UNCLAIMED_RATE_LIMIT,
88-
claimed: CLAIMED_RATE_LIMIT,
87+
unclaimed_limit: UNCLAIMED_RATE_LIMIT,
88+
claimed_limit: CLAIMED_RATE_LIMIT,
8989
}
9090
}
9191
}
9292

93-
impl RateLimit {
93+
impl RateLimiting {
9494
pub async fn new(
9595
log: &Logger,
9696
conn: &tokio::sync::Mutex<DbConnection>,
9797
licensor: &Licensor,
9898
is_bencher_cloud: bool,
99-
rate_limit: Option<JsonRateLimit>,
100-
) -> Result<Self, RateLimitError> {
101-
let Some(rate_limit) = rate_limit else {
99+
rate_limiting: Option<JsonRateLimiting>,
100+
) -> Result<Self, RateLimitingError> {
101+
let Some(rate_limiting) = rate_limiting else {
102102
return Ok(Self::default());
103103
};
104104

@@ -118,7 +118,7 @@ impl RateLimit {
118118
}
119119
}
120120

121-
Ok(rate_limit.into())
121+
Ok(rate_limiting.into())
122122
}
123123

124124
pub fn window(&self) -> (DateTime, DateTime) {

lib/bencher_schema/src/macros/rate_limit.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! fn_rate_limit {
99
let query_organization = query_project.organization(conn_lock!(context))?;
1010
let is_claimed = query_organization.is_claimed(conn_lock!(context))?;
1111

12-
let (start_time, end_time) = context.rate_limit.window();
12+
let (start_time, end_time) = context.rate_limiting.window();
1313
let creation_count: u32 = $crate::schema::$table::table
1414
.filter($crate::schema::$table::project_id.eq(project_id))
1515
.filter($crate::schema::$table::created.ge(start_time))
@@ -28,24 +28,24 @@ macro_rules! fn_rate_limit {
2828

2929
match (is_claimed, creation_count) {
3030
(false, creation_count)
31-
if creation_count >= context.rate_limit.unclaimed =>
31+
if creation_count >= context.rate_limiting.unclaimed_limit =>
3232
{
3333
Err($crate::error::too_many_requests(
34-
$crate::context::RateLimitError::UnclaimedProject {
34+
$crate::context::RateLimitingError::UnclaimedProject {
3535
project: query_project,
3636
resource: $crate::error::BencherResource::$resource,
37-
rate_limit: context.rate_limit.unclaimed,
37+
rate_limit: context.rate_limiting.unclaimed_limit,
3838
},
3939
))
4040
},
4141
(true, creation_count)
42-
if creation_count >= context.rate_limit.claimed =>
42+
if creation_count >= context.rate_limiting.claimed_limit =>
4343
{
4444
Err($crate::error::too_many_requests(
45-
$crate::context::RateLimitError::ClaimedProject {
45+
$crate::context::RateLimitingError::ClaimedProject {
4646
project: query_project,
4747
resource: $crate::error::BencherResource::$resource,
48-
rate_limit: context.rate_limit.claimed,
48+
rate_limit: context.rate_limiting.claimed_limit,
4949
},
5050
))
5151
},

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl QueryOrganization {
264264
pub async fn daily_usage(&self, context: &ApiContext) -> Result<u32, HttpError> {
265265
use crate::model::project::metric::QueryMetric;
266266

267-
let (start_time, end_time) = context.rate_limit.window();
267+
let (start_time, end_time) = context.rate_limiting.window();
268268
QueryMetric::usage(conn_lock!(context), self.id, start_time, end_time)
269269
}
270270

@@ -306,10 +306,10 @@ pub struct InsertOrganization {
306306
impl InsertOrganization {
307307
#[cfg(feature = "plus")]
308308
pub async fn rate_limit(context: &ApiContext, query_user: &QueryUser) -> Result<(), HttpError> {
309-
use crate::context::RateLimitError;
309+
use crate::context::RateLimitingError;
310310

311311
let resource = BencherResource::Organization;
312-
let (start_time, end_time) = context.rate_limit.window();
312+
let (start_time, end_time) = context.rate_limiting.window();
313313
let creation_count: u32 = schema::organization::table
314314
.inner_join(schema::organization_role::table)
315315
.filter(schema::organization_role::user_id.eq(query_user.id))
@@ -327,9 +327,9 @@ impl InsertOrganization {
327327
)}
328328
)?;
329329

330-
let rate_limit = context.rate_limit.unclaimed;
330+
let rate_limit = context.rate_limiting.unclaimed_limit;
331331
if creation_count >= rate_limit {
332-
Err(crate::error::too_many_requests(RateLimitError::User {
332+
Err(crate::error::too_many_requests(RateLimitingError::User {
333333
user: query_user.clone(),
334334
resource,
335335
rate_limit,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@ impl PlanKind {
281281
let is_claimed = query_organization.is_claimed(conn_lock!(context))?;
282282
let daily_usage = query_organization.daily_usage(context).await?;
283283
match (is_claimed, daily_usage) {
284-
(false, daily_usage) if daily_usage >= context.rate_limit.unclaimed => {
284+
(false, daily_usage) if daily_usage >= context.rate_limiting.unclaimed_limit => {
285285
Err(payment_required_error(PlanKindError::UnclaimedUsage {
286286
organization: query_organization.clone(),
287-
rate_limit: context.rate_limit.unclaimed,
287+
rate_limit: context.rate_limiting.unclaimed_limit,
288288
}))
289289
},
290-
(true, daily_usage) if daily_usage >= context.rate_limit.claimed => {
290+
(true, daily_usage) if daily_usage >= context.rate_limiting.claimed_limit => {
291291
Err(payment_required_error(PlanKindError::ClaimedUsage {
292292
organization: query_organization.clone(),
293-
rate_limit: context.rate_limit.claimed,
293+
rate_limit: context.rate_limiting.claimed_limit,
294294
}))
295295
},
296296
(_, _) => Ok(Self::None),

lib/bencher_schema/src/model/project/branch/head.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ impl InsertHead {
206206
context: &ApiContext,
207207
query_branch: &QueryBranch,
208208
) -> Result<(), HttpError> {
209-
use crate::{context::RateLimitError, error::BencherResource};
209+
use crate::{context::RateLimitingError, error::BencherResource};
210210

211211
let resource = BencherResource::Head;
212-
let (start_time, end_time) = context.rate_limit.window();
212+
let (start_time, end_time) = context.rate_limiting.window();
213213
let creation_count: u32 = schema::head::table
214214
.filter(schema::head::branch_id.eq(query_branch.id))
215215
.filter(schema::head::created.ge(start_time))
@@ -230,9 +230,9 @@ impl InsertHead {
230230
// or by updating an existing branch using the API.
231231
// The running of a Report will be rate limited already for unclaimed projects,
232232
// and the API endpoint to update an existing branch would require authentication and would therefore be a claimed project.
233-
let rate_limit = context.rate_limit.claimed;
233+
let rate_limit = context.rate_limiting.claimed_limit;
234234
if creation_count >= rate_limit {
235-
Err(crate::error::too_many_requests(RateLimitError::Branch {
235+
Err(crate::error::too_many_requests(RateLimitingError::Branch {
236236
branch: query_branch.clone(),
237237
resource,
238238
rate_limit,

0 commit comments

Comments
 (0)