Skip to content

Commit ffb73b5

Browse files
authored
Merge pull request #1815 from EliahKagan/quadratic
Rename `Exponential` backoff to `Quadratic`
2 parents 99c2706 + 6990f76 commit ffb73b5

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

gix-lock/src/acquire.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub enum Fail {
1414
/// Fail after the first unsuccessful attempt of obtaining a lock.
1515
#[default]
1616
Immediately,
17-
/// Retry after failure with exponentially longer sleep times to block the current thread.
17+
/// Retry after failure with quadratically longer sleep times to block the current thread.
1818
/// Fail once the given duration is exceeded, similar to [Fail::Immediately]
1919
AfterDurationWithBackoff(Duration),
2020
}
@@ -176,7 +176,7 @@ fn lock_with_mode<T>(
176176
match mode {
177177
Fail::Immediately => try_lock(&lock_path, directory, cleanup),
178178
Fail::AfterDurationWithBackoff(time) => {
179-
for wait in backoff::Exponential::default_with_random().until_no_remaining(time) {
179+
for wait in backoff::Quadratic::default_with_random().until_no_remaining(time) {
180180
attempts += 1;
181181
match try_lock(&lock_path, directory, cleanup.clone()) {
182182
Ok(v) => return Ok((lock_path, v)),

gix-utils/src/backoff.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ fn randomize(backoff_ms: usize) -> usize {
99
}
1010
}
1111

12-
/// A utility to calculate steps for exponential backoff similar to how it's done in `git`.
13-
pub struct Exponential<Fn> {
12+
/// A utility to calculate steps for quadratic backoff similar to how it's done in `git`.
13+
pub struct Quadratic<Fn> {
1414
multiplier: usize,
1515
max_multiplier: usize,
1616
exponent: usize,
1717
transform: Fn,
1818
}
1919

20-
impl Default for Exponential<fn(usize) -> usize> {
20+
impl Default for Quadratic<fn(usize) -> usize> {
2121
fn default() -> Self {
22-
Exponential {
22+
Quadratic {
2323
multiplier: 1,
2424
max_multiplier: 1000,
2525
exponent: 1,
@@ -28,10 +28,10 @@ impl Default for Exponential<fn(usize) -> usize> {
2828
}
2929
}
3030

31-
impl Exponential<fn(usize) -> usize> {
32-
/// Create a new exponential backoff iterator that backs off in randomized, ever increasing steps.
31+
impl Quadratic<fn(usize) -> usize> {
32+
/// Create a new quadratic backoff iterator that backs off in randomized, ever increasing steps.
3333
pub fn default_with_random() -> Self {
34-
Exponential {
34+
Quadratic {
3535
multiplier: 1,
3636
max_multiplier: 1000,
3737
exponent: 1,
@@ -40,7 +40,7 @@ impl Exponential<fn(usize) -> usize> {
4040
}
4141
}
4242

43-
impl<Transform> Exponential<Transform>
43+
impl<Transform> Quadratic<Transform>
4444
where
4545
Transform: Fn(usize) -> usize,
4646
{
@@ -62,7 +62,7 @@ where
6262
}
6363
}
6464

65-
impl<Transform> Iterator for Exponential<Transform>
65+
impl<Transform> Iterator for Quadratic<Transform>
6666
where
6767
Transform: Fn(usize) -> usize,
6868
{

gix-utils/tests/backoff/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::time::Duration;
22

3-
use gix_utils::backoff::Exponential;
3+
use gix_utils::backoff::Quadratic;
44

55
const EXPECTED_TILL_SECOND: &[usize] = &[
66
1usize, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576,
77
625, 676, 729, 784, 841, 900, 961, 1000, 1000,
88
];
99

1010
#[test]
11-
fn random_exponential_produces_values_in_the_correct_range() {
11+
fn random_quadratic_produces_values_in_the_correct_range() {
1212
let mut num_identities = 0;
13-
for (actual, expected) in Exponential::default_with_random().zip(EXPECTED_TILL_SECOND) {
13+
for (actual, expected) in Quadratic::default_with_random().zip(EXPECTED_TILL_SECOND) {
1414
let actual: usize = actual.as_millis().try_into().unwrap();
1515
if actual == *expected {
1616
num_identities += 1;
@@ -33,9 +33,9 @@ fn random_exponential_produces_values_in_the_correct_range() {
3333
#[test]
3434
fn how_many_iterations_for_a_second_of_waittime() {
3535
let max = Duration::from_millis(1000);
36-
assert_eq!(Exponential::default().until_no_remaining(max).count(), 14);
36+
assert_eq!(Quadratic::default().until_no_remaining(max).count(), 14);
3737
assert_eq!(
38-
Exponential::default()
38+
Quadratic::default()
3939
.until_no_remaining(max)
4040
.reduce(|acc, n| acc + n)
4141
.unwrap(),
@@ -47,7 +47,7 @@ fn how_many_iterations_for_a_second_of_waittime() {
4747
#[test]
4848
fn output_with_default_settings() {
4949
assert_eq!(
50-
Exponential::default().take(33).collect::<Vec<_>>(),
50+
Quadratic::default().take(33).collect::<Vec<_>>(),
5151
EXPECTED_TILL_SECOND
5252
.iter()
5353
.map(|n| Duration::from_millis(*n as u64))

tests/tools/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ pub fn spawn_git_daemon(working_dir: impl AsRef<Path>) -> std::io::Result<GitDae
219219
.spawn()?;
220220

221221
let server_addr = addr_at(free_port);
222+
// TODO(deps): Upgrading dependencies will require changing `Exponential` to `Quadratic`.
222223
for time in gix_lock::backoff::Exponential::default_with_random() {
223224
std::thread::sleep(time);
224225
if std::net::TcpStream::connect(server_addr).is_ok() {
@@ -652,8 +653,8 @@ fn configure_command<'a, I: IntoIterator<Item = S>, S: AsRef<OsStr>>(
652653
}
653654

654655
fn bash_program() -> &'static Path {
655-
// TODO: use `gix_path::env::login_shell()` when available.
656656
if cfg!(windows) {
657+
// TODO(deps): Once `gix_path::env::shell()` is available, maybe do `shell().parent()?.join("bash.exe")`
657658
static GIT_BASH: Lazy<Option<PathBuf>> = Lazy::new(|| {
658659
GIT_CORE_DIR
659660
.parent()?

0 commit comments

Comments
 (0)