Skip to content

Commit

Permalink
Cache lock improvements (#140)
Browse files Browse the repository at this point in the history
1. Exponential backoff makes protofetch sometimes take too long to run.
2. It makes sense to initialize signal handlers to make sure the locks
are removed when protofetch is killed.
  • Loading branch information
rtimush authored Jun 27, 2024
1 parent ca91ea8 commit e995316
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
53 changes: 45 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ clap = { version = "4.4.7", features = ["derive"] }
config = { version = "0.13.3", default-features = false, features = ["toml"] }
env_logger = { version = "0.10.0", default-features = false, features = ["auto-color"] }
git2 = "0.18.1"
gix-lock = "13.1.1"
gix-lock = { version = "14.0.0" }
gix-tempfile = { version = "14.0.0", features = ["signals"] }
home = "0.5.5"
log = "0.4.20"
regex-lite = "0.1.5"
Expand Down
29 changes: 17 additions & 12 deletions src/git/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
path::{Path, PathBuf},
time::Duration,
time::{Duration, Instant},
};

use git2::{
Expand Down Expand Up @@ -56,6 +56,7 @@ impl ProtofetchGitCache {
std::fs::create_dir_all(&location)?;
}

gix_lock::tempfile::signal::setup(Default::default());
let lock = Self::acquire_lock(&location)?;

let worktrees = location.join(WORKTREES_DIR);
Expand Down Expand Up @@ -98,17 +99,21 @@ impl ProtofetchGitCache {
"Acquiring a lock on the cache location: {}",
location.display()
);
match Marker::acquire_to_hold_resource(location, Fail::Immediately, None) {
Ok(lock) => Ok(lock),
Err(_) => {
info!("Failed to acquire a lock on the cache location, retrying");
let lock = Marker::acquire_to_hold_resource(
location,
Fail::AfterDurationWithBackoff(Duration::from_secs(300)),
None,
)?;
info!("Acquired a lock on the cache location");
Ok(lock)
let start = Instant::now();
loop {
match Marker::acquire_to_hold_resource(location, Fail::Immediately, None) {
Ok(lock) => {
info!("Acquired a lock on the cache location");
return Ok(lock);
}
Err(error) => {
if start.elapsed() < Duration::from_secs(300) {
debug!("Failed to acquire a lock on the cache location, retrying");
std::thread::sleep(Duration::from_secs(1));
} else {
return Err(error.into());
}
}
}
}
}
Expand Down

0 comments on commit e995316

Please sign in to comment.