unistore_retry/
lib.rs

1//! # unistore-retry
2//!
3//! 重试策略能力 - UniStore 能力生态的一部分。
4//!
5//! ## 功能特性
6//!
7//! - **多种策略**: 固定间隔、指数退避、线性递增、无延迟
8//! - **抖动支持**: 防止惊群效应
9//! - **可重试判断**: 自定义哪些错误应该重试
10//! - **异步友好**: 完美支持 async/await
11//! - **详细统计**: 提供重试次数和耗时统计
12//!
13//! ## 快速开始
14//!
15//! ```rust
16//! use unistore_retry::{retry, ExponentialBackoff};
17//! use std::time::Duration;
18//!
19//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
20//! // 使用默认指数退避重试
21//! let result = retry(ExponentialBackoff::default(), || async {
22//!     // 可能失败的操作
23//!     Ok::<_, &str>(42)
24//! }).await?;
25//! # Ok(())
26//! # }
27//! ```
28//!
29//! ## 自定义策略
30//!
31//! ```rust
32//! use unistore_retry::{RetryConfig, ExponentialBackoff, retry};
33//! use std::time::Duration;
34//!
35//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
36//! let config = RetryConfig::default()
37//!     .max_retries(5)
38//!     .initial_delay(Duration::from_millis(100))
39//!     .max_delay(Duration::from_secs(30))
40//!     .jitter(0.1);
41//!
42//! let strategy = ExponentialBackoff::new(config);
43//!
44//! let result = retry(strategy, || async {
45//!     Ok::<_, &str>("success")
46//! }).await?;
47//! # Ok(())
48//! # }
49//! ```
50//!
51//! ## 条件重试
52//!
53//! ```rust
54//! use unistore_retry::{retry_if, FixedInterval};
55//! use std::time::Duration;
56//!
57//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
58//! let strategy = FixedInterval::new(Duration::from_millis(100), 3);
59//!
60//! let result = retry_if(
61//!     strategy,
62//!     || async { Err::<(), _>("retryable") },
63//!     |err| *err == "retryable",  // 只重试特定错误
64//! ).await;
65//! # Ok(())
66//! # }
67//! ```
68
69// === 内部模块 ===
70mod config;
71mod deps;
72mod error;
73mod executor;
74mod strategy;
75
76// === 对外接口(SDK)===
77// 统一从 lib.rs 导出所有公开类型
78
79pub use config::RetryConfig;
80pub use error::{RetryError, RetryResult};
81pub use executor::{retry, retry_if, retry_with_stats, RetryPolicy, RetryStats};
82pub use strategy::{BackoffStrategy, ExponentialBackoff, FixedInterval, LinearBackoff, NoDelay};