libdoh/
globals.rs

1use std::net::SocketAddr;
2#[cfg(feature = "tls")]
3use std::path::PathBuf;
4use std::sync::atomic::{AtomicUsize, Ordering};
5use std::sync::Arc;
6use std::time::Duration;
7
8use tokio::runtime;
9
10use crate::odoh::ODoHRotator;
11
12#[derive(Debug)]
13pub struct Globals {
14    #[cfg(feature = "tls")]
15    pub tls_cert_path: Option<PathBuf>,
16
17    #[cfg(feature = "tls")]
18    pub tls_cert_key_path: Option<PathBuf>,
19
20    pub listen_address: SocketAddr,
21    pub local_bind_address: SocketAddr,
22    pub server_address: SocketAddr,
23    pub path: String,
24    pub max_clients: usize,
25    pub timeout: Duration,
26    pub clients_count: ClientsCount,
27    pub max_concurrent_streams: u32,
28    pub min_ttl: u32,
29    pub max_ttl: u32,
30    pub err_ttl: u32,
31    pub keepalive: bool,
32    pub disable_post: bool,
33    pub allow_odoh_post: bool,
34    pub enable_ecs: bool,
35    pub ecs_prefix_v4: u8,
36    pub ecs_prefix_v6: u8,
37    pub odoh_configs_path: String,
38    pub odoh_rotator: Arc<ODoHRotator>,
39
40    pub runtime_handle: runtime::Handle,
41}
42
43#[derive(Debug, Clone, Default)]
44pub struct ClientsCount(Arc<AtomicUsize>);
45
46impl ClientsCount {
47    pub fn current(&self) -> usize {
48        self.0.load(Ordering::Relaxed)
49    }
50
51    pub fn increment(&self) -> usize {
52        self.0.fetch_add(1, Ordering::Relaxed)
53    }
54
55    pub fn decrement(&self) -> usize {
56        let mut count;
57        while {
58            count = self.0.load(Ordering::Relaxed);
59            count > 0
60                && self
61                    .0
62                    .compare_exchange(count, count - 1, Ordering::Relaxed, Ordering::Relaxed)
63                    != Ok(count)
64        } {}
65        count
66    }
67}