reqwest_proxy_pool/
config.rs

1//! Configuration for the proxy pool.
2
3use std::time::Duration;
4
5/// Strategy for selecting a proxy from the pool.
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum ProxySelectionStrategy {
8    /// Select the proxy with the fastest response time.
9    FastestResponse,
10    /// Select the proxy with the highest success rate.
11    MostReliable,
12    /// Select a random healthy proxy.
13    Random,
14    /// Select proxies in round-robin fashion.
15    RoundRobin,
16}
17
18/// Configuration for the proxy pool.
19#[derive(Debug, Clone)]
20pub struct ProxyPoolConfig {
21    /// Source URLs to fetch proxy lists from.
22    pub sources: Vec<String>,
23    /// Interval between health checks.
24    pub health_check_interval: Duration,
25    /// Timeout for health checks.
26    pub health_check_timeout: Duration,
27    /// Minimum number of available proxies.
28    pub min_available_proxies: usize,
29    /// URL used for health checks.
30    pub health_check_url: String,
31    /// Number of times to retry a request with different proxies.
32    pub retry_count: usize,
33    /// Strategy for selecting proxies.
34    pub selection_strategy: ProxySelectionStrategy,
35    /// Maximum requests per second per proxy.
36    pub max_requests_per_second: f64,
37}
38
39impl ProxyPoolConfig {
40    /// Create a new configuration builder.
41    pub fn builder() -> ProxyPoolConfigBuilder {
42        ProxyPoolConfigBuilder::new()
43    }
44}
45
46/// Builder for `ProxyPoolConfig`.
47pub struct ProxyPoolConfigBuilder {
48    sources: Vec<String>,
49    health_check_interval: Option<Duration>,
50    health_check_timeout: Option<Duration>,
51    min_available_proxies: Option<usize>,
52    health_check_url: Option<String>,
53    retry_count: Option<usize>,
54    selection_strategy: Option<ProxySelectionStrategy>,
55    max_requests_per_second: Option<f64>,
56}
57
58impl ProxyPoolConfigBuilder {
59    /// Create a new builder with default values.
60    pub fn new() -> Self {
61        Self {
62            sources: Vec::new(),
63            health_check_interval: None,
64            health_check_timeout: None,
65            min_available_proxies: None,
66            health_check_url: None,
67            retry_count: None,
68            selection_strategy: None,
69            max_requests_per_second: None,
70        }
71    }
72
73    /// Set the source URLs to fetch proxy lists from.
74    pub fn sources(mut self, sources: Vec<impl Into<String>>) -> Self {
75        self.sources = sources.into_iter().map(Into::into).collect();
76        self
77    }
78
79    /// Set the interval between health checks.
80    pub fn health_check_interval(mut self, interval: Duration) -> Self {
81        self.health_check_interval = Some(interval);
82        self
83    }
84
85    /// Set the timeout for health checks.
86    pub fn health_check_timeout(mut self, timeout: Duration) -> Self {
87        self.health_check_timeout = Some(timeout);
88        self
89    }
90
91    /// Set the minimum number of available proxies.
92    pub fn min_available_proxies(mut self, count: usize) -> Self {
93        self.min_available_proxies = Some(count);
94        self
95    }
96
97    /// Set the URL used for health checks.
98    pub fn health_check_url(mut self, url: impl Into<String>) -> Self {
99        self.health_check_url = Some(url.into());
100        self
101    }
102
103    /// Set the number of times to retry a request with different proxies.
104    pub fn retry_count(mut self, count: usize) -> Self {
105        self.retry_count = Some(count);
106        self
107    }
108
109    /// Set the strategy for selecting proxies.
110    pub fn selection_strategy(mut self, strategy: ProxySelectionStrategy) -> Self {
111        self.selection_strategy = Some(strategy);
112        self
113    }
114
115    /// Set the maximum requests per second per proxy.
116    pub fn max_requests_per_second(mut self, rps: f64) -> Self {
117        self.max_requests_per_second = Some(rps);
118        self
119    }
120
121    /// Build the configuration.
122    pub fn build(self) -> ProxyPoolConfig {
123        ProxyPoolConfig {
124            sources: self.sources,
125            health_check_interval: self.health_check_interval.unwrap_or(Duration::from_secs(300)),
126            health_check_timeout: self.health_check_timeout.unwrap_or(Duration::from_secs(10)),
127            min_available_proxies: self.min_available_proxies.unwrap_or(3),
128            health_check_url: self.health_check_url.unwrap_or_else(|| "https://www.google.com".to_string()),
129            retry_count: self.retry_count.unwrap_or(3),
130            selection_strategy: self.selection_strategy.unwrap_or(ProxySelectionStrategy::FastestResponse),
131            max_requests_per_second: self.max_requests_per_second.unwrap_or(5.0),
132        }
133    }
134}
135
136impl Default for ProxyPoolConfigBuilder {
137    fn default() -> Self {
138        Self::new()
139    }
140}