use super::optimistic::{RetryPolicy};
use crate::error::{StoreError, StoreResult};
use std::thread;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct FixedRetry {
pub max_attempts: usize,
pub backoff: Duration,
}
impl Default for FixedRetry {
fn default() -> Self {
Self {
max_attempts: 10,
backoff: Duration::from_millis(10),
}
}
}
impl RetryPolicy for FixedRetry {
fn should_retry(&self, error: &StoreError, attempt: usize) -> StoreResult<()> {
if attempt >= self.max_attempts {
return Err(StoreError::Other(format!(
"Optimistic transaction failed after {} attempts. Last error: {}",
self.max_attempts, error
)));
}
if let StoreError::RocksDb(db_err) = error {
log::debug!(
"Optimistic transaction conflict detected (attempt {}): {}. Retrying after {:?}...",
attempt, db_err, self.backoff
);
if self.backoff > Duration::ZERO {
thread::sleep(self.backoff);
}
return Ok(()); }
Err(error.clone())
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoRetry;
impl RetryPolicy for NoRetry {
fn should_retry(&self, error: &StoreError, _attempt: usize) -> StoreResult<()> {
Err(error.clone())
}
}