|
| 1 | +<div align="center"> |
| 2 | + <img src="./logo.png" alt="Recoverable Logo" width="96"> |
| 3 | + |
| 4 | +# Recoverable |
| 5 | + |
| 6 | +[](https://crates.io/crates/recoverable) |
| 7 | +[](https://docs.rs/recoverable) |
| 8 | +[](https://crates.io/crates/recoverable) |
| 9 | +[](https://github.com/microsoft/oxidizer/actions/workflows/main.yml) |
| 10 | +[](https://codecov.io/gh/microsoft/oxidizer) |
| 11 | +[](../../LICENSE) |
| 12 | +<a href="../.."><img src="../../logo.svg" alt="This crate was developed as part of the Oxidizer project" width="20"></a> |
| 13 | + |
| 14 | +</div> |
| 15 | + |
| 16 | +Recovery information and classification for resilience patterns. |
| 17 | + |
| 18 | +## Why |
| 19 | + |
| 20 | +This crate provides types for classifying conditions based on their **recoverability state**, |
| 21 | +enabling consistent recovery behavior across different error types and resilience middleware. |
| 22 | + |
| 23 | +## Recovery Information |
| 24 | + |
| 25 | +The recovery information describes whether recovering from an operation might help, not whether |
| 26 | +the operation succeeded or failed. Both successful operations and permanent failures |
| 27 | +should use [`RecoveryInfo::never`][__link0] since recovery is not necessary or desirable. |
| 28 | + |
| 29 | +## Core Types |
| 30 | + |
| 31 | +* [`RecoveryInfo`][__link1]: Classifies conditions as recoverable (transient) or non-recoverable (permanent/successful). |
| 32 | +* [`Recovery`][__link2]: A trait for types that can determine their recoverability. |
| 33 | +* [`RecoveryKind`][__link3]: An enum representing the kind of recovery that can be attempted. |
| 34 | + |
| 35 | +## Examples |
| 36 | + |
| 37 | +### Recovery Error |
| 38 | + |
| 39 | +```rust |
| 40 | +use recoverable::{Recovery, RecoveryInfo, RecoveryKind}; |
| 41 | + |
| 42 | +#[derive(Debug)] |
| 43 | +enum DatabaseError { |
| 44 | + ConnectionTimeout, |
| 45 | + InvalidCredentials, |
| 46 | + TableNotFound, |
| 47 | +} |
| 48 | + |
| 49 | +impl Recovery for DatabaseError { |
| 50 | + fn recovery(&self) -> RecoveryInfo { |
| 51 | + match self { |
| 52 | + // Transient failure - might succeed if retried |
| 53 | + DatabaseError::ConnectionTimeout => RecoveryInfo::retry(), |
| 54 | + // Permanent failures - retrying won't help |
| 55 | + DatabaseError::InvalidCredentials => RecoveryInfo::never(), |
| 56 | + DatabaseError::TableNotFound => RecoveryInfo::never(), |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +let error = DatabaseError::ConnectionTimeout; |
| 62 | +assert_eq!(error.recovery().kind(), RecoveryKind::Retry); |
| 63 | + |
| 64 | +// For successful operations, also use never() since retry is unnecessary |
| 65 | +let success_result: Result<(), DatabaseError> = Ok(()); |
| 66 | +// If we had a wrapper type for success, it would also return RecoveryInfo::never() |
| 67 | +``` |
| 68 | + |
| 69 | +### Retry Delay |
| 70 | + |
| 71 | +You can specify when to retry an operation using the `delay` method: |
| 72 | + |
| 73 | +```rust |
| 74 | +use std::time::Duration; |
| 75 | +use recoverable::{RecoveryInfo, RecoveryKind}; |
| 76 | + |
| 77 | +// Retry with a 30-second delay (e.g., from a Retry-After header) |
| 78 | +let recovery = RecoveryInfo::retry().delay(Duration::from_secs(30)); |
| 79 | +assert_eq!(recovery.kind(), RecoveryKind::Retry); |
| 80 | +assert_eq!(recovery.get_delay(), Some(Duration::from_secs(30))); |
| 81 | + |
| 82 | +// Immediate retry |
| 83 | +let immediate = RecoveryInfo::retry().delay(Duration::ZERO); |
| 84 | +assert_eq!(immediate.get_delay(), Some(Duration::ZERO)); |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +<hr/> |
| 89 | +<sub> |
| 90 | +This crate was developed as part of <a href="../..">The Oxidizer Project</a>. Browse this crate's <a href="https://github.com/microsoft/oxidizer/tree/main/crates/recoverable">source code</a>. |
| 91 | +</sub> |
| 92 | + |
| 93 | + [__cargo_doc2readme_dependencies_info]: ggGkYW0CYXSEGy4k8ldDFPOhG2VNeXtD5nnKG6EPY6OfW5wBG8g18NOFNdxpYXKEG4cFLMVQymhvG3_1rzbl1X55G-vZhEWC9_13GwjdQK0PrVchYWSBgmtyZWNvdmVyYWJsZWUwLjEuMA |
| 94 | + [__link0]: https://docs.rs/recoverable/0.1.0/recoverable/?search=RecoveryInfo::never |
| 95 | + [__link1]: https://docs.rs/recoverable/0.1.0/recoverable/struct.RecoveryInfo.html |
| 96 | + [__link2]: https://docs.rs/recoverable/0.1.0/recoverable/trait.Recovery.html |
| 97 | + [__link3]: https://docs.rs/recoverable/0.1.0/recoverable/enum.RecoveryKind.html |
0 commit comments