From 33351953baf32be96f6ec11982c05f2bb1edb989 Mon Sep 17 00:00:00 2001 From: Sakil Mostak <73734619+Sakilmostak@users.noreply.github.com> Date: Fri, 26 Apr 2024 12:00:27 +0530 Subject: [PATCH] refactor(scheduler): join frequency and count in `RetryMapping` (#4313) --- crates/router/src/workflows/payment_sync.rs | 2 +- .../src/consumer/types/process_data.rs | 25 +++++----------- crates/scheduler/src/utils.rs | 30 +++++++------------ 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/crates/router/src/workflows/payment_sync.rs b/crates/router/src/workflows/payment_sync.rs index 4b35e4d73b70..932a06bae6f6 100644 --- a/crates/router/src/workflows/payment_sync.rs +++ b/crates/router/src/workflows/payment_sync.rs @@ -300,7 +300,7 @@ mod tests { vec![schedule_time_delta, first_retry_time_delta], vec![ cpt_default.start_after, - *cpt_default.frequency.first().unwrap() + cpt_default.frequencies.first().unwrap().0 ] ); } diff --git a/crates/scheduler/src/consumer/types/process_data.rs b/crates/scheduler/src/consumer/types/process_data.rs index 267305c37f99..98d0fe530e27 100644 --- a/crates/scheduler/src/consumer/types/process_data.rs +++ b/crates/scheduler/src/consumer/types/process_data.rs @@ -6,8 +6,7 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct RetryMapping { pub start_after: i32, - pub frequency: Vec, - pub count: Vec, + pub frequencies: Vec<(i32, i32)>, // (frequency, count) } #[derive(Serialize, Deserialize)] @@ -23,8 +22,7 @@ impl Default for ConnectorPTMapping { custom_merchant_mapping: HashMap::new(), default_mapping: RetryMapping { start_after: 60, - frequency: vec![300], - count: vec![5], + frequencies: vec![(300, 5)], }, max_retries_count: 5, } @@ -44,8 +42,7 @@ impl Default for PaymentMethodsPTMapping { custom_pm_mapping: HashMap::new(), default_mapping: RetryMapping { start_after: 900, - frequency: vec![300], - count: vec![5], + frequencies: vec![(300, 5)], }, max_retries_count: 5, } @@ -70,21 +67,15 @@ impl Default for OutgoingWebhookRetryProcessTrackerMapping { // 1st attempt happens after 1 minute start_after: 60, - frequency: vec![ + frequencies: vec![ // 2nd and 3rd attempts happen at intervals of 5 minutes each - 60 * 5, + (60 * 5, 2), // 4th, 5th, 6th, 7th and 8th attempts happen at intervals of 10 minutes each - 60 * 10, + (60 * 10, 5), // 9th, 10th, 11th, 12th and 13th attempts happen at intervals of 1 hour each - 60 * 60, + (60 * 60, 5), // 14th, 15th and 16th attempts happen at intervals of 6 hours each - 60 * 60 * 6, - ], - count: vec![ - 2, // 2nd and 3rd attempts - 5, // 4th, 5th, 6th, 7th and 8th attempts - 5, // 9th, 10th, 11th, 12th and 13th attempts - 3, // 14th, 15th and 16th attempts + (60 * 60 * 6, 3), ], }, custom_merchant_mapping: HashMap::new(), diff --git a/crates/scheduler/src/utils.rs b/crates/scheduler/src/utils.rs index 48d02f7fcedc..11924dd1be2e 100644 --- a/crates/scheduler/src/utils.rs +++ b/crates/scheduler/src/utils.rs @@ -315,10 +315,7 @@ pub fn get_schedule_time( if retry_count == 0 { Some(mapping.start_after) } else { - get_delay( - retry_count, - mapping.count.iter().zip(mapping.frequency.iter()), - ) + get_delay(retry_count, &mapping.frequencies) } } @@ -335,10 +332,7 @@ pub fn get_pm_schedule_time( if retry_count == 0 { Some(mapping.start_after) } else { - get_delay( - retry_count, - mapping.count.iter().zip(mapping.frequency.iter()), - ) + get_delay(retry_count, &mapping.frequencies) } } @@ -356,25 +350,22 @@ pub fn get_outgoing_webhook_retry_schedule_time( if retry_count == 0 { Some(retry_mapping.start_after) } else { - get_delay( - retry_count, - retry_mapping - .count - .iter() - .zip(retry_mapping.frequency.iter()), - ) + get_delay(retry_count, &retry_mapping.frequencies) } } /// Get the delay based on the retry count -fn get_delay<'a>(retry_count: i32, array: impl Iterator) -> Option { +fn get_delay<'a>( + retry_count: i32, + frequencies: impl IntoIterator, +) -> Option { // Preferably, fix this by using unsigned ints if retry_count <= 0 { return None; } let mut cumulative_count = 0; - for (&count, &frequency) in array { + for &(frequency, count) in frequencies.into_iter() { cumulative_count += count; if cumulative_count >= retry_count { return Some(frequency); @@ -423,8 +414,7 @@ mod tests { #[test] fn test_get_delay() { - let count = [10, 5, 3, 2]; - let frequency = [300, 600, 1800, 3600]; + let frequency_count = vec![(300, 10), (600, 5), (1800, 3), (3600, 2)]; let retry_counts_and_expected_delays = [ (-4, None), @@ -442,7 +432,7 @@ mod tests { ]; for (retry_count, expected_delay) in retry_counts_and_expected_delays { - let delay = get_delay(retry_count, count.iter().zip(frequency.iter())); + let delay = get_delay(retry_count, &frequency_count); assert_eq!( delay, expected_delay,