mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
lock: use exponential backoff
This commit is contained in:
parent
9138de6ff2
commit
4ce2aed17f
1 changed files with 20 additions and 12 deletions
|
@ -12,6 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
use backoff::{ExponentialBackoff, Operation};
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -26,19 +27,26 @@ impl FileLock {
|
||||||
let mut options = OpenOptions::new();
|
let mut options = OpenOptions::new();
|
||||||
options.create_new(true);
|
options.create_new(true);
|
||||||
options.write(true);
|
options.write(true);
|
||||||
let retry_delay = Duration::from_millis(10);
|
let mut try_write_lock_file = || match options.open(&path) {
|
||||||
loop {
|
Ok(file) => Ok(FileLock {
|
||||||
match options.open(&path) {
|
path: path.clone(),
|
||||||
Ok(file) => return FileLock { path, _file: file },
|
_file: file,
|
||||||
|
}),
|
||||||
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
|
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
|
||||||
std::thread::sleep(retry_delay);
|
Err(backoff::Error::Transient(err))
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(backoff::Error::Permanent(err)),
|
||||||
|
};
|
||||||
|
let mut backoff = ExponentialBackoff::default();
|
||||||
|
backoff.initial_interval = Duration::from_millis(1);
|
||||||
|
backoff.max_elapsed_time = Some(Duration::from_secs(10));
|
||||||
|
match try_write_lock_file.retry(&mut backoff) {
|
||||||
Err(err) => panic!(
|
Err(err) => panic!(
|
||||||
"failed to create lock file {}: {}",
|
"failed to create lock file {}: {}",
|
||||||
path.to_string_lossy(),
|
path.to_string_lossy(),
|
||||||
err
|
err
|
||||||
),
|
),
|
||||||
}
|
Ok(file_lock) => file_lock,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue