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
mod ip_extractor;
pub mod ip_hash;
pub mod round_robin;
use super::worker::WorkerConfig;
use crate::client::Client;
use crate::error::FaucetResult;
use crate::leak;
use hyper::Request;
pub use ip_extractor::IpExtractor;
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;

use self::ip_hash::IpHash;
use self::round_robin::RoundRobin;

trait LoadBalancingStrategy {
    async fn entry(&self, ip: IpAddr) -> Client;
}

#[derive(Debug, Clone, Copy, clap::ValueEnum, Eq, PartialEq, serde::Deserialize)]
#[serde(rename = "snake_case")]
pub enum Strategy {
    RoundRobin,
    IpHash,
}

impl FromStr for Strategy {
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "round_robin" => Ok(Self::RoundRobin),
            "ip_hash" => Ok(Self::IpHash),
            _ => Err("invalid strategy"),
        }
    }
}

#[derive(Copy, Clone)]
enum DynLoadBalancer {
    IpHash(&'static ip_hash::IpHash),
    RoundRobin(&'static round_robin::RoundRobin),
}

impl LoadBalancingStrategy for DynLoadBalancer {
    async fn entry(&self, ip: IpAddr) -> Client {
        match self {
            DynLoadBalancer::RoundRobin(rr) => rr.entry(ip).await,
            DynLoadBalancer::IpHash(ih) => ih.entry(ip).await,
        }
    }
}

pub(crate) struct LoadBalancer {
    strategy: DynLoadBalancer,
    extractor: IpExtractor,
}

impl LoadBalancer {
    pub fn new(
        strategy: Strategy,
        extractor: IpExtractor,
        workers: &[WorkerConfig],
    ) -> FaucetResult<Self> {
        let strategy: DynLoadBalancer = match strategy {
            Strategy::RoundRobin => DynLoadBalancer::RoundRobin(leak!(RoundRobin::new(workers)?)),
            Strategy::IpHash => DynLoadBalancer::IpHash(leak!(IpHash::new(workers)?)),
        };
        Ok(Self {
            strategy,
            extractor,
        })
    }
    pub async fn get_client(&self, ip: IpAddr) -> FaucetResult<Client> {
        Ok(self.strategy.entry(ip).await)
    }
    pub fn extract_ip<B>(
        &self,
        request: &Request<B>,
        socket: Option<IpAddr>,
    ) -> FaucetResult<IpAddr> {
        self.extractor.extract(request, socket)
    }
}

impl Clone for LoadBalancer {
    fn clone(&self) -> Self {
        Self {
            strategy: self.strategy,
            extractor: self.extractor,
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_strategy_from_str() {
        assert_eq!(
            Strategy::from_str("round_robin").unwrap(),
            Strategy::RoundRobin
        );
        assert_eq!(Strategy::from_str("ip_hash").unwrap(), Strategy::IpHash);
        assert!(Strategy::from_str("invalid").is_err());
    }

    #[test]
    fn test_load_balancer_new_round_robin() {
        let configs = Vec::new();
        let _ = LoadBalancer::new(Strategy::RoundRobin, IpExtractor::XForwardedFor, &configs)
            .expect("failed to create load balancer");
    }

    #[test]
    fn test_load_balancer_new_ip_hash() {
        let configs = Vec::new();
        let _ = LoadBalancer::new(Strategy::IpHash, IpExtractor::XForwardedFor, &configs)
            .expect("failed to create load balancer");
    }

    #[test]
    fn test_load_balancer_extract_ip() {
        let configs = Vec::new();
        let load_balancer =
            LoadBalancer::new(Strategy::RoundRobin, IpExtractor::XForwardedFor, &configs)
                .expect("failed to create load balancer");
        let request = Request::builder()
            .header("x-forwarded-for", "192.168.0.1")
            .body(())
            .unwrap();
        let ip = load_balancer
            .extract_ip(&request, Some("127.0.0.1".parse().unwrap()))
            .expect("failed to extract ip");

        assert_eq!(ip, "192.168.0.1".parse::<IpAddr>().unwrap());
    }

    #[tokio::test]
    async fn test_load_balancer_get_client() {
        use crate::client::ExtractSocketAddr;
        let configs = [
            WorkerConfig::dummy("test", "127.0.0.1:9999", true),
            WorkerConfig::dummy("test", "127.0.0.1:9998", true),
        ];
        let load_balancer =
            LoadBalancer::new(Strategy::RoundRobin, IpExtractor::XForwardedFor, &configs)
                .expect("failed to create load balancer");
        let ip = "192.168.0.1".parse().unwrap();
        let client = load_balancer
            .get_client(ip)
            .await
            .expect("failed to get client");
        assert_eq!(client.socket_addr(), "127.0.0.1:9999".parse().unwrap());

        let client = load_balancer
            .get_client(ip)
            .await
            .expect("failed to get client");

        assert_eq!(client.socket_addr(), "127.0.0.1:9998".parse().unwrap());
    }

    #[test]
    fn test_clone_load_balancer() {
        let configs = Vec::new();
        let load_balancer =
            LoadBalancer::new(Strategy::RoundRobin, IpExtractor::XForwardedFor, &configs)
                .expect("failed to create load balancer");
        let _ = load_balancer.clone();
    }
}