From 7e4453620eb3b9eb618ff195b72fc0d5df5d898b Mon Sep 17 00:00:00 2001
From: Jason Volk <jason@zemos.net>
Date: Wed, 1 Jan 2025 20:09:41 +0000
Subject: [PATCH] move exponential backoff util to different submod

Signed-off-by: Jason Volk <jason@zemos.net>
---
 src/core/utils/math.rs                        | 30 +------------------
 src/core/utils/mod.rs                         |  5 +++-
 src/core/utils/time.rs                        |  2 ++
 src/core/utils/time/exponential_backoff.rs    | 29 ++++++++++++++++++
 .../fetch_and_handle_outliers.rs              |  2 +-
 .../rooms/event_handler/handle_prev_pdu.rs    |  2 +-
 src/service/sending/sender.rs                 |  2 +-
 7 files changed, 39 insertions(+), 33 deletions(-)
 create mode 100644 src/core/utils/time/exponential_backoff.rs

diff --git a/src/core/utils/math.rs b/src/core/utils/math.rs
index da2357d5..c5a785e2 100644
--- a/src/core/utils/math.rs
+++ b/src/core/utils/math.rs
@@ -1,4 +1,4 @@
-use std::{cmp, convert::TryFrom, time::Duration};
+use std::{cmp, convert::TryFrom};
 
 pub use checked_ops::checked_ops;
 
@@ -53,34 +53,6 @@ macro_rules! validated {
 	($($input:tt)+) => { $crate::expected!($($input)+) }
 }
 
-/// Returns false if the exponential backoff has expired based on the inputs
-#[inline]
-#[must_use]
-pub fn continue_exponential_backoff_secs(
-	min: u64,
-	max: u64,
-	elapsed: Duration,
-	tries: u32,
-) -> bool {
-	let min = Duration::from_secs(min);
-	let max = Duration::from_secs(max);
-	continue_exponential_backoff(min, max, elapsed, tries)
-}
-
-/// Returns false if the exponential backoff has expired based on the inputs
-#[inline]
-#[must_use]
-pub fn continue_exponential_backoff(
-	min: Duration,
-	max: Duration,
-	elapsed: Duration,
-	tries: u32,
-) -> bool {
-	let min = min.saturating_mul(tries).saturating_mul(tries);
-	let min = cmp::min(min, max);
-	elapsed < min
-}
-
 #[inline]
 #[allow(clippy::as_conversions)]
 pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs
index 38232820..16072765 100644
--- a/src/core/utils/mod.rs
+++ b/src/core/utils/mod.rs
@@ -37,7 +37,10 @@ pub use self::{
 	stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},
 	string::{str_from_bytes, string_from_bytes},
 	sys::compute::parallelism as available_parallelism,
-	time::{now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now},
+	time::{
+		exponential_backoff::{continue_exponential_backoff, continue_exponential_backoff_secs},
+		now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now,
+	},
 };
 
 #[inline]
diff --git a/src/core/utils/time.rs b/src/core/utils/time.rs
index d65eb2d4..81fdda2a 100644
--- a/src/core/utils/time.rs
+++ b/src/core/utils/time.rs
@@ -1,3 +1,5 @@
+pub mod exponential_backoff;
+
 use std::time::{Duration, SystemTime, UNIX_EPOCH};
 
 use crate::{err, Result};
diff --git a/src/core/utils/time/exponential_backoff.rs b/src/core/utils/time/exponential_backoff.rs
new file mode 100644
index 00000000..682c2592
--- /dev/null
+++ b/src/core/utils/time/exponential_backoff.rs
@@ -0,0 +1,29 @@
+use std::{cmp, time::Duration};
+
+/// Returns false if the exponential backoff has expired based on the inputs
+#[inline]
+#[must_use]
+pub fn continue_exponential_backoff_secs(
+	min: u64,
+	max: u64,
+	elapsed: Duration,
+	tries: u32,
+) -> bool {
+	let min = Duration::from_secs(min);
+	let max = Duration::from_secs(max);
+	continue_exponential_backoff(min, max, elapsed, tries)
+}
+
+/// Returns false if the exponential backoff has expired based on the inputs
+#[inline]
+#[must_use]
+pub fn continue_exponential_backoff(
+	min: Duration,
+	max: Duration,
+	elapsed: Duration,
+	tries: u32,
+) -> bool {
+	let min = min.saturating_mul(tries).saturating_mul(tries);
+	let min = cmp::min(min, max);
+	elapsed < min
+}
diff --git a/src/service/rooms/event_handler/fetch_and_handle_outliers.rs b/src/service/rooms/event_handler/fetch_and_handle_outliers.rs
index 2f6940ed..84d0edd0 100644
--- a/src/service/rooms/event_handler/fetch_and_handle_outliers.rs
+++ b/src/service/rooms/event_handler/fetch_and_handle_outliers.rs
@@ -6,7 +6,7 @@ use std::{
 
 use conduwuit::{
 	debug, debug_error, debug_warn, implement, pdu, trace,
-	utils::math::continue_exponential_backoff_secs, warn, PduEvent,
+	utils::continue_exponential_backoff_secs, warn, PduEvent,
 };
 use futures::TryFutureExt;
 use ruma::{
diff --git a/src/service/rooms/event_handler/handle_prev_pdu.rs b/src/service/rooms/event_handler/handle_prev_pdu.rs
index a8893160..0a5295dc 100644
--- a/src/service/rooms/event_handler/handle_prev_pdu.rs
+++ b/src/service/rooms/event_handler/handle_prev_pdu.rs
@@ -5,7 +5,7 @@ use std::{
 };
 
 use conduwuit::{
-	debug, implement, utils::math::continue_exponential_backoff_secs, Err, PduEvent, Result,
+	debug, implement, utils::continue_exponential_backoff_secs, Err, PduEvent, Result,
 };
 use ruma::{CanonicalJsonValue, EventId, OwnedEventId, RoomId, ServerName};
 
diff --git a/src/service/sending/sender.rs b/src/service/sending/sender.rs
index bf4ebafb..482c31cf 100644
--- a/src/service/sending/sender.rs
+++ b/src/service/sending/sender.rs
@@ -13,7 +13,7 @@ use conduwuit::{
 	debug, err, error,
 	result::LogErr,
 	trace,
-	utils::{calculate_hash, math::continue_exponential_backoff_secs, ReadyExt},
+	utils::{calculate_hash, continue_exponential_backoff_secs, ReadyExt},
 	warn, Error, Result,
 };
 use futures::{