Crate unistore_retry

Crate unistore_retry 

Source
Expand description

§unistore-retry

重试策略能力 - UniStore 能力生态的一部分。

§功能特性

  • 多种策略: 固定间隔、指数退避、线性递增、无延迟
  • 抖动支持: 防止惊群效应
  • 可重试判断: 自定义哪些错误应该重试
  • 异步友好: 完美支持 async/await
  • 详细统计: 提供重试次数和耗时统计

§快速开始

use unistore_retry::{retry, ExponentialBackoff};
use std::time::Duration;

// 使用默认指数退避重试
let result = retry(ExponentialBackoff::default(), || async {
    // 可能失败的操作
    Ok::<_, &str>(42)
}).await?;

§自定义策略

use unistore_retry::{RetryConfig, ExponentialBackoff, retry};
use std::time::Duration;

let config = RetryConfig::default()
    .max_retries(5)
    .initial_delay(Duration::from_millis(100))
    .max_delay(Duration::from_secs(30))
    .jitter(0.1);

let strategy = ExponentialBackoff::new(config);

let result = retry(strategy, || async {
    Ok::<_, &str>("success")
}).await?;

§条件重试

use unistore_retry::{retry_if, FixedInterval};
use std::time::Duration;

let strategy = FixedInterval::new(Duration::from_millis(100), 3);

let result = retry_if(
    strategy,
    || async { Err::<(), _>("retryable") },
    |err| *err == "retryable",  // 只重试特定错误
).await;

Structs§

ExponentialBackoff
指数退避策略
FixedInterval
固定间隔策略
LinearBackoff
线性递增策略
NoDelay
无延迟策略(立即重试)
RetryConfig
重试配置
RetryPolicy
重试策略构建器(流畅 API)
RetryStats
重试统计信息

Enums§

RetryError
重试错误

Traits§

BackoffStrategy
重试策略 trait

Functions§

retry
执行带重试的异步操作
retry_if
执行带条件重试的异步操作
retry_with_stats
执行带重试和统计的异步操作

Type Aliases§

RetryResult
重试结果类型别名