diff --git a/light-client/src/backoff_decorator.rs b/light-client/src/backoff_decorator.rs index 8000b74..e4d1906 100644 --- a/light-client/src/backoff_decorator.rs +++ b/light-client/src/backoff_decorator.rs @@ -1,5 +1,3 @@ -use std::time::Duration as StdDuration; - use async_trait::async_trait; use chrono::{DateTime, Duration, Utc}; use log::{info, warn}; @@ -7,6 +5,30 @@ use tokio::sync::Mutex; use crate::{LightClient, LightClientError}; +#[derive(Clone, Copy, Debug)] +pub struct BackoffConfig { + pub start_delay: Duration, + pub max_delay: Duration, +} + +impl Default for BackoffConfig { + fn default() -> Self { + Self { + start_delay: Duration::milliseconds(125), + max_delay: Duration::seconds(1), + } + } +} + +impl BackoffConfig { + pub fn slow() -> Self { + Self { + start_delay: Duration::seconds(1), + max_delay: Duration::seconds(8), + } + } +} + #[derive(PartialEq)] enum ConnectionStatus { Healthy, @@ -24,40 +46,22 @@ pub struct BackoffDecorator { inner: T, start_delay: Duration, max_delay: Duration, - timeout: StdDuration, state: Mutex, } impl BackoffDecorator { - pub fn new(light_client: T) -> BackoffDecorator { - let default_start_delay = Duration::seconds(1); + pub fn new(light_client: T, config: BackoffConfig) -> BackoffDecorator { Self { inner: light_client, - start_delay: default_start_delay, - max_delay: Duration::seconds(8), - timeout: StdDuration::from_millis(100), + start_delay: config.start_delay, + max_delay: config.max_delay, state: Mutex::new(BackoffState { status: ConnectionStatus::Healthy, - delay: default_start_delay, + delay: config.start_delay, next_check: Utc::now(), }), } } - - pub fn with_start_delay(mut self, delay: Duration) -> Self { - self.start_delay = delay; - self - } - - pub fn with_max_delay(mut self, delay: Duration) -> Self { - self.max_delay = delay; - self - } - - pub fn with_timeout(mut self, timeout: StdDuration) -> Self { - self.timeout = timeout; - self - } } #[async_trait] @@ -112,7 +116,15 @@ where } pub trait WithBackoff: Sized + LightClient { - fn with_backoff(self) -> BackoffDecorator { - BackoffDecorator::new(self) + fn with_backoff(self, config: BackoffConfig) -> BackoffDecorator { + BackoffDecorator::new(self, config) + } + + fn with_default_backoff(self) -> BackoffDecorator { + Self::with_backoff(self, Default::default()) + } + + fn with_slow_backoff(self) -> BackoffDecorator { + Self::with_backoff(self, BackoffConfig::slow()) } } diff --git a/light-client/src/combined.rs b/light-client/src/combined.rs index 67e5e7a..ec360fa 100644 --- a/light-client/src/combined.rs +++ b/light-client/src/combined.rs @@ -98,7 +98,7 @@ impl CombinedLightClientBuilder { self.with( HttpLightClient::new(url.as_ref()) .with_byte_order(byte_order) - .with_backoff(), + .with_slow_backoff(), ) } @@ -107,7 +107,7 @@ impl CombinedLightClientBuilder { self.with( TcpLightClient::new(url.as_ref()) .with_byte_order(byte_order) - .with_backoff(), + .with_default_backoff(), ) } @@ -116,7 +116,7 @@ impl CombinedLightClientBuilder { self.with( UdpLightClient::new(url.as_ref()) .with_byte_order(byte_order) - .with_backoff(), + .with_default_backoff(), ) } @@ -125,7 +125,7 @@ impl CombinedLightClientBuilder { Ok(self.with( TtyLightClient::new()? .with_byte_order(byte_order) - .with_backoff(), + .with_default_backoff(), )) }