-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce retry blocks for initialization tasks
- Loading branch information
Showing
7 changed files
with
134 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::time::Duration; | ||
|
||
/// Global maximum span delay for a retry. | ||
const GLOBAL_MAX: Duration = Duration::from_secs(120); | ||
|
||
/// An exponential backoff. | ||
pub struct Exponential { | ||
initial: Duration, | ||
attempt: usize, | ||
} | ||
|
||
impl Exponential { | ||
/// Construct a new exponential backoff. | ||
pub fn new(initial: Duration) -> Self { | ||
Self { | ||
initial, | ||
attempt: 0, | ||
} | ||
} | ||
|
||
/// Get the next duration and increment the attempt counter. | ||
pub fn next(&mut self) -> Duration { | ||
let mut duration = self.initial; | ||
|
||
if self.attempt <= 4 { | ||
duration *= 2u32 << usize::min(self.attempt, 4); | ||
|
||
if duration > GLOBAL_MAX { | ||
duration = GLOBAL_MAX; | ||
} | ||
} else { | ||
duration = GLOBAL_MAX; | ||
} | ||
|
||
self.attempt += 1; | ||
duration | ||
} | ||
} |
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
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