use std::time::Duration;
const TRADE_OPERATIONS: &[&str] = &["place_order", "modify_order", "cancel_order"];
pub struct RetryPolicy {
pub max_retries: u32,
pub max_retry_time: Duration,
pub base_delay: Duration,
pub max_delay: Duration,
}
impl Default for RetryPolicy {
fn default() -> Self {
Self {
max_retries: 5,
max_retry_time: Duration::from_secs(60),
base_delay: Duration::from_secs(1),
max_delay: Duration::from_secs(16),
}
}
}
impl RetryPolicy {
pub fn should_retry(&self, api_method: &str) -> bool {
!is_trade_operation(api_method)
}
pub fn calculate_backoff(&self, retry_count: u32) -> Duration {
let delay = self.base_delay.mul_f64(2f64.powi(retry_count as i32));
if delay > self.max_delay {
self.max_delay
} else {
delay
}
}
}
pub fn is_trade_operation(api_method: &str) -> bool {
TRADE_OPERATIONS.contains(&api_method)
}
#[cfg(test)]
mod tests;