net_pool/
macros.rs

1#[macro_export]
2macro_rules! base_pool_impl {
3    (
4        $state:ident
5    ) => {
6        fn get_id(&self) -> &str {
7            &self.$state.id
8        }
9        
10        fn set_id(&mut self, id: &str) {
11            self.$state.id = id.to_string();
12        }
13        
14        fn set_max_conn(&self, mut max: Option<usize>) {
15            if let Some(0) = max {
16                max = None;
17            }
18            match max {
19                None => self
20                    .$state
21                    .max_conn
22                    .store(usize::MAX, std::sync::atomic::Ordering::Relaxed),
23                Some(v) => self
24                    .$state
25                    .max_conn
26                    .store(v, std::sync::atomic::Ordering::Relaxed),
27            }
28        }
29
30        fn get_max_conn(&self) -> Option<usize> {
31            match self
32                .$state
33                .max_conn
34                .load(std::sync::atomic::Ordering::Relaxed)
35            {
36                usize::MAX => None,
37                v => Some(v),
38            }
39        }
40
41        fn get_cur_conn(&self) -> usize {
42            self.$state
43                .cur_conn
44                .load(std::sync::atomic::Ordering::Relaxed)
45        }
46
47        fn set_keepalive(&self, mut duration: Option<std::time::Duration>) {
48            if let Some(d) = duration {
49                if d.is_zero() {
50                    duration = None;
51                }
52            }
53
54            match duration {
55                None => self
56                    .$state
57                    .keepalive
58                    .store(u64::MAX, std::sync::atomic::Ordering::Relaxed),
59                Some(v) => self
60                    .$state
61                    .keepalive
62                    .store(v.as_secs(), std::sync::atomic::Ordering::Relaxed),
63            }
64        }
65
66        fn get_keepalive(&self) -> Option<std::time::Duration> {
67            match self
68                .$state
69                .keepalive
70                .load(std::sync::atomic::Ordering::Relaxed)
71            {
72                u64::MAX => None,
73                v => Some(std::time::Duration::from_secs(v)),
74            }
75        }
76
77        fn get_strategy(&self) -> Arc<dyn Strategy> {
78            self.$state.lb_strategy.clone()
79        }
80        
81        fn set_strategy(&mut self, strategy: Arc<dyn Strategy>) {
82            self.$state.lb_strategy = strategy;
83        }
84    };
85}
86
87#[allow(unused_imports)]
88pub use base_pool_impl;