-
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 20 times at max
If HTTP client fails to download, retry to download once in 1000 msec, up to 20 times.
- Loading branch information
1 parent
47d3643
commit ee93fd6
Showing
3 changed files
with
44 additions
and
3 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,25 @@ | ||
use core::time::Duration; | ||
use std::thread::sleep; | ||
|
||
const RETRY_INTERVAL_MSEC: u64 = 1000; | ||
|
||
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; | ||
} | ||
sleep(Duration::from_millis(RETRY_INTERVAL_MSEC)); | ||
} | ||
} | ||
} | ||
} |