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
pub mod ip_hash;
pub use ip_hash::IpHash;
pub mod random;
pub use random::Random;

pub struct Balancer<P, S> {
    pub policy: P,
    pub instances: Vec<S>,
    pub fallback: S,
}

impl<P, S> Balancer<P, S> {
    pub fn new(policy: P, instances: Vec<S>, fallback: S) -> Self {
        Self { policy, instances, fallback }
    }
}

pub trait BalancePolicy<S, R> {
    fn pick<'s>(&self, instances: &'s [S], req: &R) -> Option<&'s S>;
}

impl<P, R, S> hyper::service::Service<R> for Balancer<P, S>
where
    P: BalancePolicy<S, R>,
    S: hyper::service::Service<R>,
    S::Future: std::marker::Send,
{
    type Response = S::Response;

    type Error = S::Error;

    type Future = S::Future;

    fn call(&self, req: R) -> Self::Future {
        self.policy.pick(&self.instances, &req).unwrap_or(&self.fallback).call(req)
    }
}