Use exponential backoff when waiting for delivery retry

This commit is contained in:
silverpill 2022-08-03 14:57:46 +00:00
parent eb5e6b969b
commit 980d65fa5e

View file

@ -77,6 +77,12 @@ async fn send_activity(
Ok(()) Ok(())
} }
// 30 secs, 5 mins, 50 mins, 8 hours
fn backoff(retry_count: u32) -> Duration {
debug_assert!(retry_count > 0);
Duration::from_secs(3 * 10_u64.pow(retry_count))
}
async fn deliver_activity_worker( async fn deliver_activity_worker(
instance: Instance, instance: Instance,
sender: User, sender: User,
@ -107,8 +113,8 @@ async fn deliver_activity_worker(
let max_retries = 2; let max_retries = 2;
while !inboxes.is_empty() && retry_count <= max_retries { while !inboxes.is_empty() && retry_count <= max_retries {
if retry_count > 0 { if retry_count > 0 {
// Wait 30 secs before next attempt // Wait before next attempt
sleep(Duration::from_secs(30)).await; sleep(backoff(retry_count)).await;
}; };
let mut failed = vec![]; let mut failed = vec![];
for inbox_url in inboxes { for inbox_url in inboxes {
@ -159,3 +165,14 @@ impl<A: Serialize + Send + 'static> OutgoingActivity<A> {
}); });
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_backoff() {
assert_eq!(backoff(1).as_secs(), 30);
assert_eq!(backoff(2).as_secs(), 300);
}
}