-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
download_sysext: retry to download 10 times at max
If HTTP client fails to download, retry to download once in 500 msec, up to 10 times.
- Loading branch information
1 parent
f75d089
commit 278002f
Showing
3 changed files
with
38 additions
and
6 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
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,23 @@ | ||
use tokio::runtime::Handle; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
pub fn retry_loop<F, T, E>(mut func: F, max_tries: u32) -> Result<T, E> | ||
where | ||
F: FnMut() -> Result<T, E>, | ||
{ | ||
let mut tries = 0; | ||
|
||
loop { | ||
match func() { | ||
ok @ Ok(_) => return ok, | ||
err @ Err(_) => { | ||
tries += 1; | ||
|
||
if tries >= max_tries { | ||
return err; | ||
} | ||
Handle::current().block_on(sleep(Duration::from_millis(500))); | ||
} | ||
} | ||
} | ||
} |