From 022fb421b24a49d586c68acaac7453ea6dff4ab3 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 8 Dec 2018 10:57:09 +0100 Subject: [PATCH] lock with timeout clumsy, but I have node idea how to make it better --- src/backup/chunk_store.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/backup/chunk_store.rs b/src/backup/chunk_store.rs index 632d6c22..cd334b71 100644 --- a/src/backup/chunk_store.rs +++ b/src/backup/chunk_store.rs @@ -98,7 +98,7 @@ impl ChunkStore { let mut lockfile_path = base.clone(); lockfile_path.push(".lock"); - let lockfile = match OpenOptions::new() + let lockfile = match OpenOptions::new() .create(true) .append(true) .open(&lockfile_path) { @@ -109,12 +109,34 @@ impl ChunkStore { let fd = lockfile.as_raw_fd(); - // fixme: lock with timeout - flock(fd, FlockArg::LockExclusive)?; + let now = std::time::SystemTime::now(); + 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); - - //std::thread::sleep_ms(30000); + match now.elapsed() { + Ok(elapsed) => { + 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 { base,