4 个版本 (重大更改)

0.4.0 2022 年 12 月 1 日
0.3.0 2022 年 11 月 12 日
0.2.0 2022 年 10 月 17 日
0.1.0 2022 年 10 月 15 日

#961异步

MIT/Apache

23KB
413

Retry Future

该 crate 的主要目的是重试可能包含复杂场景的 Futures,这些场景不仅包括处理错误,还包括任何应该重试的内容。这可能包括重试 http 请求的 500 状态错误或重试类似于“伪”成功的 grpc 请求。

有关示例,请查看 examples/ 目录,但这里有一个

// imports...
use retry_future::{
    RetryFuture, RetryPolicy, ExponentialRetryStrategy, Error
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let resp = RetryFuture::new(
        || async {
            let resp = reqwest::get("https://127.0.0.1:8080").await?;
            match resp.status() {
                StatusCode::OK => Ok(resp),
                StatusCode::BAD_REQUEST | StatusCode::FORBIDDEN => Err(RetryPolicy::Fail(
                    String::from("Cannot recover from these kind of errors ._."),
                )),
                StatusCode::INTERNAL_SERVER_ERROR => Err(RetryPolicy::Retry(None)),
                StatusCode::UNAUTHORIZED => {
                    // What if authorization server lies us?! Retry it to be convinced
                    let maybe_response_text = resp.text().await.ok().map(Error::msg);  // debug info
                    Err(RetryPolicy::Retry(maybe_response_text))
                }
                _ => Err(RetryPolicy::Fail(format!("Some unusual response here: {resp:?}"))),
            }
        },
        ExponentialRetryStrategy::new()
            .max_attempts(5)
            .initial_delay(Duration::from_millis(100))
            .retry_early_returned_errors(true),
    )
        .await?;

    eprintln!("resp = {:#?}", resp);

    Ok(())
}

许可证

许可协议为以下之一

任选其一。

贡献

除非您明确声明,否则任何有意提交以包含在您的工作中的贡献(根据 Apache-2.0 许可证定义),都应双重许可,如上所述,没有任何额外的条款或条件。

依赖关系

~3–5MB
~82K SLoC