From 2c56bf8ad57296338093c0321d5923358413571d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 8 Apr 2024 11:45:36 +0200 Subject: [PATCH] ignore first error --- crates/api_common/src/lib.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs index 399f9513d..dd9d51015 100644 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@ -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)); } }