-
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.
Merge pull request #42 from flatcar/dongsu/client-timeout-retry
download_sysext: HTTP client timeout and retry
- Loading branch information
Showing
4 changed files
with
49 additions
and
4 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
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)); | ||
} | ||
} | ||
} | ||
} |