-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use more robust cache locking (#150)
The lock created by `gix-lock` is not cleared when the process is killed. This, however, seems to happen quite often when protofetch is used in a cargo build script and a project is opened in VSCode with rust-analyzer. The lock implementation in this PR is resilient to such issues.
- Loading branch information
Showing
6 changed files
with
63 additions
and
213 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::{ | ||
fs::File, | ||
path::Path, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use fs4::fs_std::FileExt; | ||
use log::debug; | ||
use thiserror::Error; | ||
|
||
pub struct FileLock { | ||
_file: File, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
#[error(transparent)] | ||
pub struct Error(#[from] std::io::Error); | ||
|
||
impl FileLock { | ||
pub fn new(path: &Path) -> Result<Self, Error> { | ||
let file = File::create(path)?; | ||
let start = Instant::now(); | ||
loop { | ||
match file.try_lock_exclusive() { | ||
Ok(_) => { | ||
return Ok(Self { _file: file }); | ||
} | ||
Err(error) | ||
if error.raw_os_error() == fs4::lock_contended_error().raw_os_error() | ||
&& start.elapsed().as_secs() < 300 => | ||
{ | ||
debug!("Failed to acquire a lock on {}, retrying", path.display()); | ||
std::thread::sleep(Duration::from_secs(1)); | ||
} | ||
Err(error) => return Err(error.into()), | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.