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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::net::SocketAddr;
use std::time::Duration;
const DEFAULT_MIN_CONNS: usize = 5;
const DEFAULT_MAX_CONNS: usize = 10;
#[derive(Clone, Debug)]
pub struct Options {
pub(crate) addr: SocketAddr,
pub(crate) database: String,
pub(crate) username: String,
pub(crate) password: String,
pub(crate) compression: bool,
pub(crate) pool_min: usize,
pub(crate) pool_max: usize,
pub(crate) nodelay: bool,
pub(crate) keepalive: Option<Duration>,
pub(crate) ping_before_query: bool,
pub(crate) send_retries: usize,
pub(crate) retry_timeout: Duration,
pub(crate) ping_timeout: Duration,
}
impl Default for Options {
fn default() -> Options {
Options {
addr: "127.0.0.1:9000".parse().unwrap(),
database: "default".to_string(),
username: "default".to_string(),
password: "".to_string(),
compression: false,
pool_min: DEFAULT_MIN_CONNS,
pool_max: DEFAULT_MAX_CONNS,
nodelay: true,
keepalive: None,
ping_before_query: true,
send_retries: 3,
retry_timeout: Duration::from_secs(5),
ping_timeout: Duration::from_millis(500),
}
}
}
macro_rules! property {
( $k:ident: $t:ty ) => {
pub fn $k(self, $k: $t) -> Options {
Options {
$k: $k.into(),
..self
}
}
}
}
impl Options {
pub fn new(addr: SocketAddr) -> Options {
Options {
addr,
..Options::default()
}
}
property!(database: &str);
property!(username: &str);
property!(password: &str);
pub fn with_compression(self) -> Options {
Options {
compression: true,
..self
}
}
property!(pool_min: usize);
property!(pool_max: usize);
property!(nodelay: bool);
property!(keepalive: Option<Duration>);
property!(ping_before_query: bool);
property!(send_retries: usize);
property!(retry_timeout: Duration);
property!(ping_timeout: Duration);
}