Skip to content

Commit

Permalink
refactor(scheduler): join frequency and count in RetryMapping (#4313)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakilmostak authored Apr 26, 2024
1 parent e730030 commit 3335195
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
2 changes: 1 addition & 1 deletion crates/router/src/workflows/payment_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
);
}
Expand Down
25 changes: 8 additions & 17 deletions crates/scheduler/src/consumer/types/process_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RetryMapping {
pub start_after: i32,
pub frequency: Vec<i32>,
pub count: Vec<i32>,
pub frequencies: Vec<(i32, i32)>, // (frequency, count)
}

#[derive(Serialize, Deserialize)]
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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(),
Expand Down
30 changes: 10 additions & 20 deletions crates/scheduler/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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<Item = (&'a i32, &'a i32)>) -> Option<i32> {
fn get_delay<'a>(
retry_count: i32,
frequencies: impl IntoIterator<Item = &'a (i32, i32)>,
) -> Option<i32> {
// 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);
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down

0 comments on commit 3335195

Please sign in to comment.