Fix exponential backoff values

This commit is contained in:
Kirill Bulatov
2022-08-10 23:47:14 +03:00
committed by Kirill Bulatov
parent 4227cfc96e
commit 7a36d06cc2
2 changed files with 47 additions and 6 deletions

View File

@@ -232,7 +232,7 @@ pub(crate) fn create_timeline(
return Ok(None);
}
let _new_timeline = match ancestor_timeline_id {
match ancestor_timeline_id {
Some(ancestor_timeline_id) => {
let ancestor_timeline = repo
.get_timeline_load(ancestor_timeline_id)

View File

@@ -233,13 +233,23 @@ async fn subscribe_for_timeline_updates(
const DEFAULT_BASE_BACKOFF_SECONDS: f64 = 0.1;
const DEFAULT_MAX_BACKOFF_SECONDS: f64 = 3.0;
async fn exponential_backoff(n: u32, base: f64, max_seconds: f64) {
fn exponential_backoff_duration_seconds(n: u32, base_increment: f64, max_seconds: f64) -> f64 {
if n == 0 {
return;
0.0
} else {
(1.0 + base_increment).powf(f64::from(n)).min(max_seconds)
}
}
async fn exponential_backoff(n: u32, base_increment: f64, max_seconds: f64) {
let backoff_duration_seconds =
exponential_backoff_duration_seconds(n, base_increment, max_seconds);
if backoff_duration_seconds > 0.0 {
info!(
"Backoff: waiting {backoff_duration_seconds} seconds before proceeding with the task",
);
tokio::time::sleep(Duration::from_secs_f64(backoff_duration_seconds)).await;
}
let seconds_to_wait = base.powf(f64::from(n) - 1.0).min(max_seconds);
info!("Backoff: waiting {seconds_to_wait} seconds before proceeding with the task");
tokio::time::sleep(Duration::from_secs_f64(seconds_to_wait)).await;
}
/// All data that's needed to run endless broker loop and keep the WAL streaming connection alive, if possible.
@@ -1217,3 +1227,34 @@ mod tests {
}
}
}
#[cfg(test)]
mod backoff_defaults_tests {
use super::*;
#[test]
fn backoff_defaults_produce_growing_backoff_sequence() {
let mut current_backoff_value = None;
for i in 0..10_000 {
let new_backoff_value = exponential_backoff_duration_seconds(
i,
DEFAULT_BASE_BACKOFF_SECONDS,
DEFAULT_MAX_BACKOFF_SECONDS,
);
if let Some(old_backoff_value) = current_backoff_value.replace(new_backoff_value) {
assert!(
old_backoff_value <= new_backoff_value,
"{i}th backoff value {new_backoff_value} is smaller than the previous one {old_backoff_value}"
)
}
}
assert_eq!(
current_backoff_value.expect("Should have produced backoff values to compare"),
DEFAULT_MAX_BACKOFF_SECONDS,
"Given big enough of retries, backoff should reach its allowed max value"
);
}
}