ignore first error

federation-send-retry-limit
Felix Ableitner 2024-04-08 11:45:36 +02:00
parent e4abdefe09
commit 2c56bf8ad5
1 changed files with 16 additions and 5 deletions

View File

@ -48,8 +48,14 @@ impl Default for SuccessResponse {
const DAY: Duration = Duration::from_secs(3600);
/// Calculate how long to sleep until next federation send based on how many
/// retries have already happened. Uses exponential backoff with maximum of one day.
/// retries have already happened. Uses exponential backoff with maximum of one day. The first
/// error is ignored.
pub fn federate_retry_sleep_duration(retry_count: i32) -> Duration {
debug_assert!(retry_count != 0);
if retry_count == 1 {
return Duration::from_secs(0);
}
let retry_count = retry_count - 1;
let pow = 1.25_f64.powf(retry_count.into());
let pow = Duration::try_from_secs_f64(pow).unwrap_or(DAY);
min(DAY, pow)
@ -61,10 +67,15 @@ pub(crate) mod tests {
#[test]
fn test_federate_retry_sleep_duration() {
let s = |s,m: u32| Duration::new(s, m * 1000);
assert_eq!(s(1, 0), federate_retry_sleep_duration(0));
assert_eq!(s(1, 250000), federate_retry_sleep_duration(1));
assert_eq!(s(1, 562500), federate_retry_sleep_duration(2));
assert_eq!(Duration::from_secs(0), federate_retry_sleep_duration(1));
assert_eq!(
Duration::new(1, 250000000),
federate_retry_sleep_duration(2)
);
assert_eq!(
Duration::new(2, 441406250),
federate_retry_sleep_duration(5)
);
assert_eq!(DAY, federate_retry_sleep_duration(100));
}
}