reqwest_lb/lb/
registry.rs

1use crate::lb::BoxLoadBalancer;
2use crate::LoadBalancerTrait;
3use std::collections::HashMap;
4use std::convert::Infallible;
5
6pub struct LoadBalancerRegistry<I, E = Infallible> {
7    registry: HashMap<String, BoxLoadBalancer<I, E>>,
8}
9
10impl<I, E> Default for LoadBalancerRegistry<I, E> {
11    fn default() -> Self {
12        Self {
13            registry: HashMap::default(),
14        }
15    }
16}
17
18impl<I, E> LoadBalancerRegistry<I, E> {
19    pub fn add<L>(&mut self, host: &str, load_balancer: L)
20    where
21        L: LoadBalancerTrait<Element = I, Error = E> + Send + Sync + 'static,
22        L::Future: Send + 'static,
23    {
24        self.registry
25            .insert(host.to_string(), load_balancer.boxed());
26    }
27
28    pub fn remove(&mut self, host: &str) {
29        self.registry.remove(host);
30    }
31
32    pub fn find(&self, host: &str) -> Option<&BoxLoadBalancer<I, E>> {
33        self.registry.get(host)
34    }
35}