mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-01 02:57:02 +08:00 
			
		
		
		
	refactor(scheduler): join frequency and count in RetryMapping (#4313)
				
					
				
			This commit is contained in:
		| @ -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 | ||||
|             ] | ||||
|         ); | ||||
|     } | ||||
|  | ||||
| @ -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)] | ||||
| @ -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(), | ||||
|  | ||||
| @ -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<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); | ||||
| @ -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, | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Sakil Mostak
					Sakil Mostak