-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: add tokio-retry crate to retry failed requests #8
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||
use tokio_retry::strategy::{FibonacciBackoff, jitter}; | ||||||
use std::future::Future; | ||||||
use std::{ | ||||||
iter::{Take, Map}, | ||||||
time::Duration | ||||||
}; | ||||||
|
||||||
fn retry_strategy(ms: u64, attempts: usize) -> Take<Map<FibonacciBackoff, fn(Duration) -> Duration>>{ | ||||||
FibonacciBackoff::from_millis(ms) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.map(jitter as fn(Duration) -> Duration) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.take(attempts) | ||||||
} | ||||||
|
||||||
|
||||||
pub async fn with_retry<A, F, R, E>(ms: Option<u64>, attempts: Option<usize>, action: A) -> Result<R, E> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's put the defaults into the client module as constants and make them configurable using |
||||||
where | ||||||
A: FnMut() -> F, | ||||||
E: std::fmt::Display, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bound probably isn't necessary. The tokio-retry crate does not require the error type to implement |
||||||
F: Future<Output=Result<R, E>> | ||||||
{ | ||||||
let attempts = if let Some(attempts) = attempts { attempts } else { 10 }; | ||||||
let ms = if let Some(ms) = ms { ms } else { 1000 /*1 sec */}; | ||||||
|
||||||
tokio_retry::Retry::spawn(retry_strategy(ms, attempts), action).await | ||||||
} |
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.
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.
The function itself probably isn't necessary. It's very simple and it's used only once. It only bloats the code. I'd put the contents directly into
with_retry
.