From 3f9e182110c2f9d2dfb3c22a50bcdc1862205925 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 5 Apr 2024 11:11:58 +0200 Subject: [PATCH] Limit federation send retry interval to one hour --- crates/api_common/src/lib.rs | 30 +++++++++++++++++++++++++++--- docker/Dockerfile | 2 +- scripts/test.sh | 4 ++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs index b55dff32f..fb0087f4c 100644 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@ -27,7 +27,7 @@ pub extern crate lemmy_utils; pub use lemmy_utils::LemmyErrorType; use serde::{Deserialize, Serialize}; -use std::time::Duration; +use std::{cmp::min, time::Duration}; #[derive(Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(ts_rs::TS))] @@ -43,7 +43,31 @@ impl Default for SuccessResponse { } } -/// how long to sleep based on how many retries have already happened +// TODO: use from_hours once stabilized +// https://github.com/rust-lang/rust/issues/120301 +const HOUR: 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 hour. pub fn federate_retry_sleep_duration(retry_count: i32) -> Duration { - Duration::from_secs_f64(2.0_f64.powf(f64::from(retry_count))) + let pow = 2.0_f64.powf(retry_count.into()); + let pow = Duration::from_secs_f64(pow); + min(HOUR, pow) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + #[test] + fn test_federate_retry_sleep_duration() { + let s = |secs| Duration::from_secs(secs); + assert_eq!(s(1), federate_retry_sleep_duration(0)); + assert_eq!(s(2), federate_retry_sleep_duration(1)); + assert_eq!(s(4), federate_retry_sleep_duration(2)); + assert_eq!(s(8), federate_retry_sleep_duration(3)); + assert_eq!(s(16), federate_retry_sleep_duration(4)); + assert_eq!(s(1024), federate_retry_sleep_duration(10)); + assert_eq!(s(3600), federate_retry_sleep_duration(20)); + } } diff --git a/docker/Dockerfile b/docker/Dockerfile index 9539402df..9dcd88026 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.6 -ARG RUST_VERSION=1.76 +ARG RUST_VERSION=1.77 ARG CARGO_BUILD_FEATURES=default ARG RUST_RELEASE_MODE=debug diff --git a/scripts/test.sh b/scripts/test.sh index efe9b1513..3e0581fc7 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -6,7 +6,7 @@ CWD="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" cd $CWD/../ PACKAGE="$1" -echo "$PACKAGE" +TEST="$2" source scripts/start_dev_db.sh @@ -17,7 +17,7 @@ export RUST_BACKTRACE=1 if [ -n "$PACKAGE" ]; then - cargo test -p $PACKAGE --all-features --no-fail-fast + cargo test -p $PACKAGE --all-features --no-fail-fast $TEST else cargo test --workspace --no-fail-fast fi