Skip to content

Commit e5e756e

Browse files
authored
Merge pull request #6872 from ferrous-systems/pa-rate-limits-part1
First batch of rate limiter changes
2 parents 9499555 + f165456 commit e5e756e

File tree

13 files changed

+142
-93
lines changed

13 files changed

+142
-93
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/config/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::{anyhow, Context};
22
use ipnetwork::IpNetwork;
33
use oauth2::{ClientId, ClientSecret};
44

5-
use crate::publish_rate_limit::PublishRateLimit;
5+
use crate::rate_limiter::RateLimiter;
66
use crate::{env, env_optional, Env};
77

88
use super::base::Base;
@@ -30,7 +30,7 @@ pub struct Server {
3030
pub gh_client_secret: ClientSecret,
3131
pub max_upload_size: u64,
3232
pub max_unpack_size: u64,
33-
pub publish_rate_limit: PublishRateLimit,
33+
pub rate_limiter: RateLimiter,
3434
pub new_version_rate_limit: Option<u32>,
3535
pub blocked_traffic: Vec<(String, Vec<String>)>,
3636
pub max_allowed_page_offset: u32,
@@ -153,7 +153,7 @@ impl Default for Server {
153153
gh_client_secret: ClientSecret::new(env("GH_CLIENT_SECRET")),
154154
max_upload_size: 10 * 1024 * 1024, // 10 MB default file upload size limit
155155
max_unpack_size: 512 * 1024 * 1024, // 512 MB max when decompressed
156-
publish_rate_limit: Default::default(),
156+
rate_limiter: Default::default(),
157157
new_version_rate_limit: env_optional("MAX_NEW_VERSIONS_DAILY"),
158158
blocked_traffic: blocked_traffic(),
159159
max_allowed_page_offset: env_optional("WEB_MAX_ALLOWED_PAGE_OFFSET").unwrap_or(200),

src/controllers/krate/publish.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
137137
};
138138

139139
let license_file = new_crate.license_file.as_deref();
140-
let krate =
141-
persist.create_or_update(conn, user.id, Some(&app.config.publish_rate_limit))?;
140+
let krate = persist.create_or_update(conn, user.id, Some(&app.config.rate_limiter))?;
142141

143142
let owners = krate.owners(conn)?;
144143
if user.rights(&app, &owners)? < Rights::Publish {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ pub mod github;
4646
pub mod headers;
4747
pub mod metrics;
4848
pub mod middleware;
49-
mod publish_rate_limit;
49+
mod rate_limiter;
5050
pub mod schema;
51+
#[macro_use]
5152
pub mod sql;
5253
pub mod ssh;
5354
pub mod swirl;

src/models/action.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
use chrono::NaiveDateTime;
2-
use diesel::prelude::*;
3-
use diesel::{
4-
deserialize::{self, FromSql},
5-
pg::Pg,
6-
serialize::{self, Output, ToSql},
7-
sql_types::Integer,
8-
};
9-
101
use crate::models::{ApiToken, User, Version};
112
use crate::schema::*;
3+
use chrono::NaiveDateTime;
4+
use diesel::prelude::*;
125

13-
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromSqlRow, AsExpression)]
14-
#[repr(i32)]
15-
#[diesel(sql_type = Integer)]
16-
pub enum VersionAction {
17-
Publish = 0,
18-
Yank = 1,
19-
Unyank = 2,
6+
pg_enum! {
7+
pub enum VersionAction {
8+
Publish = 0,
9+
Yank = 1,
10+
Unyank = 2,
11+
}
2012
}
2113

2214
impl From<VersionAction> for &'static str {
@@ -37,23 +29,6 @@ impl From<VersionAction> for String {
3729
}
3830
}
3931

40-
impl FromSql<Integer, Pg> for VersionAction {
41-
fn from_sql(bytes: diesel::pg::PgValue<'_>) -> deserialize::Result<Self> {
42-
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? {
43-
0 => Ok(VersionAction::Publish),
44-
1 => Ok(VersionAction::Yank),
45-
2 => Ok(VersionAction::Unyank),
46-
n => Err(format!("unknown version action: {n}").into()),
47-
}
48-
}
49-
}
50-
51-
impl ToSql<Integer, Pg> for VersionAction {
52-
fn to_sql(&self, out: &mut Output<'_, '_, Pg>) -> serialize::Result {
53-
ToSql::<Integer, Pg>::to_sql(&(*self as i32), &mut out.reborrow())
54-
}
55-
}
56-
5732
#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)]
5833
#[diesel(
5934
table_name = version_owner_actions,

src/models/dependency.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use diesel::deserialize::{self, FromSql};
2-
use diesel::pg::Pg;
31
use diesel::sql_types::{Integer, Text};
42

53
use crate::models::{Crate, Version};
@@ -36,14 +34,12 @@ pub struct ReverseDependency {
3634
pub name: String,
3735
}
3836

39-
#[derive(Copy, Clone, Serialize, Deserialize, Debug, FromSqlRow)]
40-
#[serde(rename_all = "lowercase")]
41-
#[repr(u32)]
42-
pub enum DependencyKind {
43-
Normal = 0,
44-
Build = 1,
45-
Dev = 2,
46-
// if you add a kind here, be sure to update `from_row` below.
37+
pg_enum! {
38+
pub enum DependencyKind {
39+
Normal = 0,
40+
Build = 1,
41+
Dev = 2,
42+
}
4743
}
4844

4945
impl From<IndexDependencyKind> for DependencyKind {
@@ -65,14 +61,3 @@ impl From<DependencyKind> for IndexDependencyKind {
6561
}
6662
}
6763
}
68-
69-
impl FromSql<Integer, Pg> for DependencyKind {
70-
fn from_sql(bytes: diesel::pg::PgValue<'_>) -> deserialize::Result<Self> {
71-
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? {
72-
0 => Ok(DependencyKind::Normal),
73-
1 => Ok(DependencyKind::Build),
74-
2 => Ok(DependencyKind::Dev),
75-
n => Err(format!("unknown kind: {n}").into()),
76-
}
77-
}
78-
}

src/models/krate.rs

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

1919
use crate::models::helpers::with_count::*;
20-
use crate::publish_rate_limit::PublishRateLimit;
20+
use crate::rate_limiter::RateLimiter;
2121
use crate::schema::*;
2222
use crate::sql::canon_crate_name;
2323

@@ -107,7 +107,7 @@ impl<'a> NewCrate<'a> {
107107
self,
108108
conn: &mut PgConnection,
109109
uploader: i32,
110-
rate_limit: Option<&PublishRateLimit>,
110+
rate_limit: Option<&RateLimiter>,
111111
) -> AppResult<Crate> {
112112
use diesel::update;
113113

0 commit comments

Comments
 (0)