mirror of
https://github.com/LemmyNet/lemmy
synced 2024-11-10 06:54:12 +00:00
Change exponential backoff algorithm for federation send (#4597)
* Limit federation send retry interval to one hour * clippy * avoid overflow * change base for exp backoff * ignore first error * fix day duration
This commit is contained in:
parent
1d0a6ac08f
commit
b4670988b5
2 changed files with 37 additions and 5 deletions
|
@ -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,39 @@ impl Default for SuccessResponse {
|
|||
}
|
||||
}
|
||||
|
||||
/// how long to sleep based on how many retries have already happened
|
||||
// TODO: use from_days once stabilized
|
||||
// https://github.com/rust-lang/rust/issues/120301
|
||||
const DAY: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
|
||||
/// 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. The first
|
||||
/// error is ignored.
|
||||
pub fn federate_retry_sleep_duration(retry_count: i32) -> Duration {
|
||||
Duration::from_secs_f64(2.0_f64.powf(f64::from(retry_count)))
|
||||
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)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_federate_retry_sleep_duration() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue