-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add per data-source healthcheck with retry (#101)
Automatically retry connection to failed data sources between 2 general status checks - Ensure that all sources are updated even after single source update - Make ProxyService keep track of the status of all its sources - Split part of the status check helpers in their own files Also pick up small clippy lints Fixes: FP-2072
- Loading branch information
Showing
4 changed files
with
260 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::time::Duration; | ||
|
||
use fiberplane::protocols::names::Name; | ||
|
||
/// A token representing both: | ||
/// - a task to check the status of the data source having a given name, and | ||
/// - the retry strategy to use in case the status check failed. | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct DataSourceCheckTask { | ||
name: Name, | ||
retries_left: isize, | ||
delay_till_next: Duration, | ||
backoff_factor: f32, | ||
} | ||
|
||
impl DataSourceCheckTask { | ||
/// Constructor | ||
/// | ||
/// `backoff_factor` MUST be greater than 1 | ||
/// `initial_delay` MUST be greater than 0s | ||
/// | ||
/// The constructor guarantees that: | ||
/// - assuming a "try" takes a negligible amount of time, | ||
/// - no retry will be attempted past `total_checks_duration` | ||
/// | ||
/// If you are not sure that a "try" is going to be instant, | ||
/// you should add a safety buffer by decreasing the `total_checks_duration` argument. | ||
pub(crate) fn new( | ||
name: Name, | ||
total_checks_duration: Duration, | ||
initial_delay: Duration, | ||
backoff_factor: f32, | ||
) -> DataSourceCheckTask { | ||
let max_retries: isize = if initial_delay > total_checks_duration { | ||
0 | ||
} else { | ||
// Formula comes from the sum of terms in geometric series | ||
(((1.0 | ||
+ (backoff_factor - 1.0) * total_checks_duration.as_secs_f32() | ||
/ initial_delay.as_secs_f32()) | ||
.ln() | ||
/ backoff_factor.ln()) | ||
.floor() | ||
- 1.0) as isize | ||
}; | ||
|
||
Self { | ||
name, | ||
retries_left: max_retries, | ||
delay_till_next: initial_delay, | ||
backoff_factor, | ||
} | ||
} | ||
|
||
/// Return the next check task to accomplish after this one, with | ||
/// the delay to wait before sending it to the channel. | ||
pub(crate) fn next(self) -> Option<(Duration, Self)> { | ||
let Self { | ||
name, | ||
retries_left, | ||
delay_till_next, | ||
backoff_factor, | ||
} = self; | ||
if retries_left <= 0 { | ||
return None; | ||
} | ||
Some(( | ||
delay_till_next, | ||
Self { | ||
name, | ||
retries_left: retries_left - 1, | ||
delay_till_next: Duration::from_secs_f32( | ||
delay_till_next.as_secs_f32() * backoff_factor, | ||
), | ||
backoff_factor, | ||
}, | ||
)) | ||
} | ||
|
||
pub(crate) fn name(&self) -> &Name { | ||
&self.name | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn retries_left(&self) -> isize { | ||
self.retries_left | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::service::status_check::DataSourceCheckTask; | ||
use fiberplane::protocols::names::Name; | ||
use std::time::Duration; | ||
|
||
#[test] | ||
fn exponential_backoff_cap() { | ||
fn test_rec(task: DataSourceCheckTask, old_delay: Duration, remaining_budget: Duration) { | ||
if let Some((delay, new_task)) = task.next() { | ||
assert!( | ||
delay > old_delay, | ||
"the new delay is longer than the old one." | ||
); | ||
let new_remaining_budget = remaining_budget.checked_sub(delay); | ||
assert!( | ||
new_remaining_budget.is_some(), | ||
"The delay ({:?}) is bigger than the remaining budget {:?}", | ||
delay, | ||
remaining_budget | ||
); | ||
test_rec(new_task, delay, new_remaining_budget.unwrap()); | ||
} | ||
} | ||
|
||
fn test_case(total_duration: Duration, initial_delay: Duration, backoff_factor: f32) { | ||
let task = DataSourceCheckTask::new( | ||
Name::from_static("be-the-change"), | ||
total_duration, | ||
initial_delay, | ||
backoff_factor, | ||
); | ||
assert!(task.retries_left() >= 0, "At least 1 try will be attempted"); | ||
test_rec(task, Duration::from_secs(0), total_duration); | ||
} | ||
|
||
test_case(Duration::from_secs(300), Duration::from_secs(10), 1.5); | ||
test_case(Duration::from_secs(300), Duration::from_secs(1000), 1.5); | ||
test_case(Duration::from_secs(300), Duration::from_secs(300), 1.5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters