-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use cool down period if TCP accept fails
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::cmp; | ||
use std::time::Duration; | ||
|
||
/// Simple exponential backoff | ||
pub struct ExponentialBackoff { | ||
start_delay: Duration, | ||
max_delay: Duration, | ||
current_delay: Duration, | ||
} | ||
|
||
impl ExponentialBackoff { | ||
/// Creates a new exponential backoff instance starting with delay | ||
/// `start_delay` and maxing out at `max_delay`. | ||
pub fn new(start_delay: Duration, max_delay: Duration) -> Self { | ||
Self { | ||
start_delay, | ||
max_delay, | ||
current_delay: start_delay, | ||
} | ||
} | ||
|
||
/// Resets the exponential backoff so that the next delay is the start delay again. | ||
pub fn reset(&mut self) { | ||
self.current_delay = self.start_delay; | ||
} | ||
|
||
/// Returns the next delay. This is twice as long as last returned delay, | ||
/// up until `max_delay` is reached. | ||
pub fn next_delay(&mut self) -> Duration { | ||
let delay = self.current_delay; | ||
let next_cooldown = self.current_delay.mul_f64(2.0); | ||
self.current_delay = cmp::min(next_cooldown, self.max_delay); | ||
delay | ||
} | ||
} |
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