reqwest_lb/lb/
factory.rs

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
use crate::lb::{BoxLoadBalancer, LoadBalancer};
use std::collections::HashMap;
use std::convert::Infallible;

pub struct LoadBalancerFactory<I, E = Infallible> {
    registry: HashMap<String, BoxLoadBalancer<I, E>>,
}

impl<I, E> Default for LoadBalancerFactory<I, E> {
    fn default() -> Self {
        Self {
            registry: HashMap::default(),
        }
    }
}

impl<I, E> LoadBalancerFactory<I, E> {
    pub fn add<L>(&mut self, host: &str, load_balancer: L)
    where
        L: LoadBalancer<Element = I, Error = E> + Send + Sync + 'static,
        L::Future: Send + 'static,
    {
        self.registry
            .insert(host.to_string(), load_balancer.boxed());
    }

    pub fn remove(&mut self, host: &str) {
        self.registry.remove(host);
    }

    pub fn get(&self, host: &str) -> Option<&BoxLoadBalancer<I, E>> {
        self.registry.get(host)
    }
}