Skip to content

Commit 1f1462d

Browse files
committed
wip
1 parent 59a4ca9 commit 1f1462d

20 files changed

+761
-35
lines changed

RustConfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION=1.32.0
1+
VERSION=1.33.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE publish_limit_buckets;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE publish_limit_buckets(
2+
user_id INTEGER PRIMARY KEY NOT NULL REFERENCES users,
3+
tokens INTEGER NOT NULL,
4+
last_refill TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
5+
);

src/bin/update-downloads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod test {
107107
name: "foo",
108108
..Default::default()
109109
}
110-
.create_or_update(conn, None, user_id)
110+
.create_or_update(conn, None, user_id, None)
111111
.unwrap();
112112
let version = NewVersion::new(
113113
krate.id,

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::publish_rate_limit::PublishRateLimit;
12
use crate::{env, uploaders::Uploader, Env, Replica};
23
use std::path::PathBuf;
34
use url::Url;
@@ -16,6 +17,7 @@ pub struct Config {
1617
pub max_unpack_size: u64,
1718
pub mirror: Replica,
1819
pub api_protocol: String,
20+
pub publish_rate_limit: PublishRateLimit,
1921
}
2022

2123
impl Default for Config {
@@ -132,6 +134,7 @@ impl Default for Config {
132134
max_unpack_size: 512 * 1024 * 1024, // 512 MB max when decompressed
133135
mirror,
134136
api_protocol,
137+
publish_rate_limit: Default::default(),
135138
}
136139
}
137140
}

src/controllers/krate/publish.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
8989
};
9090

9191
let license_file = new_crate.license_file.as_ref().map(|s| &**s);
92-
let krate = persist.create_or_update(&conn, license_file, user.id)?;
92+
let krate = persist.create_or_update(
93+
&conn,
94+
license_file,
95+
user.id,
96+
Some(&app.config.publish_rate_limit),
97+
)?;
9398

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

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ pub mod email;
3737
pub mod git;
3838
pub mod github;
3939
pub mod middleware;
40+
mod publish_rate_limit;
4041
pub mod render;
4142
pub mod schema;
43+
mod test_util;
4244
pub mod uploaders;
4345
pub mod util;
4446

src/models/category.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,11 @@ impl<'a> NewCategory<'a> {
183183
#[cfg(test)]
184184
mod tests {
185185
use super::*;
186+
use crate::test_util::pg_connection_no_transaction;
186187
use diesel::connection::SimpleConnection;
187188

188189
fn pg_connection() -> PgConnection {
189-
let database_url =
190-
dotenv::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests");
191-
let conn = PgConnection::establish(&database_url).unwrap();
190+
let conn = pg_connection_no_transaction();
192191
// These tests deadlock if run concurrently
193192
conn.batch_execute("BEGIN; LOCK categories IN ACCESS EXCLUSIVE MODE")
194193
.unwrap();

src/models/krate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::models::{
1414
use crate::views::{EncodableCrate, EncodableCrateLinks};
1515

1616
use crate::models::helpers::with_count::*;
17+
use crate::publish_rate_limit::PublishRateLimit;
1718
use crate::schema::*;
1819

1920
/// Hosts in this list are known to not be hosting documentation,
@@ -111,6 +112,7 @@ impl<'a> NewCrate<'a> {
111112
conn: &PgConnection,
112113
license_file: Option<&'a str>,
113114
uploader: i32,
115+
rate_limit: Option<&PublishRateLimit>,
114116
) -> CargoResult<Crate> {
115117
use diesel::update;
116118

@@ -121,6 +123,9 @@ impl<'a> NewCrate<'a> {
121123
// To avoid race conditions, we try to insert
122124
// first so we know whether to add an owner
123125
if let Some(krate) = self.save_new_crate(conn, uploader)? {
126+
if let Some(rate_limit) = rate_limit {
127+
rate_limit.check_rate_limit(uploader, conn)?;
128+
}
124129
return Ok(krate);
125130
}
126131

src/models/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct User {
2121
pub gh_id: i32,
2222
}
2323

24-
#[derive(Insertable, Debug)]
24+
#[derive(Insertable, Debug, Default)]
2525
#[table_name = "users"]
2626
pub struct NewUser<'a> {
2727
pub gh_id: i32,

0 commit comments

Comments
 (0)