1use crate::*;
2
3#[derive(Clone)]
4pub enum GeneralLoadBalancer<T>
5where
6 T: Send + Sync + Clone + 'static,
7{
8 Interval(interval::IntervalLoadBalancer<T>),
9 Limit(limit::LimitLoadBalancer<T>),
10 Random(random::RandomLoadBalancer<T>),
11 Simple(simple::SimpleLoadBalancer<T>),
12 Threshold(threshold::ThresholdLoadBalancer<T>),
13}
14
15impl<T> GeneralLoadBalancer<T>
16where
17 T: Send + Sync + Clone + 'static,
18{
19 pub fn as_interval(&self) -> &interval::IntervalLoadBalancer<T> {
20 if let GeneralLoadBalancer::Interval(v) = self {
21 v
22 } else {
23 panic!("called as_interval on non-Interval variant");
24 }
25 }
26
27 pub fn as_limit(&self) -> &limit::LimitLoadBalancer<T> {
28 if let GeneralLoadBalancer::Limit(v) = self {
29 v
30 } else {
31 panic!("called as_limit on non-Limit variant");
32 }
33 }
34
35 pub fn as_random(&self) -> &random::RandomLoadBalancer<T> {
36 if let GeneralLoadBalancer::Random(v) = self {
37 v
38 } else {
39 panic!("called as_random on non-Random variant");
40 }
41 }
42
43 pub fn as_simple(&self) -> &simple::SimpleLoadBalancer<T> {
44 if let GeneralLoadBalancer::Simple(v) = self {
45 v
46 } else {
47 panic!("called as_simple on non-Simple variant");
48 }
49 }
50
51 pub fn as_threshold(&self) -> &threshold::ThresholdLoadBalancer<T> {
52 if let GeneralLoadBalancer::Threshold(v) = self {
53 v
54 } else {
55 panic!("called as_threshold on non-Threshold variant");
56 }
57 }
58}
59
60impl<T> LoadBalancer<T> for GeneralLoadBalancer<T>
61where
62 T: Send + Sync + Clone + 'static,
63{
64 async fn alloc(&self) -> T {
65 match self {
66 GeneralLoadBalancer::Interval(v) => LoadBalancer::alloc(v).await,
67 GeneralLoadBalancer::Limit(v) => LoadBalancer::alloc(v).await,
68 GeneralLoadBalancer::Random(v) => LoadBalancer::alloc(v).await,
69 GeneralLoadBalancer::Simple(v) => LoadBalancer::alloc(v).await,
70 GeneralLoadBalancer::Threshold(v) => LoadBalancer::alloc(v).await,
71 }
72 }
73
74 fn try_alloc(&self) -> Option<T> {
75 match self {
76 GeneralLoadBalancer::Interval(v) => LoadBalancer::try_alloc(v),
77 GeneralLoadBalancer::Limit(v) => LoadBalancer::try_alloc(v),
78 GeneralLoadBalancer::Random(v) => LoadBalancer::try_alloc(v),
79 GeneralLoadBalancer::Simple(v) => LoadBalancer::try_alloc(v),
80 GeneralLoadBalancer::Threshold(v) => LoadBalancer::try_alloc(v),
81 }
82 }
83}
84
85#[async_trait]
86impl<T> BoxLoadBalancer<T> for GeneralLoadBalancer<T>
87where
88 T: Send + Sync + Clone + 'static,
89{
90 async fn alloc(&self) -> T {
91 match self {
92 GeneralLoadBalancer::Interval(v) => BoxLoadBalancer::alloc(v).await,
93 GeneralLoadBalancer::Limit(v) => BoxLoadBalancer::alloc(v).await,
94 GeneralLoadBalancer::Random(v) => BoxLoadBalancer::alloc(v).await,
95 GeneralLoadBalancer::Simple(v) => BoxLoadBalancer::alloc(v).await,
96 GeneralLoadBalancer::Threshold(v) => BoxLoadBalancer::alloc(v).await,
97 }
98 }
99
100 fn try_alloc(&self) -> Option<T> {
101 match self {
102 GeneralLoadBalancer::Interval(v) => BoxLoadBalancer::try_alloc(v),
103 GeneralLoadBalancer::Limit(v) => BoxLoadBalancer::try_alloc(v),
104 GeneralLoadBalancer::Random(v) => BoxLoadBalancer::try_alloc(v),
105 GeneralLoadBalancer::Simple(v) => BoxLoadBalancer::try_alloc(v),
106 GeneralLoadBalancer::Threshold(v) => BoxLoadBalancer::try_alloc(v),
107 }
108 }
109}