use std::time::Duration;
#[derive(Clone, Debug)]
pub struct ReconnectOptions {
pub(crate) retry_initial: bool,
pub(crate) reconnect: bool,
pub(crate) delay: Duration,
pub(crate) backoff_factor: u32,
pub(crate) delay_max: Duration,
}
impl ReconnectOptions {
pub fn reconnect(reconnect: bool) -> ReconnectOptionsBuilder {
ReconnectOptionsBuilder::new(reconnect)
}
}
impl Default for ReconnectOptions {
fn default() -> ReconnectOptions {
ReconnectOptions {
retry_initial: false,
reconnect: true,
delay: Duration::from_secs(1),
backoff_factor: 2,
delay_max: Duration::from_secs(60),
}
}
}
pub struct ReconnectOptionsBuilder {
opts: ReconnectOptions,
}
impl ReconnectOptionsBuilder {
pub fn new(reconnect: bool) -> Self {
let opts = ReconnectOptions {
reconnect,
..Default::default()
};
Self { opts }
}
pub fn retry_initial(mut self, retry: bool) -> Self {
self.opts.retry_initial = retry;
self
}
pub fn delay(mut self, delay: Duration) -> Self {
self.opts.delay = delay;
self
}
pub fn backoff_factor(mut self, factor: u32) -> Self {
self.opts.backoff_factor = factor;
self
}
pub fn delay_max(mut self, max: Duration) -> Self {
self.opts.delay_max = max;
self
}
pub fn build(self) -> ReconnectOptions {
self.opts
}
}