net_pool/
backend.rs

1use std::cmp::Ordering;
2use std::hash::{Hash, Hasher};
3use std::net::{AddrParseError, IpAddr, SocketAddr, SocketAddrV4, SocketAddrV6};
4use std::str::FromStr;
5
6#[derive(Clone, Debug)]
7pub enum Address {
8    Ori(String),
9    Addr(SocketAddr),
10}
11
12impl Address {
13    pub fn hash_code(&self) -> u64 {
14        match self {
15            Address::Ori(ori) => crate::utils::bytes_to_hash_code(ori.as_bytes()),
16            Address::Addr(addr) => crate::utils::socketaddr_to_hash_code(addr),
17        }
18    }
19}
20
21impl From<String> for Address {
22    fn from(ori: String) -> Self {
23        Address::Ori(ori)
24    }
25}
26
27impl From<&str> for Address {
28    fn from(ori: &str) -> Self {
29        Address::Ori(ori.to_string())
30    }
31}
32
33impl From<SocketAddr> for Address {
34    fn from(addr: SocketAddr) -> Self {
35        Address::Addr(addr)
36    }
37}
38
39impl<I: Into<IpAddr>> From<(I, u16)> for Address {
40    fn from(value: (I, u16)) -> Self {
41        Address::from(SocketAddr::from(value))
42    }
43}
44
45impl From<SocketAddrV4> for Address {
46    fn from(value: SocketAddrV4) -> Self {
47        Address::from(SocketAddr::from(value))
48    }
49}
50
51impl From<SocketAddrV6> for Address {
52    fn from(value: SocketAddrV6) -> Self {
53        Address::from(SocketAddr::from(value))
54    }
55}
56
57impl FromStr for Address {
58    type Err = AddrParseError;
59
60    fn from_str(s: &str) -> Result<Self, Self::Err> {
61        let a = SocketAddr::from_str(s)?;
62        Ok(Address::from(a))
63    }
64}
65
66#[derive(Clone, Debug)]
67pub struct BackendState {
68    id: Option<u32>,
69    address: Address,
70    hash_code: u64,
71}
72
73impl BackendState {
74    pub fn new(id: Option<u32>, addr: Address) -> BackendState {
75        let hash_code = addr.hash_code();
76        BackendState {
77            id,
78            address: addr,
79            hash_code,
80        }
81    }
82
83    pub fn id(&self) -> Option<u32> {
84        self.id.clone()
85    }
86
87    pub fn hash_code(&self) -> u64 {
88        self.hash_code
89    }
90
91    pub fn get_address(&self) -> &Address {
92        &self.address
93    }
94}
95
96impl Hash for BackendState {
97    fn hash<H: Hasher>(&self, state: &mut H) {
98        state.write_u64(self.hash_code)
99    }
100}
101
102impl Eq for BackendState {}
103
104impl PartialEq for BackendState {
105    fn eq(&self, other: &Self) -> bool {
106        self.hash_code.eq(&other.hash_code)
107    }
108}
109
110impl PartialOrd for BackendState {
111    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
112        self.hash_code.partial_cmp(&other.hash_code)
113    }
114}
115
116impl Ord for BackendState {
117    fn cmp(&self, other: &Self) -> Ordering {
118        self.hash_code.cmp(&other.hash_code)
119    }
120}
121
122impl From<Address> for BackendState {
123    fn from(addr: Address) -> Self {
124        BackendState::new(None, addr)
125    }
126}
127
128#[derive(Default)]
129pub(crate) struct BackendStates(Vec<BackendState>);
130
131impl BackendStates {
132    pub fn new() -> Self {
133        BackendStates(Vec::new())
134    }
135
136    pub fn add_backend(&mut self, id: Option<u32>, addr: Address) {
137        let hash_code = addr.hash_code();
138        if self.0.iter().any(|bs| bs.hash_code == hash_code) {
139            return;
140        }
141
142        self.0.push(BackendState::new(id, addr));
143        self.sort_backends();
144    }
145
146    pub fn remove_backend(&mut self, addr: &Address) -> bool {
147        let c = self.0.len();
148        let hash_code = addr.hash_code();
149        self.0.retain(|bs| bs.hash_code != hash_code);
150        self.0.len() != c
151    }
152
153    pub fn get_backend(&self, addr: &Address) -> Option<&BackendState> {
154        let hash_code = addr.hash_code();
155        self.0.iter().find(|bs| bs.hash_code == hash_code)
156    }
157
158    #[allow(dead_code)]
159    pub fn get_backend_mut(&mut self, addr: &Address) -> Option<&mut BackendState> {
160        let hash_code = addr.hash_code();
161        self.0.iter_mut().find(|bs| bs.hash_code == hash_code)
162    }
163
164    pub fn get_backends(&self) -> &[BackendState] {
165        self.0.as_slice()
166    }
167
168    #[allow(dead_code)]
169    pub fn get_backends_mut(&mut self) -> &mut [BackendState] {
170        self.0.as_mut_slice()
171    }
172
173    fn sort_backends(&mut self) {
174        self.0.sort_by(|bs1, bs2| bs1.hash_code.cmp(&bs2.hash_code));
175    }
176}