| use futures::FutureExt; |
| use std::task::{Context, Poll, Waker}; |
| use std::time::Duration; |
|
|
| use futures_timer::Delay; |
|
|
| |
| pub(crate) const DEFAULT_AUTOMATIC_THROTTLE: Duration = Duration::from_millis(500); |
|
|
| #[derive(Debug)] |
| pub(crate) struct Status { |
| |
| |
| |
| interval_and_delay: Option<(Duration, Delay)>, |
|
|
| |
| |
| automatic_throttle: Option<Duration>, |
| |
| |
| |
| |
| throttle_timer: Option<ThrottleTimer>, |
|
|
| |
| |
| current_bootstrap_requests: usize, |
| |
| waker: Option<Waker>, |
| } |
|
|
| impl Status { |
| pub(crate) fn new( |
| periodic_interval: Option<Duration>, |
| automatic_throttle: Option<Duration>, |
| ) -> Self { |
| Self { |
| interval_and_delay: periodic_interval.map(|interval| (interval, Delay::new(interval))), |
| waker: None, |
| automatic_throttle, |
| throttle_timer: None, |
| current_bootstrap_requests: 0, |
| } |
| } |
|
|
| |
| pub(crate) fn trigger(&mut self) { |
| |
| |
| |
| |
| if let Some(throttle_duration) = self.automatic_throttle { |
| self.throttle_timer = Some(throttle_duration.into()); |
| } else { |
| |
| } |
|
|
| |
| if let Some(waker) = self.waker.take() { |
| waker.wake() |
| } |
| } |
|
|
| pub(crate) fn reset_timers(&mut self) { |
| |
| self.throttle_timer = None; |
|
|
| if let Some((interval, delay)) = self.interval_and_delay.as_mut() { |
| delay.reset(*interval); |
| } |
| } |
|
|
| pub(crate) fn on_started(&mut self) { |
| |
| |
| |
| self.current_bootstrap_requests += 1; |
|
|
| |
| self.reset_timers(); |
| } |
|
|
| pub(crate) fn on_finish(&mut self) { |
| if let Some(value) = self.current_bootstrap_requests.checked_sub(1) { |
| self.current_bootstrap_requests = value; |
| } else { |
| debug_assert!( |
| false, |
| "Could not decrement current_bootstrap_requests because it's already 0" |
| ); |
| } |
|
|
| |
| if let Some(waker) = self.waker.take() { |
| waker.wake(); |
| } |
| } |
|
|
| pub(crate) fn poll_next_bootstrap(&mut self, cx: &mut Context<'_>) -> Poll<()> { |
| if self.current_bootstrap_requests > 0 { |
| |
| self.waker = Some(cx.waker().clone()); |
| return Poll::Pending; |
| } |
|
|
| if let Some(throttle_delay) = &mut self.throttle_timer { |
| |
| |
| |
| |
| if throttle_delay.poll_unpin(cx).is_ready() { |
| |
| |
| return Poll::Ready(()); |
| } |
|
|
| |
| } else { |
| |
| } |
|
|
| |
| if let Some((_, delay)) = self.interval_and_delay.as_mut() { |
| if let Poll::Ready(()) = delay.poll_unpin(cx) { |
| |
| |
| return Poll::Ready(()); |
| } |
| } else { |
| |
| } |
|
|
| |
| self.waker = Some(cx.waker().clone()); |
| Poll::Pending |
| } |
|
|
| #[cfg(test)] |
| async fn next(&mut self) { |
| std::future::poll_fn(|cx| self.poll_next_bootstrap(cx)).await |
| } |
| } |
|
|
| |
| |
| |
| |
| #[derive(Debug)] |
| enum ThrottleTimer { |
| Immediate, |
| Delay(Delay), |
| } |
|
|
| impl From<Duration> for ThrottleTimer { |
| fn from(value: Duration) -> Self { |
| if value.is_zero() { |
| Self::Immediate |
| } else { |
| Self::Delay(Delay::new(value)) |
| } |
| } |
| } |
|
|
| impl futures::Future for ThrottleTimer { |
| type Output = (); |
|
|
| fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| match self.get_mut() { |
| Self::Immediate => Poll::Ready(()), |
| Self::Delay(delay) => delay.poll_unpin(cx), |
| } |
| } |
| } |
|
|
| #[cfg(test)] |
| mod tests { |
| use super::*; |
| use web_time::Instant; |
|
|
| const MS_5: Duration = Duration::from_millis(5); |
| const MS_100: Duration = Duration::from_millis(100); |
|
|
| fn do_bootstrap(status: &mut Status) { |
| status.on_started(); |
| status.on_finish(); |
| } |
|
|
| async fn await_and_do_bootstrap(status: &mut Status) { |
| status.next().await; |
| do_bootstrap(status); |
| } |
|
|
| #[async_std::test] |
| async fn immediate_automatic_bootstrap_is_triggered_immediately() { |
| let mut status = Status::new(Some(Duration::from_secs(1)), Some(Duration::ZERO)); |
|
|
| await_and_do_bootstrap(&mut status).await; |
|
|
| assert!( |
| status.next().now_or_never().is_none(), |
| "bootstrap to not be triggered immediately because periodic bootstrap is in ~1s" |
| ); |
|
|
| status.trigger(); |
| assert!( |
| status.next().now_or_never().is_some(), |
| "bootstrap to be triggered immediately because we connected to a new peer" |
| ); |
|
|
| assert!( |
| async_std::future::timeout(Duration::from_millis(500), status.next()) |
| .await |
| .is_ok(), |
| "bootstrap to be triggered in less then the configured delay because we connected to a new peer" |
| ); |
| } |
|
|
| #[async_std::test] |
| async fn delayed_automatic_bootstrap_is_triggered_before_periodic_bootstrap() { |
| let mut status = Status::new(Some(Duration::from_secs(1)), Some(MS_5)); |
|
|
| await_and_do_bootstrap(&mut status).await; |
|
|
| assert!( |
| status.next().now_or_never().is_none(), |
| "bootstrap to not be triggered immediately because periodic bootstrap is in ~1s" |
| ); |
|
|
| status.trigger(); |
| assert!( |
| status.next().now_or_never().is_none(), |
| "bootstrap to not be triggered immediately because throttle is 5ms" |
| ); |
|
|
| assert!( |
| async_std::future::timeout(MS_5 * 2, status.next()) |
| .await |
| .is_ok(), |
| "bootstrap to be triggered in less then the configured periodic delay because we connected to a new peer" |
| ); |
| } |
|
|
| #[test] |
| fn given_no_periodic_bootstrap_and_immediate_automatic_bootstrap_try_on_next_connection() { |
| let mut status = Status::new(None, Some(Duration::ZERO)); |
|
|
| |
| do_bootstrap(&mut status); |
|
|
| status.trigger(); |
|
|
| assert!( |
| status.next().now_or_never().is_some(), |
| "bootstrap to be triggered immediately because we connected to a new peer" |
| ) |
| } |
|
|
| #[async_std::test] |
| async fn given_periodic_bootstrap_when_routing_table_updated_then_wont_bootstrap_until_next_interval( |
| ) { |
| let mut status = Status::new(Some(MS_100), Some(MS_5)); |
|
|
| status.trigger(); |
|
|
| let start = Instant::now(); |
| await_and_do_bootstrap(&mut status).await; |
| let elapsed = Instant::now().duration_since(start); |
|
|
| assert!(elapsed < MS_5 * 2); |
|
|
| let start = Instant::now(); |
| await_and_do_bootstrap(&mut status).await; |
| let elapsed = Instant::now().duration_since(start); |
|
|
| assert!(elapsed > MS_100); |
| } |
|
|
| #[async_std::test] |
| async fn given_no_periodic_bootstrap_and_automatic_bootstrap_when_new_entry_then_will_bootstrap( |
| ) { |
| let mut status = Status::new(None, Some(Duration::ZERO)); |
|
|
| status.trigger(); |
|
|
| status.next().await; |
| } |
|
|
| #[async_std::test] |
| async fn given_periodic_bootstrap_and_no_automatic_bootstrap_triggers_periodically() { |
| let mut status = Status::new(Some(MS_100), None); |
|
|
| let start = Instant::now(); |
| for i in 1..6 { |
| await_and_do_bootstrap(&mut status).await; |
|
|
| let elapsed = Instant::now().duration_since(start); |
|
|
| assert!(elapsed > (i * MS_100 - Duration::from_millis(10))); |
| } |
| } |
|
|
| #[async_std::test] |
| async fn given_no_periodic_bootstrap_and_automatic_bootstrap_reset_throttle_when_multiple_peers( |
| ) { |
| let mut status = Status::new(None, Some(MS_100)); |
|
|
| status.trigger(); |
| for _ in 0..10 { |
| Delay::new(MS_100 / 2).await; |
| status.trigger(); |
| } |
| assert!( |
| status.next().now_or_never().is_none(), |
| "bootstrap to not be triggered immediately because throttle has been reset" |
| ); |
|
|
| Delay::new(MS_100 - MS_5).await; |
|
|
| assert!( |
| async_std::future::timeout(MS_5*2, status.next()) |
| .await |
| .is_ok(), |
| "bootstrap to be triggered in the configured throttle delay because we connected to a new peer" |
| ); |
| } |
|
|
| #[async_std::test] |
| async fn given_periodic_bootstrap_and_no_automatic_bootstrap_manually_triggering_prevent_periodic( |
| ) { |
| let mut status = Status::new(Some(MS_100), None); |
|
|
| status.on_started(); |
| status.on_started(); |
| status.on_finish(); |
|
|
| assert!( |
| async_std::future::timeout(10 * MS_100, status.next()) |
| .await |
| .is_err(), |
| "periodic bootstrap to never be triggered because one is still being run" |
| ); |
|
|
| status.on_finish(); |
|
|
| assert!( |
| status.next().now_or_never().is_some(), |
| "bootstrap to be triggered immediately because no more bootstrap requests are running" |
| ) |
| } |
| } |
|
|