-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic "batteries-included" retry::Policy
s.
#414
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 super::Policy; | ||
use futures_util::future; | ||
|
||
/// A very basic retry policy with a limited number of retry attempts. | ||
/// | ||
/// FIXME: explain why not to use this. | ||
#[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. | ||
/// | ||
/// FIXME: explain why not to use this. | ||
#[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()) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is a way to traitize this? For example, http retries you may want to retry 500s but not 400s. In this case, if I were to apply this to hyper I would only be able to retry system errors not http errors. What do you think about providing some method to allow users to specify what they want to retry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One possibility would be to construct the
RetryLimit
with a closureF: FnMut(&E) -> bool
; then callers who wanted to retry all errors could construct it as, e.g.,and callers who want to filter retries can stick whatever logic they want in the closure. (Does that seem like the right bound for the closure?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though since this only returns an error that wouldn't support retrying on http::Responses?
I think we could use some trait and a TraitFn adapter for a closure, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'm not sure what kind of trait you're thinking of. I think I'm missing something: if the user-supplied logic has access to the whole response and error, how would that be different from just implementing
Policy::retry
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is closer to the related vector retrylogic thing I sent on discord, but the idea is that as a user I can just provide what i'd like to retry on instead of having to implement a retry policy myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't copy this 1-1 but https://github.com/timberio/vector/blob/master/src/sinks/util/retries.rs#L25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, finagle uses a
ResponseClassifier
in a similar way.