-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic retry policies to zebra-network.
This should be removed when tower-rs/tower#414 lands but is good enough for our purposes for now.
- Loading branch information
1 parent
5191b9d
commit acccdda
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use tower::retry::Policy; | ||
use futures::future; | ||
|
||
/// A very basic retry policy with a limited number of retry attempts. | ||
/// | ||
/// XXX Remove this when https://github.com/tower-rs/tower/pull/414 lands. | ||
#[derive(Clone, Debug)] | ||
pub struct RetryLimit { | ||
remaining_tries: usize, | ||
} | ||
|
||
impl RetryLimit { | ||
/// Create a policy with the given number of retry attempts. | ||
pub fn new(retry_attempts: usize) -> Self { | ||
RetryLimit { | ||
remaining_tries: retry_attempts, | ||
} | ||
} | ||
} | ||
|
||
impl<Req: Clone, Res, E> Policy<Req, Res, E> for RetryLimit { | ||
type Future = future::Ready<Self>; | ||
fn retry(&self, _: &Req, result: Result<&Res, &E>) -> Option<Self::Future> { | ||
if result.is_err() { | ||
if self.remaining_tries > 0 { | ||
Some(future::ready(RetryLimit { | ||
remaining_tries: self.remaining_tries - 1, | ||
})) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn clone_request(&self, req: &Req) -> Option<Req> { | ||
Some(req.clone()) | ||
} | ||
} | ||
|
||
/// A very basic retry policy that always retries failed requests. | ||
/// | ||
/// XXX remove this when https://github.com/tower-rs/tower/pull/414 lands. | ||
#[derive(Clone, Debug)] | ||
pub struct RetryErrors; | ||
|
||
impl<Req: Clone, Res, E> Policy<Req, Res, E> for RetryErrors { | ||
type Future = future::Ready<Self>; | ||
fn retry(&self, _: &Req, result: Result<&Res, &E>) -> Option<Self::Future> { | ||
if result.is_err() { | ||
Some(future::ready(RetryErrors)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn clone_request(&self, req: &Req) -> Option<Req> { | ||
Some(req.clone()) | ||
} | ||
} |