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
use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use tikv_util::config::ReadableDuration;
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub endpoints: Vec<String>,
pub retry_interval: ReadableDuration,
pub retry_max_count: isize,
pub retry_log_every: usize,
pub update_interval: ReadableDuration,
pub enable_forwarding: bool,
}
impl Default for Config {
fn default() -> Self {
Config {
endpoints: vec!["127.0.0.1:2379".to_string()],
retry_interval: ReadableDuration::millis(300),
retry_max_count: std::isize::MAX,
retry_log_every: 10,
update_interval: ReadableDuration::minutes(10),
enable_forwarding: false,
}
}
}
impl Config {
pub fn new(endpoints: Vec<String>) -> Self {
Config {
endpoints,
..Default::default()
}
}
pub fn validate(&self) -> Result<(), Box<dyn Error>> {
if self.endpoints.is_empty() {
return Err("please specify pd.endpoints.".into());
}
if self.retry_log_every == 0 {
return Err("pd.retry_log_every cannot be <=0".into());
}
if self.retry_max_count < -1 {
return Err("pd.retry_max_count cannot be < -1".into());
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pd_cfg() {
let cfg = Config::default();
cfg.validate().unwrap();
}
}