use anchorkit::retry::{RetryConfig, RetryEngine};
// Use default config (3 attempts, 100ms initial delay)
let engine = RetryEngine::with_default_config();
// Execute with retry
let result = engine.execute(|attempt| {
make_network_request()
});RetryConfig::new(5, 100, 5000, 2)
// 5 attempts, 100ms initial, 5s max, 2x multiplier
// Timing: 0, 100, 200, 400, 800msRetryConfig::new(5, 500, 30000, 3)
// 5 attempts, 500ms initial, 30s max, 3x multiplier
// Timing: 0, 500, 1500, 4500, 13500msRetryConfig::new(4, 200, 10000, 2)
// 4 attempts, 200ms initial, 10s max, 2x multiplier
// Timing: 0, 200, 400, 800ms✅ Will Retry:
TransportError- Network failuresTransportTimeout- TimeoutsRateLimitExceeded- Rate limiting (429)ProtocolRateLimitExceeded- Protocol rate limitsEndpointNotFound- Endpoint unavailableStaleQuote- Quote expiredCacheExpired- Cache expired
❌ Won't Retry:
InvalidConfig- Configuration errorTransportUnauthorized- Auth failure (401/403)InvalidQuote- Validation errorReplayAttack- Security violationComplianceNotMet- Compliance failureProtocolError- Protocol violation
let result = engine.execute(|_| {
operation()
});
if result.is_success() {
let value = result.value.unwrap();
}let result = engine.execute(|attempt| {
println!("Attempt {}", attempt + 1);
operation()
});
println!("Attempts: {}", result.attempts);
println!("Total delay: {}ms", result.total_delay_ms);let result = engine.execute(|_| {
match operation() {
Ok(v) => Ok(v),
Err(Error::TransportTimeout) => {
log("Timeout, retrying...");
Err(Error::TransportTimeout)
}
Err(e) => Err(e),
}
});# All retry tests
cargo test retry --lib
# Network failure tests
cargo test test_network_failure --lib
# Rate limit tests
cargo test test_rate_limit --lib- Full docs:
RETRY_BACKOFF.md - Implementation:
RETRY_IMPLEMENTATION_SUMMARY.md - Code:
src/retry.rs - Tests:
src/retry_tests.rs