Skip to content

Commit 3ed00ad

Browse files
authored
Run git gc periodically on the crates.io index (#975)
1 parent 22d7f92 commit 3ed00ad

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct Config {
3030
pub(crate) max_file_size_html: usize,
3131
// The most memory that can be used to parse an HTML file
3232
pub(crate) max_parse_memory: usize,
33+
// Time between 'git gc --auto' calls in seconds
34+
pub(crate) registry_gc_interval: u64,
3335
}
3436

3537
impl Config {
@@ -61,6 +63,7 @@ impl Config {
6163
// LOL HTML only uses as much memory as the size of the start tag!
6264
// https://github.com/rust-lang/docs.rs/pull/930#issuecomment-667729380
6365
max_parse_memory: env("DOCSRS_MAX_PARSE_MEMORY", 5 * 1024 * 1024)?,
66+
registry_gc_interval: env("DOCSRS_REGISTRY_GC_INTERVAL", 60 * 60)?,
6467
})
6568
}
6669

src/docbuilder/queue.rs

+4
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,8 @@ impl DocBuilder {
8484

8585
Ok(processed)
8686
}
87+
88+
pub fn run_git_gc(&self) {
89+
self.index.run_git_gc();
90+
}
8791
}

src/index/mod.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::path::{Path, PathBuf};
1+
use std::{
2+
path::{Path, PathBuf},
3+
process::Command,
4+
};
25

36
use url::Url;
47

@@ -71,6 +74,22 @@ impl Index {
7174
pub fn api(&self) -> &Api {
7275
&self.api
7376
}
77+
78+
pub fn run_git_gc(&self) {
79+
let gc = Command::new("git")
80+
.arg("-C")
81+
.arg(&self.path)
82+
.args(&["gc", "--auto"])
83+
.output();
84+
85+
if let Err(err) = gc {
86+
log::error!(
87+
"failed to run `git gc --auto`\npath: {:#?}\nerror: {:#?}",
88+
&self.path,
89+
err
90+
);
91+
}
92+
}
7493
}
7594

7695
impl Clone for Index {

src/utils/daemon.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ use chrono::{Timelike, Utc};
1010
use failure::Error;
1111
use log::{debug, error, info};
1212
use std::thread;
13-
use std::time::Duration;
13+
use std::time::{Duration, Instant};
1414

1515
fn start_registry_watcher(opts: DocBuilderOptions, context: &dyn Context) -> Result<(), Error> {
1616
let pool = context.pool()?;
1717
let build_queue = context.build_queue()?;
18+
let config = context.config()?;
19+
1820
thread::Builder::new()
1921
.name("registry index reader".to_string())
2022
.spawn(move || {
2123
// space this out to prevent it from clashing against the queue-builder thread on launch
2224
thread::sleep(Duration::from_secs(30));
25+
26+
let mut last_gc = Instant::now();
2327
loop {
2428
let mut doc_builder =
2529
DocBuilder::new(opts.clone(), pool.clone(), build_queue.clone());
@@ -34,6 +38,10 @@ fn start_registry_watcher(opts: DocBuilderOptions, context: &dyn Context) -> Res
3438
}
3539
}
3640

41+
if last_gc.elapsed().as_secs() >= config.registry_gc_interval {
42+
doc_builder.run_git_gc();
43+
last_gc = Instant::now();
44+
}
3745
thread::sleep(Duration::from_secs(60));
3846
}
3947
})?;

0 commit comments

Comments
 (0)