lock with timeout

clumsy, but I have node idea how to make it better
This commit is contained in:
Dietmar Maurer 2018-12-08 10:57:09 +01:00
parent 2989f6bf20
commit 022fb421b2
1 changed files with 28 additions and 6 deletions

View File

@ -98,7 +98,7 @@ impl ChunkStore {
let mut lockfile_path = base.clone(); let mut lockfile_path = base.clone();
lockfile_path.push(".lock"); lockfile_path.push(".lock");
let lockfile = match OpenOptions::new() let lockfile = match OpenOptions::new()
.create(true) .create(true)
.append(true) .append(true)
.open(&lockfile_path) { .open(&lockfile_path) {
@ -109,12 +109,34 @@ impl ChunkStore {
let fd = lockfile.as_raw_fd(); let fd = lockfile.as_raw_fd();
// fixme: lock with timeout let now = std::time::SystemTime::now();
flock(fd, FlockArg::LockExclusive)?; let timeout = 10;
let mut print_msg = true;
loop {
match flock(fd, FlockArg::LockExclusiveNonblock) {
Ok(_) => break,
Err(_) => {
if print_msg {
print_msg = false;
eprintln!("trying to aquire lock...");
}
}
}
println!("Got LOCK {:?}", fd); match now.elapsed() {
Ok(elapsed) => {
//std::thread::sleep_ms(30000); if elapsed.as_secs() >= timeout {
bail!("unable to aquire chunk store lock {:?} - got timeout",
lockfile_path);
}
}
Err(err) => {
bail!("unable to aquire chunk store lock {:?} - clock problems - {}",
lockfile_path, err);
}
}
std::thread::sleep_ms(100);
}
Ok(ChunkStore { Ok(ChunkStore {
base, base,