-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.rs
103 lines (93 loc) · 2.68 KB
/
options.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#[cfg(feature = "tls")]
use std::sync::Arc;
use derive_builder::Builder;
use getset::Getters;
#[cfg(feature = "tls")]
use tokio_rustls::rustls::{ClientConfig, RootCertStore};
/// Riemann connection options
#[derive(Builder, Clone, Getters)]
#[builder(setter(into))]
#[builder(build_fn(skip))]
#[builder(pattern = "owned")]
#[get = "pub"]
pub struct RiemannClientOptions {
host: String,
port: u16,
connect_timeout_ms: u64,
socket_timeout_ms: u64,
use_udp: bool,
#[cfg(feature = "tls")]
use_tls: bool,
#[cfg(feature = "tls")]
tls_config: Option<Arc<ClientConfig>>,
}
#[cfg(feature = "tls")]
fn default_tls_config() -> Arc<ClientConfig> {
let root_store = RootCertStore {
roots: webpki_roots::TLS_SERVER_ROOTS.iter().cloned().collect(),
};
let tls_config = ClientConfig::builder()
.with_root_certificates(root_store)
.with_no_client_auth();
Arc::new(tls_config)
}
impl RiemannClientOptionsBuilder {
#[cfg(feature = "tls")]
fn tls_enabled(&self) -> bool {
self.use_tls.unwrap_or(false)
}
#[cfg(not(feature = "tls"))]
#[inline]
fn tls_enabled(&self) -> bool {
false
}
#[cfg(feature = "tls")]
fn get_tls_config(&self) -> Option<Arc<ClientConfig>> {
self.tls_config.as_ref().cloned().unwrap_or_else(|| {
if self.tls_enabled() {
Some(default_tls_config())
} else {
None
}
})
}
pub fn build(self) -> RiemannClientOptions {
let use_tls = self.tls_enabled();
let udp = if use_tls {
false
} else {
self.use_udp.unwrap_or(false)
};
RiemannClientOptions {
host: self.host.clone().unwrap_or_else(|| "127.0.0.1".to_owned()),
port: self.port.unwrap_or(5555),
connect_timeout_ms: self.connect_timeout_ms.unwrap_or(2000),
socket_timeout_ms: self.connect_timeout_ms.unwrap_or(3000),
use_udp: udp,
#[cfg(feature = "tls")]
use_tls,
#[cfg(feature = "tls")]
tls_config: self.get_tls_config(),
}
}
}
impl Default for RiemannClientOptions {
fn default() -> RiemannClientOptions {
Self {
host: "127.0.0.1".to_owned(),
port: 5555,
connect_timeout_ms: 2000,
socket_timeout_ms: 3000,
use_udp: false,
#[cfg(feature = "tls")]
use_tls: false,
#[cfg(feature = "tls")]
tls_config: None,
}
}
}
impl RiemannClientOptions {
pub(crate) fn to_socket_addr_string(&self) -> String {
format!("{}:{}", self.host, self.port)
}
}