Skip to content

Run git gc periodically on the crates.io index #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct Config {
pub(crate) max_file_size_html: usize,
// The most memory that can be used to parse an HTML file
pub(crate) max_parse_memory: usize,
// Time between 'git gc --auto' calls in seconds
pub(crate) registry_gc_interval: u64,
}

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

Expand Down
4 changes: 4 additions & 0 deletions src/docbuilder/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ impl DocBuilder {

Ok(processed)
}

pub fn run_git_gc(&self) {
self.index.run_git_gc();
}
}
21 changes: 20 additions & 1 deletion src/index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
process::Command,
};

use url::Url;

Expand Down Expand Up @@ -71,6 +74,22 @@ impl Index {
pub fn api(&self) -> &Api {
&self.api
}

pub fn run_git_gc(&self) {
let gc = Command::new("git")
.arg("-C")
.arg(&self.path)
.args(&["gc", "--auto"])
.output();

if let Err(err) = gc {
log::error!(
"failed to run `git gc --auto`\npath: {:#?}\nerror: {:#?}",
&self.path,
err
);
}
}
}

impl Clone for Index {
Expand Down
10 changes: 9 additions & 1 deletion src/utils/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ use chrono::{Timelike, Utc};
use failure::Error;
use log::{debug, error, info};
use std::thread;
use std::time::Duration;
use std::time::{Duration, Instant};

fn start_registry_watcher(opts: DocBuilderOptions, context: &dyn Context) -> Result<(), Error> {
let pool = context.pool()?;
let build_queue = context.build_queue()?;
let config = context.config()?;

thread::Builder::new()
.name("registry index reader".to_string())
.spawn(move || {
// space this out to prevent it from clashing against the queue-builder thread on launch
thread::sleep(Duration::from_secs(30));

let mut last_gc = Instant::now();
loop {
let mut doc_builder =
DocBuilder::new(opts.clone(), pool.clone(), build_queue.clone());
Expand All @@ -34,6 +38,10 @@ fn start_registry_watcher(opts: DocBuilderOptions, context: &dyn Context) -> Res
}
}

if last_gc.elapsed().as_secs() >= config.registry_gc_interval {
doc_builder.run_git_gc();
last_gc = Instant::now();
}
thread::sleep(Duration::from_secs(60));
}
})?;
Expand Down