Skip to content

Commit acc2654

Browse files
committed
rename PublishRateLimit to RateLimiter
1 parent 120ab47 commit acc2654

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::publish_rate_limit::PublishRateLimit;
1+
use crate::rate_limiter::RateLimiter;
22
use crate::{env, env_optional, uploaders::Uploader, Env};
33

44
mod base;
@@ -16,7 +16,7 @@ pub struct Server {
1616
pub gh_base_url: String,
1717
pub max_upload_size: u64,
1818
pub max_unpack_size: u64,
19-
pub publish_rate_limit: PublishRateLimit,
19+
pub publish_rate_limit: RateLimiter,
2020
pub blocked_traffic: Vec<(String, Vec<String>)>,
2121
pub max_allowed_page_offset: u32,
2222
pub page_offset_ua_blocklist: Vec<String>,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub mod git;
4444
pub mod github;
4545
pub mod metrics;
4646
pub mod middleware;
47-
mod publish_rate_limit;
47+
mod rate_limiter;
4848
pub mod render;
4949
pub mod schema;
5050
pub mod tasks;

src/models/krate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::models::{
1515
use crate::util::errors::{cargo_err, AppResult};
1616

1717
use crate::models::helpers::with_count::*;
18-
use crate::publish_rate_limit::PublishRateLimit;
18+
use crate::rate_limiter::RateLimiter;
1919
use crate::schema::*;
2020

2121
#[derive(Debug, Queryable, Identifiable, Associations, Clone, Copy)]
@@ -97,7 +97,7 @@ impl<'a> NewCrate<'a> {
9797
self,
9898
conn: &PgConnection,
9999
uploader: i32,
100-
rate_limit: Option<&PublishRateLimit>,
100+
rate_limit: Option<&RateLimiter>,
101101
) -> AppResult<Crate> {
102102
use diesel::update;
103103

src/publish_rate_limit.rs renamed to src/rate_limiter.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ crate::pg_enum! {
1313
}
1414

1515
#[derive(Debug, Clone, Copy)]
16-
pub struct PublishRateLimit {
16+
pub struct RateLimiter {
1717
pub rate: Duration,
1818
pub burst: i32,
1919
}
2020

21-
impl Default for PublishRateLimit {
21+
impl Default for RateLimiter {
2222
fn default() -> Self {
2323
let minutes = dotenv::var("WEB_NEW_PKG_RATE_LIMIT_RATE_MINUTES")
2424
.unwrap_or_default()
@@ -37,9 +37,9 @@ impl Default for PublishRateLimit {
3737
}
3838
}
3939

40-
impl PublishRateLimit {
41-
pub fn check_rate_limit(&self, uploader: i32, conn: &PgConnection) -> AppResult<()> {
42-
let bucket = self.take_token(uploader, Utc::now().naive_utc(), conn)?;
40+
impl RateLimiter {
41+
pub fn check_rate_limit(&self, user_id: i32, conn: &PgConnection) -> AppResult<()> {
42+
let bucket = self.take_token(user_id, Utc::now().naive_utc(), conn)?;
4343
if bucket.tokens >= 1 {
4444
Ok(())
4545
} else {
@@ -59,11 +59,10 @@ impl PublishRateLimit {
5959
/// since we only refill buckets when trying to take a token from it.
6060
fn take_token(
6161
&self,
62-
uploader: i32,
62+
user_id: i32,
6363
now: NaiveDateTime,
6464
conn: &PgConnection,
6565
) -> QueryResult<Bucket> {
66-
use self::publish_limit_buckets::dsl::*;
6766
use diesel::sql_types::{Double, Interval, Text, Timestamp};
6867

6968
sql_function!(fn date_part(x: Text, y: Timestamp) -> Double);
@@ -76,7 +75,7 @@ impl PublishRateLimit {
7675
sql_function!(fn least<T>(x: T, y: T) -> T);
7776

7877
let burst: i32 = publish_rate_overrides::table
79-
.find((uploader, LimitedAction::PublishNew))
78+
.find((user_id, LimitedAction::PublishNew))
8079
.filter(
8180
publish_rate_overrides::expires_at
8281
.is_null()
@@ -91,18 +90,25 @@ impl PublishRateLimit {
9190
// However, for the intervals we're dealing with, it is always well
9291
// defined, so we convert to an f64 of seconds to represent this.
9392
let tokens_to_add = floor(
94-
(date_part("epoch", now) - date_part("epoch", last_refill))
93+
(date_part("epoch", now) - date_part("epoch", publish_limit_buckets::last_refill))
9594
/ interval_part("epoch", self.refill_rate()),
9695
);
9796

98-
diesel::insert_into(publish_limit_buckets)
99-
.values((user_id.eq(uploader), tokens.eq(burst), last_refill.eq(now)))
100-
.on_conflict(user_id)
97+
diesel::insert_into(publish_limit_buckets::table)
98+
.values((
99+
publish_limit_buckets::user_id.eq(user_id),
100+
publish_limit_buckets::tokens.eq(burst),
101+
publish_limit_buckets::last_refill.eq(now),
102+
))
103+
.on_conflict(publish_limit_buckets::user_id)
101104
.do_update()
102105
.set((
103-
tokens.eq(least(burst, greatest(0, tokens - 1) + tokens_to_add)),
104-
last_refill
105-
.eq(last_refill + self.refill_rate().into_sql::<Interval>() * tokens_to_add),
106+
publish_limit_buckets::tokens.eq(least(
107+
burst,
108+
greatest(0, publish_limit_buckets::tokens - 1) + tokens_to_add,
109+
)),
110+
publish_limit_buckets::last_refill.eq(publish_limit_buckets::last_refill
111+
+ self.refill_rate().into_sql::<Interval>() * tokens_to_add),
106112
))
107113
.get_result(conn)
108114
}
@@ -134,7 +140,7 @@ mod tests {
134140
let conn = pg_connection();
135141
let now = now();
136142

137-
let rate = PublishRateLimit {
143+
let rate = RateLimiter {
138144
rate: Duration::from_secs(1),
139145
burst: 10,
140146
};
@@ -147,7 +153,7 @@ mod tests {
147153
};
148154
assert_eq!(expected, bucket);
149155

150-
let rate = PublishRateLimit {
156+
let rate = RateLimiter {
151157
rate: Duration::from_millis(50),
152158
burst: 20,
153159
};
@@ -167,7 +173,7 @@ mod tests {
167173
let conn = pg_connection();
168174
let now = now();
169175

170-
let rate = PublishRateLimit {
176+
let rate = RateLimiter {
171177
rate: Duration::from_secs(1),
172178
burst: 10,
173179
};
@@ -188,7 +194,7 @@ mod tests {
188194
let conn = pg_connection();
189195
let now = now();
190196

191-
let rate = PublishRateLimit {
197+
let rate = RateLimiter {
192198
rate: Duration::from_secs(1),
193199
burst: 10,
194200
};
@@ -214,7 +220,7 @@ mod tests {
214220
NaiveDateTime::parse_from_str("2019-03-19T21:11:24.620401", "%Y-%m-%dT%H:%M:%S%.f")
215221
.unwrap();
216222

217-
let rate = PublishRateLimit {
223+
let rate = RateLimiter {
218224
rate: Duration::from_millis(100),
219225
burst: 10,
220226
};
@@ -236,7 +242,7 @@ mod tests {
236242
let conn = pg_connection();
237243
let now = now();
238244

239-
let rate = PublishRateLimit {
245+
let rate = RateLimiter {
240246
rate: Duration::from_millis(100),
241247
burst: 10,
242248
};
@@ -258,7 +264,7 @@ mod tests {
258264
let conn = pg_connection();
259265
let now = now();
260266

261-
let rate = PublishRateLimit {
267+
let rate = RateLimiter {
262268
rate: Duration::from_secs(1),
263269
burst: 10,
264270
};
@@ -282,7 +288,7 @@ mod tests {
282288
let conn = pg_connection();
283289
let now = now();
284290

285-
let rate = PublishRateLimit {
291+
let rate = RateLimiter {
286292
rate: Duration::from_secs(1),
287293
burst: 10,
288294
};
@@ -305,7 +311,7 @@ mod tests {
305311
let conn = pg_connection();
306312
let now = now();
307313

308-
let rate = PublishRateLimit {
314+
let rate = RateLimiter {
309315
rate: Duration::from_secs(1),
310316
burst: 10,
311317
};
@@ -328,7 +334,7 @@ mod tests {
328334
let conn = pg_connection();
329335
let now = now();
330336

331-
let rate = PublishRateLimit {
337+
let rate = RateLimiter {
332338
rate: Duration::from_secs(1),
333339
burst: 10,
334340
};
@@ -355,7 +361,7 @@ mod tests {
355361
let conn = pg_connection();
356362
let now = now();
357363

358-
let rate = PublishRateLimit {
364+
let rate = RateLimiter {
359365
rate: Duration::from_secs(1),
360366
burst: 10,
361367
};

0 commit comments

Comments
 (0)