use std::time::{
Duration,
Instant,
};
use crate::AttemptFailure;
pub(in crate::executor) struct RetryFlowState<E> {
pub(in crate::executor) started_at: Instant,
pub(in crate::executor) operation_elapsed: Duration,
pub(in crate::executor) attempts: u32,
last_failure: Option<AttemptFailure<E>>,
}
impl<E> RetryFlowState<E> {
pub(in crate::executor) fn new() -> Self {
Self {
started_at: Instant::now(),
operation_elapsed: Duration::ZERO,
attempts: 0,
last_failure: None,
}
}
#[inline]
pub(in crate::executor) fn total_elapsed(&self) -> Duration {
self.started_at.elapsed()
}
#[inline]
pub(in crate::executor) fn start_next_attempt(&mut self) -> u32 {
self.attempts += 1;
self.attempts
}
#[inline]
pub(in crate::executor) fn add_operation_elapsed(&mut self, attempt_elapsed: Duration) {
self.operation_elapsed = self.operation_elapsed.saturating_add(attempt_elapsed);
}
#[inline]
pub(in crate::executor) fn record_last_failure(&mut self, failure: AttemptFailure<E>) {
self.last_failure = Some(failure);
}
#[inline]
pub(in crate::executor) fn take_last_failure(&mut self) -> Option<AttemptFailure<E>> {
self.last_failure.take()
}
}