1mod registry;
2mod policy;
3mod weight;
4
5use futures::future::BoxFuture;
6use http::Extensions;
7use std::fmt::Debug;
8use std::future::Future;
9use std::sync::atomic::AtomicU64;
10use std::sync::Arc;
11
12pub use registry::LoadBalancerRegistry;
13pub use policy::{LoadBalancerPolicy, LoadBalancerPolicyTrait};
14pub use weight::WeightProvider;
15
16pub type BoxLoadBalancer<I, E> = Box<
17 dyn LoadBalancerTrait<Element = I, Error = E, Future = BoxFuture<'static, Result<Option<I>, E>>>
18 + Send
19 + Sync,
20>;
21
22pub trait LoadBalancerTrait {
23 type Element;
27
28 type Error;
32
33 type Future: Future<Output = Result<Option<Self::Element>, Self::Error>>;
37
38 fn choose(&self, extensions: &mut Extensions) -> Self::Future;
42
43 fn boxed(self) -> BoxLoadBalancer<Self::Element, Self::Error>
47 where
48 Self: Sized + Send + Sync + 'static,
49 Self::Future: Send + 'static,
50 {
51 Box::new(BoxFutureLoadBalancer::new(self))
52 }
53}
54
55struct BoxFutureLoadBalancer<L> {
56 inner: L,
57}
58
59impl<L> BoxFutureLoadBalancer<L> {
60 pub fn new(inner: L) -> Self {
61 Self { inner }
62 }
63}
64
65impl<L> LoadBalancerTrait for BoxFutureLoadBalancer<L>
66where
67 L: LoadBalancerTrait,
68 L::Future: Send + 'static,
69{
70 type Element = L::Element;
71 type Error = L::Error;
72 type Future = BoxFuture<'static, Result<Option<Self::Element>, Self::Error>>;
73
74 fn choose(&self, extensions: &mut Extensions) -> Self::Future {
75 Box::pin(self.inner.choose(extensions))
76 }
77}
78
79#[derive(Debug, Clone, Default)]
80pub struct Statistic {
81 pub count: Arc<AtomicU64>,
82}