Skip to content

Commit d737c4a

Browse files
committed
fix!: Rename Exponential backoff to Quadratic
The `gix_utils::backoff::Exponential` type actually implemented quadratic, not exponential, backoff. This renames it from `Exponential` to `Quadratic`. In exponential backoff, delays are a fixed base, often 2, raised to a power of a number that increases by one with each attempt. When the number that increases by one with each attempt is the base, raised to a fixed power, that is quadratic backoff. The intended behavior here was quadratic, as implemented. For example, in the tests, `EXPECTED_TILL_SECOND` lists the values 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, and so on, which are ascending squares. If they were an exponential sequence, then they would look like 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, and so on. Thus, it is only the named that needed to be changed: the implementation was already as intended.
1 parent 99c2706 commit d737c4a

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

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))

0 commit comments

Comments
 (0)