1 个不稳定版本

0.1.0 2022年6月22日

#1744 in 异步

MIT 许可证

12KB
99

attempt

Crates.io Documentation

一个用于使用各种配置选项重试失败操作的实用程序包。

示例

use attempt::Attempt;

fn fetch_data_from_unreliable_api() -> Result<Data, Error> {
    // Fetch data from an API which randomly fails for no reason.
    // We've all dealt with one of these.
}

fn main() {
    let res: Result<Data, Error> =
        Attempt::to(fetch_data_from_unreliable_api)
            .delay(std::time::Duration::from_secs(1))
            .max_tries(1000)
            .run();

    // Be careful with this one.
    let res: Data =
        Attempt::infinitely(fetch_data_from_unreliable_api);

    // "Sensible" default of 10 max tries with an increasing delay between
    //  each attempt starting at 500ms.
    let res: Result<Data, Error> = Attempt::to(fetch_data_from_unreliable_api).run();
}

async fn fetch_data_from_unreliable_api_async() -> Result<Data, Error> {
    // Fetch data from an API which randomly fails for no reason, but do it async!
}

async fn async_attempt_example() -> Result<Data, Error> {
    Attempt::to(fetch_data_from_unreliable_api_async)
        .delay(std::time::Duration::from_secs(1))
        .max_tries(1000)
        .run_async()
        .await
}

依赖

~0–1.3MB
~22K SLoC