2 releases
0.1.6 | Aug 6, 2023 |
---|---|
0.1.5 | Aug 6, 2023 |
#899 in Asynchronous
17KB
371 lines
backoff-tower
A tower layer to apply a backoff strategy to retried requests
Overview
tower
has a builtin RetryLayer
which retries requests immediately.
BackoffLayer
is a wrapper around a RetryLayer
which applies a BackoffStrategy
to requests
so that there is a delay before they are retried. Both tokio
and async-std
are supported, with tokio
being selected via default features
#[derive(Clone)]
struct MyPolicy {
attempts_left: usize,
}
impl Policy<usize, usize, &'static str> for MyPolicy {
type Future = Ready<Self>;
fn retry(&self, _req: &usize, result: Result<&usize, &&'static str>) -> Option<Self::Future> {
if self.attempts_left == 0 {
return None;
}
match result {
Ok(_) => None,
Err(_) => Some(ready(MyPolicy { attempts_left: self.attempts_left - 1 }))
}
}
fn clone_request(&self, req: &usize) -> Option<usize> {
Some(req + 1)
}
}
#[tokio::main]
async fn main() {
let mut service = ServiceBuilder::new()
.layer(BackoffLayer::new(MyPolicy { attempts_left: 8 }, ExponentialBackoffStrategy))
.service_fn(|x: usize| async move {
if x % 10 == 0 {
Ok(x / 10)
} else {
Err("bad input")
}
});
assert_eq!(Ok(6), service.call(55).await, "should be the next multiple of 10 divided by 10");
assert_eq!(Err("bad input"), service.call(51).await, "retry limit should have been hit");
}
Dependencies
~2–13MB
~149K SLoC