1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
mod direct;
mod feedback;
mod ip;
mod never_empty_handed;
mod shuffled;
mod subnet;
use super::super::regions::{DomainWithPort, IpAddrWithPort};
use auto_impl::auto_impl;
use dyn_clonable::clonable;
pub use feedback::ChooserFeedback;
use std::{
fmt::Debug,
ops::{Deref, DerefMut},
};
#[cfg(feature = "async")]
use futures::future::BoxFuture;
#[clonable]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait Chooser: Clone + Debug + Sync + Send {
fn choose(&self, ips: &[IpAddrWithPort], opts: ChooseOptions) -> ChosenResults;
fn feedback(&self, feedback: ChooserFeedback);
#[inline]
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
fn async_choose<'a>(&'a self, ips: &'a [IpAddrWithPort], opts: ChooseOptions<'a>) -> BoxFuture<'a, ChosenResults> {
Box::pin(async move { self.choose(ips, opts) })
}
#[inline]
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
fn async_feedback<'a>(&'a self, feedback: ChooserFeedback<'a>) -> BoxFuture<'a, ()> {
Box::pin(async move { self.feedback(feedback) })
}
}
#[derive(Debug, Copy, Clone, Default)]
pub struct ChooseOptions<'a> {
domain: Option<&'a DomainWithPort>,
}
impl<'a> ChooseOptions<'a> {
#[inline]
pub fn domain(&'a self) -> Option<&'a DomainWithPort> {
self.domain
}
#[inline]
pub fn builder() -> ChooseOptionsBuilder<'a> {
ChooseOptionsBuilder::new()
}
}
#[derive(Debug, Clone, Default)]
pub struct ChooseOptionsBuilder<'a>(ChooseOptions<'a>);
impl<'a> ChooseOptionsBuilder<'a> {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn domain(&mut self, domain: &'a DomainWithPort) -> &mut Self {
self.0.domain = Some(domain);
self
}
#[inline]
pub fn build(&self) -> ChooseOptions<'a> {
self.0
}
}
#[derive(Debug)]
pub struct ChosenResults(Vec<IpAddrWithPort>);
impl ChosenResults {
#[inline]
pub fn ip_addrs(&self) -> &[IpAddrWithPort] {
&self.0
}
#[inline]
pub fn ip_addrs_mut(&mut self) -> &mut Vec<IpAddrWithPort> {
&mut self.0
}
#[inline]
pub fn into_ip_addrs(self) -> Vec<IpAddrWithPort> {
self.0
}
}
impl From<Vec<IpAddrWithPort>> for ChosenResults {
#[inline]
fn from(ip_addrs: Vec<IpAddrWithPort>) -> Self {
Self(ip_addrs)
}
}
impl FromIterator<IpAddrWithPort> for ChosenResults {
#[inline]
fn from_iter<T: IntoIterator<Item = IpAddrWithPort>>(iter: T) -> Self {
Self(Vec::from_iter(iter))
}
}
impl<'a> IntoIterator for &'a ChosenResults {
type Item = &'a IpAddrWithPort;
type IntoIter = std::slice::Iter<'a, IpAddrWithPort>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl IntoIterator for ChosenResults {
type Item = IpAddrWithPort;
type IntoIter = std::vec::IntoIter<IpAddrWithPort>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<ChosenResults> for Vec<IpAddrWithPort> {
#[inline]
fn from(answers: ChosenResults) -> Self {
answers.0
}
}
impl AsRef<[IpAddrWithPort]> for ChosenResults {
#[inline]
fn as_ref(&self) -> &[IpAddrWithPort] {
&self.0
}
}
impl AsMut<[IpAddrWithPort]> for ChosenResults {
#[inline]
fn as_mut(&mut self) -> &mut [IpAddrWithPort] {
&mut self.0
}
}
impl Deref for ChosenResults {
type Target = [IpAddrWithPort];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ChosenResults {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub use direct::DirectChooser;
pub use ip::{IpChooser, IpChooserBuilder};
pub use never_empty_handed::NeverEmptyHandedChooser;
pub use shuffled::ShuffledChooser;
pub use subnet::{SubnetChooser, SubnetChooserBuilder};