freenet-core
freenet-core copied to clipboard
Use existing Backoff utility in update.rs instead of custom implementation
Use existing Backoff utility in update.rs instead of custom implementation
Description
Currently, the retry logic in crates/core/src/operations/update.rs implements a custom exponential backoff mechanism:
const MAX_RETRIES: usize = 10;
const BASE_DELAY_MS: u64 = 100;
const MAX_DELAY_MS: u64 = 5000;
// ...
let delay_ms = std::cmp::min(
BASE_DELAY_MS * (1 << retry_count), // Exponential backoff: BASE_DELAY_MS * 2^retry_count
MAX_DELAY_MS,
);
However, there's an existing Backoff utility in crates/core/src/util/mod.rs that provides the same functionality with additional features:
pub struct Backoff {
attempt: usize,
max_attempts: usize,
base: Duration,
ceiling: Duration,
strategy: BackoffStrategy,
}
Proposed Solution
Refactor the retry logic in update.rs to use the existing Backoff utility instead of the custom implementation. This would:
- Reduce code duplication
- Provide more flexibility with different backoff strategies
- Make the code more maintainable
- Ensure consistent backoff behavior across the codebase
Implementation Notes
Replace the custom backoff implementation with:
let mut backoff = Backoff::new(
Duration::from_millis(BASE_DELAY_MS),
Duration::from_millis(MAX_DELAY_MS),
MAX_RETRIES
);
// When needing to sleep:
if let Some(_) = backoff.sleep().await {
// Continue with retry
} else {
// Max retries exceeded
}
Related PR
This issue was identified during the implementation of retry logic for update propagation in PR #1596.