faucet_server/client/load_balancing/
round_robin.rs

1use super::LoadBalancingStrategy;
2use crate::{
3    client::{worker::WorkerConfig, Client},
4    error::FaucetResult,
5};
6use std::{net::IpAddr, sync::atomic::AtomicUsize};
7
8struct Targets {
9    targets: &'static [Client],
10    index: AtomicUsize,
11}
12
13// 500us is the time it takes for the round robin to move to the next target
14// in the unlikely event that the target is offline
15const WAIT_TIME_UNTIL_RETRY: std::time::Duration = std::time::Duration::from_micros(500);
16
17impl Targets {
18    fn new(configs: &[WorkerConfig]) -> FaucetResult<Self> {
19        let mut targets = Vec::new();
20        for state in configs {
21            let client = Client::builder(*state).build()?;
22            targets.push(client);
23        }
24        let targets = Box::leak(targets.into_boxed_slice());
25        Ok(Targets {
26            targets,
27            index: AtomicUsize::new(0),
28        })
29    }
30    fn next(&self) -> Client {
31        let index = self.index.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
32        self.targets[index % self.targets.len()].clone()
33    }
34}
35
36pub struct RoundRobin {
37    targets: Targets,
38}
39
40impl RoundRobin {
41    pub(crate) fn new(targets: &[WorkerConfig]) -> FaucetResult<Self> {
42        Ok(Self {
43            targets: Targets::new(targets)?,
44        })
45    }
46}
47
48impl LoadBalancingStrategy for RoundRobin {
49    type Input = IpAddr;
50    async fn entry(&self, _ip: IpAddr) -> Client {
51        let mut client = self.targets.next();
52        loop {
53            if client.is_online() {
54                break client;
55            }
56            tokio::time::sleep(WAIT_TIME_UNTIL_RETRY).await;
57            client = self.targets.next();
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64
65    use super::*;
66
67    #[test]
68    fn test_new_targets() {
69        let configs = (0..3)
70            .map(|i| WorkerConfig::dummy("test", &format!("127.0.0.1:900{i}"), true))
71            .collect::<Vec<_>>();
72
73        let _ = Targets::new(&configs).expect("failed to create targets");
74    }
75
76    #[test]
77    fn test_new_round_robin() {
78        let configs = (0..3)
79            .map(|i| WorkerConfig::dummy("test", &format!("127.0.0.1:900{i}"), true))
80            .collect::<Vec<_>>();
81
82        let _ = RoundRobin::new(&configs).expect("failed to create round robin");
83    }
84
85    #[tokio::test]
86    async fn test_round_robin_entry() {
87        use crate::client::ExtractSocketAddr;
88
89        let configs = (0..3)
90            .map(|i| WorkerConfig::dummy("test", &format!("127.0.0.1:900{i}"), true))
91            .collect::<Vec<_>>();
92
93        let rr = RoundRobin::new(&configs).expect("failed to create round robin");
94
95        let ip = "0.0.0.0".parse().expect("failed to parse ip");
96
97        assert_eq!(rr.entry(ip).await.socket_addr(), configs[0].addr);
98        assert_eq!(rr.entry(ip).await.socket_addr(), configs[1].addr);
99        assert_eq!(rr.entry(ip).await.socket_addr(), configs[2].addr);
100        assert_eq!(rr.entry(ip).await.socket_addr(), configs[0].addr);
101        assert_eq!(rr.entry(ip).await.socket_addr(), configs[1].addr);
102        assert_eq!(rr.entry(ip).await.socket_addr(), configs[2].addr);
103    }
104
105    #[tokio::test]
106    async fn test_round_robin_entry_with_offline_target() {
107        use crate::client::ExtractSocketAddr;
108
109        let configs = [
110            WorkerConfig::dummy("test", "127.0.0.1:9000", false),
111            WorkerConfig::dummy("test", "127.0.0.1:9001", false),
112            WorkerConfig::dummy("test", "127.0.0.1:9002", true),
113        ];
114
115        let rr = RoundRobin::new(&configs).expect("failed to create round robin");
116
117        let ip = "0.0.0.0".parse().expect("failed to parse ip");
118
119        assert_eq!(rr.entry(ip).await.socket_addr(), configs[2].addr);
120    }
121}