bililive_core/retry/
config.rs

1use std::fmt::{Debug, Formatter, Result as FmtResult};
2use std::time::Duration;
3
4use stream_reconnect::ReconnectOptions;
5
6use super::policy::BEBIterator;
7
8/// The configuration for retry behavior.
9#[derive(Clone)]
10pub struct RetryConfig(ReconnectOptions);
11
12impl RetryConfig {
13    /// Create a retry configuration with given `duration_generator`.
14    ///
15    /// `duration_generator` is a function that returns a duration iterator.
16    /// Each item yielded by the iterator indicates the delay time before next connection attempt after a disconnection occurs.
17    /// If `None` is returned, the stream fails.
18    ///
19    /// The `default` implementation uses [`BEBIterator`](BEBIterator).
20    pub fn new<F, I, IN>(duration_generator: F) -> Self
21    where
22        F: 'static + Send + Sync + Fn() -> IN,
23        I: 'static + Send + Sync + Iterator<Item = Duration>,
24        IN: IntoIterator<IntoIter = I, Item = Duration>,
25    {
26        Self(ReconnectOptions::new().with_retries_generator(duration_generator))
27    }
28}
29
30impl From<RetryConfig> for ReconnectOptions {
31    fn from(o: RetryConfig) -> Self {
32        o.0
33    }
34}
35
36impl Debug for RetryConfig {
37    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
38        f.debug_tuple("RetryConfig")
39            .field(&"<ReconnectOptions>")
40            .finish()
41    }
42}
43
44impl Default for RetryConfig {
45    fn default() -> Self {
46        Self::new(BEBIterator::default)
47    }
48}