refactor: delete duplicate RetryStrategy from trading_engine
The RetryStrategy enum in trading_engine/src/types/error.rs was an exact duplicate of common::error::RetryStrategy with zero callers in the crate. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,88 +1,7 @@
|
||||
//! Error module for Foxhunt HFT system.
|
||||
|
||||
// Removed pub use - import errors directly where needed
|
||||
|
||||
use std::fmt;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// Note: ErrorSeverity moved to error-handling crate to avoid duplication
|
||||
|
||||
// Note: ErrorSeverity Display impl moved to error-handling crate
|
||||
|
||||
/// Retry strategies for error recovery with exponential backoff
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
/// RetryStrategy component.
|
||||
pub enum RetryStrategy {
|
||||
/// Do not retry - error is permanent
|
||||
NoRetry,
|
||||
/// Retry immediately without delay
|
||||
Immediate,
|
||||
/// Linear backoff with fixed intervals
|
||||
Linear {
|
||||
/// Base delay in milliseconds between retries
|
||||
base_delay_ms: u64,
|
||||
},
|
||||
/// Exponential backoff with jitter
|
||||
Exponential {
|
||||
/// Base delay in milliseconds for exponential backoff
|
||||
base_delay_ms: u64,
|
||||
/// Maximum delay cap in milliseconds
|
||||
max_delay_ms: u64,
|
||||
},
|
||||
/// Wait for circuit breaker to close
|
||||
CircuitBreaker,
|
||||
}
|
||||
|
||||
impl RetryStrategy {
|
||||
/// Calculate delay for retry attempt with jitter to prevent thundering herd
|
||||
#[must_use]
|
||||
pub fn calculate_delay(&self, attempt: u32) -> Option<Duration> {
|
||||
match self {
|
||||
Self::NoRetry => None,
|
||||
Self::Immediate => Some(Duration::from_millis(0)),
|
||||
Self::Linear { base_delay_ms } => {
|
||||
Some(Duration::from_millis(base_delay_ms * u64::from(attempt)))
|
||||
}
|
||||
Self::Exponential {
|
||||
base_delay_ms,
|
||||
max_delay_ms,
|
||||
} => {
|
||||
let delay_ms = base_delay_ms * 2_u64.pow(attempt.min(10));
|
||||
let capped_delay = delay_ms.min(*max_delay_ms);
|
||||
|
||||
// Add jitter to prevent thundering herd (±10%)
|
||||
// Use float division for precise calculation, then convert back to int
|
||||
#[allow(
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_precision_loss
|
||||
)]
|
||||
// Safe: round() ensures valid range, precision loss is acceptable for jitter calculation
|
||||
let jitter = (((capped_delay as f64) * 0.1).round() as u64).max(1);
|
||||
let jittered_delay =
|
||||
capped_delay.saturating_sub(jitter) + (jitter * 2).saturating_div(2); // Simple deterministic jitter replacement
|
||||
|
||||
Some(Duration::from_millis(jittered_delay))
|
||||
}
|
||||
Self::CircuitBreaker => Some(Duration::from_secs(30)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get maximum recommended retry attempts for this strategy
|
||||
#[must_use]
|
||||
pub const fn max_attempts(&self) -> Option<u32> {
|
||||
match self {
|
||||
Self::NoRetry => Some(0),
|
||||
Self::Immediate => Some(3),
|
||||
Self::Linear { .. } => Some(5),
|
||||
Self::Exponential { .. } => Some(7),
|
||||
Self::CircuitBreaker => Some(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
// Error types and conversions for trading_engine.
|
||||
// ErrorSeverity and RetryStrategy live in the common crate.
|
||||
|
||||
// ===== EXTERNAL ERROR CONVERSIONS =====
|
||||
|
||||
|
||||
Reference in New Issue
Block a user