reqwest_lb/lb/
mod.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
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
use futures::future::BoxFuture;
use http::Extensions;
use std::fmt::Debug;
use std::future::Future;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;

mod factory;
mod policy;
mod weight;

pub use factory::LoadBalancerFactory;
pub use policy::{LoadBalancerPolicy, LoadBalancerPolicyTrait};
pub use weight::WeightProvider;

pub type BoxLoadBalancer<I, E> = Box<
    dyn LoadBalancer<Element=I, Error=E, Future=BoxFuture<'static, Result<Option<I>, E>>>
    + Send
    + Sync,
>;

pub trait LoadBalancer {
    ///
    /// load balancer element type
    ///
    type Element;

    ///
    /// load balancer choose element maybe error type
    ///
    type Error;

    ///
    /// load balancer choose element future type
    ///
    type Future: Future<Output=Result<Option<Self::Element>, Self::Error>>;

    ///
    /// load balancer choose a effect element
    ///
    fn choose(&self, extensions: &mut Extensions) -> Self::Future;

    ///
    /// Wrap to boxed load balancer
    ///
    fn boxed(self) -> BoxLoadBalancer<Self::Element, Self::Error>
    where
        Self: Sized + Send + Sync + 'static,
        Self::Future: Send + 'static,
    {
        Box::new(BoxFutureLoadBalancer::new(self))
    }
}

struct BoxFutureLoadBalancer<L> {
    inner: L,
}

impl<L> BoxFutureLoadBalancer<L> {
    pub fn new(inner: L) -> Self {
        Self { inner }
    }
}

impl<L> LoadBalancer for BoxFutureLoadBalancer<L>
where
    L: LoadBalancer,
    L::Future: Send + 'static,
{
    type Element = L::Element;
    type Error = L::Error;
    type Future = BoxFuture<'static, Result<Option<Self::Element>, Self::Error>>;

    fn choose(&self, extensions: &mut Extensions) -> Self::Future {
        Box::pin(self.inner.choose(extensions))
    }
}

#[derive(Debug, Clone, Default)]
pub struct Statistic {
    pub count: Arc<AtomicU64>,
}