Skip to content

Commit 5383f51

Browse files
committed
add the action column to rate limit tables
1 parent d0b04c8 commit 5383f51

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DELETE FROM publish_limit_buckets WHERE action != 0;
2+
ALTER TABLE publish_limit_buckets DROP CONSTRAINT publish_limit_buckets_pkey;
3+
ALTER TABLE publish_limit_buckets ADD CONSTRAINT publish_limit_buckets_pkey PRIMARY KEY (user_id);
4+
ALTER TABLE publish_limit_buckets DROP COLUMN action;
5+
6+
DELETE FROM publish_rate_overrides WHERE action != 0;
7+
ALTER TABLE publish_rate_overrides DROP CONSTRAINT publish_rate_overrides_pkey;
8+
ALTER TABLE publish_rate_overrides ADD CONSTRAINT publish_rate_overrides_pkey PRIMARY KEY (user_id);
9+
ALTER TABLE publish_rate_overrides DROP COLUMN action;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ALTER TABLE publish_limit_buckets ADD COLUMN action INTEGER NOT NULL DEFAULT 0;
2+
ALTER TABLE publish_limit_buckets DROP CONSTRAINT publish_limit_buckets_pkey;
3+
ALTER TABLE publish_limit_buckets ADD CONSTRAINT publish_limit_buckets_pkey PRIMARY KEY (user_id, action);
4+
5+
ALTER TABLE publish_rate_overrides ADD COLUMN action INTEGER NOT NULL DEFAULT 0;
6+
ALTER TABLE publish_rate_overrides DROP CONSTRAINT publish_rate_overrides_pkey;
7+
ALTER TABLE publish_rate_overrides ADD CONSTRAINT publish_rate_overrides_pkey PRIMARY KEY (user_id, action);

src/publish_rate_limit.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ use crate::schema::{publish_limit_buckets, publish_rate_overrides};
88
use crate::sql::{date_part, floor, greatest, interval_part, least};
99
use crate::util::errors::{AppResult, TooManyRequests};
1010

11+
crate::pg_enum! {
12+
pub enum LimitedAction {
13+
PublishNew = 0,
14+
}
15+
}
16+
1117
#[derive(Debug, Clone, Copy)]
1218
pub struct PublishRateLimit {
1319
pub rate: Duration,
@@ -40,6 +46,7 @@ struct Bucket {
4046
user_id: i32,
4147
tokens: i32,
4248
last_refill: NaiveDateTime,
49+
action: LimitedAction,
4350
}
4451

4552
impl PublishRateLimit {
@@ -71,7 +78,7 @@ impl PublishRateLimit {
7178
use self::publish_limit_buckets::dsl::*;
7279

7380
let burst: i32 = publish_rate_overrides::table
74-
.find(uploader)
81+
.find((uploader, LimitedAction::PublishNew))
7582
.filter(
7683
publish_rate_overrides::expires_at
7784
.is_null()
@@ -128,6 +135,7 @@ mod tests {
128135
user_id: bucket.user_id,
129136
tokens: 10,
130137
last_refill: now,
138+
action: LimitedAction::PublishNew,
131139
};
132140
assert_eq!(expected, bucket);
133141

@@ -140,6 +148,7 @@ mod tests {
140148
user_id: bucket.user_id,
141149
tokens: 20,
142150
last_refill: now,
151+
action: LimitedAction::PublishNew,
143152
};
144153
assert_eq!(expected, bucket);
145154
Ok(())
@@ -160,6 +169,7 @@ mod tests {
160169
user_id,
161170
tokens: 4,
162171
last_refill: now,
172+
action: LimitedAction::PublishNew,
163173
};
164174
assert_eq!(expected, bucket);
165175
Ok(())
@@ -181,6 +191,7 @@ mod tests {
181191
user_id,
182192
tokens: 6,
183193
last_refill: refill_time,
194+
action: LimitedAction::PublishNew,
184195
};
185196
assert_eq!(expected, bucket);
186197
Ok(())
@@ -206,6 +217,7 @@ mod tests {
206217
user_id,
207218
tokens: 7,
208219
last_refill: refill_time,
220+
action: LimitedAction::PublishNew,
209221
};
210222
assert_eq!(expected, bucket);
211223
Ok(())
@@ -227,6 +239,7 @@ mod tests {
227239
user_id,
228240
tokens: 6,
229241
last_refill: expected_refill_time,
242+
action: LimitedAction::PublishNew,
230243
};
231244
assert_eq!(expected, bucket);
232245
Ok(())
@@ -247,6 +260,7 @@ mod tests {
247260
user_id,
248261
tokens: 0,
249262
last_refill: now,
263+
action: LimitedAction::PublishNew,
250264
};
251265
assert_eq!(expected, bucket);
252266

@@ -271,6 +285,7 @@ mod tests {
271285
user_id,
272286
tokens: 1,
273287
last_refill: refill_time,
288+
action: LimitedAction::PublishNew,
274289
};
275290
assert_eq!(expected, bucket);
276291

@@ -293,6 +308,7 @@ mod tests {
293308
user_id,
294309
tokens: 10,
295310
last_refill: refill_time,
311+
action: LimitedAction::PublishNew,
296312
};
297313
assert_eq!(expected, bucket);
298314

@@ -391,6 +407,7 @@ mod tests {
391407
user_id: new_user(conn, "new_user")?,
392408
tokens,
393409
last_refill: now,
410+
action: LimitedAction::PublishNew,
394411
})
395412
.get_result(conn)
396413
}

src/schema.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ diesel::table! {
605605
/// Representation of the `publish_limit_buckets` table.
606606
///
607607
/// (Automatically generated by Diesel.)
608-
publish_limit_buckets (user_id) {
608+
publish_limit_buckets (user_id, action) {
609609
/// The `user_id` column of the `publish_limit_buckets` table.
610610
///
611611
/// Its SQL type is `Int4`.
@@ -624,14 +624,20 @@ diesel::table! {
624624
///
625625
/// (Automatically generated by Diesel.)
626626
last_refill -> Timestamp,
627+
/// The `action` column of the `publish_limit_buckets` table.
628+
///
629+
/// Its SQL type is `Int4`.
630+
///
631+
/// (Automatically generated by Diesel.)
632+
action -> Int4,
627633
}
628634
}
629635

630636
diesel::table! {
631637
/// Representation of the `publish_rate_overrides` table.
632638
///
633639
/// (Automatically generated by Diesel.)
634-
publish_rate_overrides (user_id) {
640+
publish_rate_overrides (user_id, action) {
635641
/// The `user_id` column of the `publish_rate_overrides` table.
636642
///
637643
/// Its SQL type is `Int4`.
@@ -650,6 +656,12 @@ diesel::table! {
650656
///
651657
/// (Automatically generated by Diesel.)
652658
expires_at -> Nullable<Timestamp>,
659+
/// The `action` column of the `publish_rate_overrides` table.
660+
///
661+
/// Its SQL type is `Int4`.
662+
///
663+
/// (Automatically generated by Diesel.)
664+
action -> Int4,
653665
}
654666
}
655667

src/worker/dump_db/dump-db.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ total_downloads = "public"
143143

144144
[publish_limit_buckets.columns]
145145
user_id = "private"
146+
action = "private"
146147
tokens = "private"
147148
last_refill = "private"
148149

149150
[publish_rate_overrides.columns]
150151
user_id = "private"
152+
action = "private"
151153
burst = "private"
152154
expires_at = "private"
153155

0 commit comments

Comments
 (0)