1个不稳定版本

0.1.0 2023年9月14日

#23#backoff

Download history 2750/week @ 2024-03-13 1455/week @ 2024-03-20 1029/week @ 2024-03-27 1797/week @ 2024-04-03 2706/week @ 2024-04-10 2696/week @ 2024-04-17 1536/week @ 2024-04-24 2198/week @ 2024-05-01 759/week @ 2024-05-08 671/week @ 2024-05-15 784/week @ 2024-05-22 2129/week @ 2024-05-29 2314/week @ 2024-06-05 1813/week @ 2024-06-12 1544/week @ 2024-06-19 1864/week @ 2024-06-26

8,156 每月下载量
用于 futures-retry-policies

MIT/Apache

10KB
81

futures-retry-policies-core

一个帮助重试Futures的crate。

use futures_retry_policies_core::{retry, RetryPolicy};
use std::{ops::ControlFlow, time::Duration};

// 1. Create your retry policy

/// Retries a request n times
pub struct Retries(usize);

// 2. Define how your policy behaves

impl RetryPolicy<Result<(), &'static str>> for Retries {
    fn should_retry(&mut self, result: Result<(), &'static str>) -> ControlFlow<Result<(), &'static str>, Duration> {
        if self.0 > 0 && result.is_err() {
            self.0 -= 1;
            // continue to retry on error
            ControlFlow::Continue(Duration::from_millis(100))
        } else {
            // We've got a success, or we've exhausted our retries, so break
            ControlFlow::Break(result)
        }
    }
}

/// Makes a request, like a HTTP request or gRPC request which you want to retry
async fn make_request() -> Result<(), &'static str>  {
    // make a request
    # static COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
    # if COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst) < 2 { Err("fail") } else { Ok(()) }
}

#[tokio::main]
async fn main() -> Result<(), &'static str> {
    // 3. Await the retry with your policy, a sleep function, and your async function.
    retry(Retries(3), tokio::time::sleep, make_request).await
}

依赖项

~0.3–0.8MB
~18K SLoC