reqwest_proxy_pool/
config.rs1use std::time::Duration;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum ProxySelectionStrategy {
8 FastestResponse,
10 MostReliable,
12 Random,
14 RoundRobin,
16}
17
18#[derive(Debug, Clone)]
20pub struct ProxyPoolConfig {
21 pub sources: Vec<String>,
23 pub health_check_interval: Duration,
25 pub health_check_timeout: Duration,
27 pub min_available_proxies: usize,
29 pub health_check_url: String,
31 pub retry_count: usize,
33 pub selection_strategy: ProxySelectionStrategy,
35 pub max_requests_per_second: f64,
37}
38
39impl ProxyPoolConfig {
40 pub fn builder() -> ProxyPoolConfigBuilder {
42 ProxyPoolConfigBuilder::new()
43 }
44}
45
46pub 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 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 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 pub fn health_check_interval(mut self, interval: Duration) -> Self {
81 self.health_check_interval = Some(interval);
82 self
83 }
84
85 pub fn health_check_timeout(mut self, timeout: Duration) -> Self {
87 self.health_check_timeout = Some(timeout);
88 self
89 }
90
91 pub fn min_available_proxies(mut self, count: usize) -> Self {
93 self.min_available_proxies = Some(count);
94 self
95 }
96
97 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 pub fn retry_count(mut self, count: usize) -> Self {
105 self.retry_count = Some(count);
106 self
107 }
108
109 pub fn selection_strategy(mut self, strategy: ProxySelectionStrategy) -> Self {
111 self.selection_strategy = Some(strategy);
112 self
113 }
114
115 pub fn max_requests_per_second(mut self, rps: f64) -> Self {
117 self.max_requests_per_second = Some(rps);
118 self
119 }
120
121 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}