Skip to content

Commit 3277ddd

Browse files
Nemo157syphar
authored andcommitted
Add a config option to set the default build memory limit
1 parent 08d589b commit 3277ddd

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub struct Config {
101101
pub(crate) docker_image: Option<String>,
102102
pub(crate) toolchain: String,
103103
pub(crate) build_cpu_limit: Option<u32>,
104+
pub(crate) build_default_memory_limit: Option<usize>,
104105
pub(crate) include_default_targets: bool,
105106
pub(crate) disable_memory_limit: bool,
106107
}
@@ -203,6 +204,7 @@ impl Config {
203204
.or(maybe_env("DOCSRS_DOCKER_IMAGE")?),
204205
toolchain: env("DOCSRS_TOOLCHAIN", "nightly".to_string())?,
205206
build_cpu_limit: maybe_env("DOCSRS_BUILD_CPU_LIMIT")?,
207+
build_default_memory_limit: maybe_env("DOCSRS_BUILD_DEFAULT_MEMORY_LIMIT")?,
206208
include_default_targets: env("DOCSRS_INCLUDE_DEFAULT_TARGETS", true)?,
207209
disable_memory_limit: env("DOCSRS_DISABLE_MEMORY_LIMIT", false)?,
208210
})

src/docbuilder/limits.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::db::Overrides;
2-
use crate::error::Result;
1+
use crate::{db::Overrides, error::Result, Config};
32
use postgres::Client;
43
use serde::Serialize;
54
use std::time::Duration;
65

6+
const GB: usize = 1024 * 1024 * 1024;
7+
78
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
89
pub(crate) struct Limits {
910
memory: usize,
@@ -13,21 +14,20 @@ pub(crate) struct Limits {
1314
max_log_size: usize,
1415
}
1516

16-
impl Default for Limits {
17-
fn default() -> Self {
17+
impl Limits {
18+
pub(crate) fn new(config: &Config) -> Self {
1819
Self {
19-
memory: 3 * 1024 * 1024 * 1024, // 3 GB
20+
// 3 GB default default
21+
memory: config.build_default_memory_limit.unwrap_or(3 * GB),
2022
timeout: Duration::from_secs(15 * 60), // 15 minutes
2123
targets: 10,
2224
networking: false,
2325
max_log_size: 100 * 1024, // 100 KB
2426
}
2527
}
26-
}
2728

28-
impl Limits {
29-
pub(crate) fn for_crate(conn: &mut Client, name: &str) -> Result<Self> {
30-
let default = Self::default();
29+
pub(crate) fn for_crate(config: &Config, conn: &mut Client, name: &str) -> Result<Self> {
30+
let default = Self::new(config);
3131
let overrides = Overrides::for_crate(conn, name)?.unwrap_or_default();
3232
Ok(Self {
3333
memory: overrides.memory.unwrap_or(default.memory),
@@ -74,8 +74,8 @@ mod test {
7474

7575
let krate = "hexponent";
7676
// limits work if no crate has limits set
77-
let hexponent = Limits::for_crate(&mut db.conn(), krate)?;
78-
assert_eq!(hexponent, Limits::default());
77+
let hexponent = Limits::for_crate(&env.config(), &mut db.conn(), krate)?;
78+
assert_eq!(hexponent, Limits::new(&env.config()));
7979

8080
Overrides::save(
8181
&mut db.conn(),
@@ -86,12 +86,12 @@ mod test {
8686
},
8787
)?;
8888
// limits work if crate has limits set
89-
let hexponent = Limits::for_crate(&mut db.conn(), krate)?;
89+
let hexponent = Limits::for_crate(&env.config(), &mut db.conn(), krate)?;
9090
assert_eq!(
9191
hexponent,
9292
Limits {
9393
targets: 15,
94-
..Limits::default()
94+
..Limits::new(&env.config())
9595
}
9696
);
9797

@@ -101,7 +101,7 @@ mod test {
101101
memory: 100_000,
102102
timeout: Duration::from_secs(300),
103103
targets: 1,
104-
..Limits::default()
104+
..Limits::new(&env.config())
105105
};
106106
Overrides::save(
107107
&mut db.conn(),
@@ -112,7 +112,10 @@ mod test {
112112
timeout: Some(limits.timeout),
113113
},
114114
)?;
115-
assert_eq!(limits, Limits::for_crate(&mut db.conn(), krate)?);
115+
assert_eq!(
116+
limits,
117+
Limits::for_crate(&env.config(), &mut db.conn(), krate)?
118+
);
116119
Ok(())
117120
});
118121
}
@@ -130,10 +133,26 @@ mod test {
130133
..Overrides::default()
131134
},
132135
)?;
133-
let limits = Limits::for_crate(&mut db.conn(), krate)?;
136+
let limits = Limits::for_crate(&env.config(), &mut db.conn(), krate)?;
134137
assert_eq!(limits.targets, 1);
135138

136139
Ok(())
137140
});
138141
}
142+
143+
#[test]
144+
fn config_default_memory_limit() {
145+
wrapper(|env| {
146+
env.override_config(|config| {
147+
config.build_default_memory_limit = Some(6 * GB);
148+
});
149+
150+
let db = env.db();
151+
152+
let limits = Limits::for_crate(&env.config(), &mut db.conn(), "krate")?;
153+
assert_eq!(limits.memory, 6 * GB);
154+
155+
Ok(())
156+
});
157+
}
139158
}

src/docbuilder/rustwide_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl RustwideBuilder {
228228
info!("building a dummy crate to get essential files");
229229

230230
let mut conn = self.db.get()?;
231-
let limits = Limits::for_crate(&mut conn, DUMMY_CRATE_NAME)?;
231+
let limits = Limits::for_crate(&self.config, &mut conn, DUMMY_CRATE_NAME)?;
232232

233233
let mut build_dir = self
234234
.workspace
@@ -342,7 +342,7 @@ impl RustwideBuilder {
342342
return Ok(false);
343343
}
344344

345-
let limits = Limits::for_crate(&mut conn, name)?;
345+
let limits = Limits::for_crate(&self.config, &mut conn, name)?;
346346
#[cfg(target_os = "linux")]
347347
if !self.config.disable_memory_limit {
348348
use anyhow::Context;

src/web/builds.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
impl_axum_webpage,
66
utils::spawn_blocking,
77
web::{error::AxumResult, match_version_axum, MetaData},
8+
Config,
89
};
910
use anyhow::Result;
1011
use axum::{
@@ -15,6 +16,7 @@ use axum::{
1516
};
1617
use chrono::{DateTime, Utc};
1718
use serde::Serialize;
19+
use std::sync::Arc;
1820

1921
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2022
pub(crate) struct Build {
@@ -40,6 +42,7 @@ impl_axum_webpage! {
4042
pub(crate) async fn build_list_handler(
4143
Path((name, req_version)): Path<(String, String)>,
4244
Extension(pool): Extension<Pool>,
45+
Extension(config): Extension<Arc<Config>>,
4346
) -> AxumResult<impl IntoResponse> {
4447
let (version, version_or_latest) = match match_version_axum(&pool, &name, Some(&req_version))
4548
.await?
@@ -62,7 +65,7 @@ pub(crate) async fn build_list_handler(
6265
move || {
6366
let mut conn = pool.get()?;
6467
Ok((
65-
Limits::for_crate(&mut conn, &name)?,
68+
Limits::for_crate(&config, &mut conn, &name)?,
6669
get_builds(&mut conn, &name, &version)?,
6770
MetaData::from_crate(&mut conn, &name, &version, &version_or_latest)?,
6871
))

src/web/sitemap.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
error::{AxumNope, AxumResult},
88
AxumErrorPage,
99
},
10+
Config,
1011
};
1112
use axum::{
1213
extract::{Extension, Path},
@@ -15,6 +16,7 @@ use axum::{
1516
};
1617
use chrono::{DateTime, TimeZone, Utc};
1718
use serde::Serialize;
19+
use std::sync::Arc;
1820

1921
/// sitemap index
2022
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
@@ -70,9 +72,9 @@ pub(crate) async fn sitemap_handler(
7072
MAX(releases.release_time) as release_time
7173
FROM crates
7274
INNER JOIN releases ON releases.crate_id = crates.id
73-
WHERE
74-
rustdoc_status = true AND
75-
crates.name ILIKE $1
75+
WHERE
76+
rustdoc_status = true AND
77+
crates.name ILIKE $1
7678
GROUP BY crates.name, releases.target_name
7779
",
7880
&[&format!("{letter}%")],
@@ -112,6 +114,7 @@ impl_axum_webpage!(AboutBuilds = "core/about/builds.html");
112114

113115
pub(crate) async fn about_builds_handler(
114116
Extension(pool): Extension<Pool>,
117+
Extension(config): Extension<Arc<Config>>,
115118
) -> AxumResult<impl IntoResponse> {
116119
let rustc_version = spawn_blocking(move || {
117120
let mut conn = pool.get()?;
@@ -121,7 +124,7 @@ pub(crate) async fn about_builds_handler(
121124

122125
Ok(AboutBuilds {
123126
rustc_version,
124-
limits: Limits::default(),
127+
limits: Limits::new(&config),
125128
active_tab: "builds",
126129
})
127130
}

0 commit comments

Comments
 (0)