forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes kube-rs#1635 Signed-off-by: Natalie Klestrup Röijezon <[email protected]>
- Loading branch information
Showing
16 changed files
with
254 additions
and
123 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
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
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 |
---|---|---|
@@ -1,86 +1,116 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use backoff::{backoff::Backoff, Clock, SystemClock}; | ||
use backon::BackoffBuilder; | ||
|
||
/// A [`Backoff`] wrapper that resets after a fixed duration has elapsed. | ||
pub struct ResetTimerBackoff<B, C = SystemClock> { | ||
backoff: B, | ||
clock: C, | ||
last_backoff: Option<Instant>, | ||
reset_duration: Duration, | ||
use super::{ResettableBackoff, ResettableBackoffWrapper}; | ||
|
||
// TODO: do we actually need this or should we just use tokio::time? | ||
pub trait Clock: Send + Sync + Unpin { | ||
fn now(&self) -> Instant; | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct TokioClock; | ||
impl Clock for TokioClock { | ||
fn now(&self) -> Instant { | ||
tokio::time::Instant::now().into_std() | ||
} | ||
} | ||
|
||
impl<B: Backoff> ResetTimerBackoff<B> { | ||
pub fn new(backoff: B, reset_duration: Duration) -> Self { | ||
Self::new_with_custom_clock(backoff, reset_duration, SystemClock {}) | ||
impl<B: BackoffBuilder> ResetTimerBackoffBuilder<B> { | ||
pub fn new(inner_backoff_builder: B, reset_duration: Duration) -> Self { | ||
Self::new_with_custom_clock(inner_backoff_builder, reset_duration, TokioClock) | ||
} | ||
} | ||
|
||
impl<B: Backoff, C: Clock> ResetTimerBackoff<B, C> { | ||
fn new_with_custom_clock(backoff: B, reset_duration: Duration, clock: C) -> Self { | ||
impl<B: BackoffBuilder, C: Clock> ResetTimerBackoffBuilder<B, C> { | ||
fn new_with_custom_clock(inner_backoff_builder: B, reset_duration: Duration, clock: C) -> Self { | ||
Self { | ||
backoff, | ||
inner_backoff_builder, | ||
clock, | ||
last_backoff: None, | ||
reset_duration, | ||
} | ||
} | ||
} | ||
|
||
impl<B: Backoff, C: Clock> Backoff for ResetTimerBackoff<B, C> { | ||
fn next_backoff(&mut self) -> Option<Duration> { | ||
/// A [`Backoff`] wrapper that resets after a fixed duration has elapsed. | ||
#[derive(Debug, Clone)] | ||
pub struct ResetTimerBackoffBuilder<B, C = TokioClock> { | ||
inner_backoff_builder: B, | ||
clock: C, | ||
reset_duration: Duration, | ||
} | ||
|
||
impl<B: BackoffBuilder + Clone, C: Clock> BackoffBuilder for ResetTimerBackoffBuilder<B, C> { | ||
type Backoff = ResetTimerBackoff<ResettableBackoffWrapper<B>, C>; | ||
|
||
fn build(self) -> Self::Backoff { | ||
ResetTimerBackoff { | ||
inner_backoff: ResettableBackoffWrapper::new(self.inner_backoff_builder), | ||
clock: self.clock, | ||
reset_duration: self.reset_duration, | ||
last_backoff: None, | ||
} | ||
} | ||
} | ||
|
||
/// Constructed by [`ResetTimerBackoffBuilder`]. | ||
#[derive(Debug)] | ||
pub struct ResetTimerBackoff<B, C = TokioClock> { | ||
inner_backoff: B, | ||
clock: C, | ||
reset_duration: Duration, | ||
last_backoff: Option<Instant>, | ||
} | ||
|
||
// impl Backoff, which is now effectively an alias for Iterator<Item = Duration> | ||
impl<B: ResettableBackoff, C: Clock> Iterator for ResetTimerBackoff<B, C> { | ||
type Item = Duration; | ||
|
||
fn next(&mut self) -> Option<Duration> { | ||
if let Some(last_backoff) = self.last_backoff { | ||
if self.clock.now() > last_backoff + self.reset_duration { | ||
tracing::debug!( | ||
?last_backoff, | ||
reset_duration = ?self.reset_duration, | ||
"Resetting backoff, since reset duration has expired" | ||
); | ||
self.backoff.reset(); | ||
self.inner_backoff.reset(); | ||
} | ||
} | ||
self.last_backoff = Some(self.clock.now()); | ||
self.backoff.next_backoff() | ||
} | ||
|
||
fn reset(&mut self) { | ||
// Do not even bother trying to reset here, since `next_backoff` will take care of this when the timer expires. | ||
self.inner_backoff.next() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use backoff::{backoff::Backoff, Clock}; | ||
use backon::BackoffBuilder; | ||
use tokio::time::advance; | ||
|
||
use super::ResetTimerBackoff; | ||
use crate::utils::stream_backoff::tests::LinearBackoff; | ||
use std::time::{Duration, Instant}; | ||
use crate::utils::{ | ||
backoff_reset_timer::TokioClock, stream_backoff::tests::LinearBackoffBuilder, | ||
ResetTimerBackoffBuilder, | ||
}; | ||
use std::time::Duration; | ||
|
||
#[tokio::test] | ||
async fn should_reset_when_timer_expires() { | ||
tokio::time::pause(); | ||
let mut backoff = ResetTimerBackoff::new_with_custom_clock( | ||
LinearBackoff::new(Duration::from_secs(2)), | ||
let mut backoff = ResetTimerBackoffBuilder::new_with_custom_clock( | ||
LinearBackoffBuilder::new(Duration::from_secs(2)), | ||
Duration::from_secs(60), | ||
TokioClock, | ||
); | ||
assert_eq!(backoff.next_backoff(), Some(Duration::from_secs(2))); | ||
) | ||
.build(); | ||
assert_eq!(backoff.next(), Some(Duration::from_secs(2))); | ||
advance(Duration::from_secs(40)).await; | ||
assert_eq!(backoff.next_backoff(), Some(Duration::from_secs(4))); | ||
assert_eq!(backoff.next(), Some(Duration::from_secs(4))); | ||
advance(Duration::from_secs(40)).await; | ||
assert_eq!(backoff.next_backoff(), Some(Duration::from_secs(6))); | ||
assert_eq!(backoff.next(), Some(Duration::from_secs(6))); | ||
advance(Duration::from_secs(80)).await; | ||
assert_eq!(backoff.next_backoff(), Some(Duration::from_secs(2))); | ||
assert_eq!(backoff.next(), Some(Duration::from_secs(2))); | ||
advance(Duration::from_secs(80)).await; | ||
assert_eq!(backoff.next_backoff(), Some(Duration::from_secs(2))); | ||
} | ||
|
||
struct TokioClock; | ||
|
||
impl Clock for TokioClock { | ||
fn now(&self) -> Instant { | ||
tokio::time::Instant::now().into_std() | ||
} | ||
assert_eq!(backoff.next(), Some(Duration::from_secs(2))); | ||
} | ||
} |
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,55 @@ | ||
use std::{ops::DerefMut, time::Duration}; | ||
|
||
use backon::{Backoff, BackoffBuilder}; | ||
|
||
/// A [`Backoff`] that can also be reset. | ||
/// | ||
/// Implemented by [`ResettableBackoffWrapper`]. | ||
// Separated into a trait so that it can be used as a trait object, erasing the backing [`BackoffBuilder`]. | ||
pub trait ResettableBackoff: Backoff { | ||
fn reset(&mut self); | ||
} | ||
|
||
impl ResettableBackoff for Box<dyn ResettableBackoff + Send> { | ||
fn reset(&mut self) { | ||
Box::deref_mut(self).reset(); | ||
} | ||
} | ||
|
||
/// Implements [`ResettableBackoff`] by reconstructing the backing [`Backoff`] each time [`Self::reset`] has been called. | ||
#[derive(Debug)] | ||
pub struct ResettableBackoffWrapper<B: BackoffBuilder> { | ||
backoff_builder: B, | ||
current_backoff: Option<B::Backoff>, | ||
} | ||
|
||
impl<B: BackoffBuilder> ResettableBackoffWrapper<B> { | ||
pub fn new(backoff_builder: B) -> Self { | ||
Self { | ||
backoff_builder, | ||
current_backoff: None, | ||
} | ||
} | ||
} | ||
|
||
impl<B: BackoffBuilder + Default> Default for ResettableBackoffWrapper<B> { | ||
fn default() -> Self { | ||
Self::new(B::default()) | ||
} | ||
} | ||
|
||
impl<B: BackoffBuilder + Clone> Iterator for ResettableBackoffWrapper<B> { | ||
type Item = Duration; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.current_backoff | ||
.get_or_insert_with(|| self.backoff_builder.clone().build()) | ||
.next() | ||
} | ||
} | ||
|
||
impl<B: BackoffBuilder + Clone> ResettableBackoff for ResettableBackoffWrapper<B> { | ||
fn reset(&mut self) { | ||
self.current_backoff = None; | ||
} | ||
} |
Oops, something went wrong.