-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconnection_pool_builder.rs
More file actions
344 lines (313 loc) · 11.1 KB
/
connection_pool_builder.rs
File metadata and controls
344 lines (313 loc) · 11.1 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use std::{net::IpAddr, time::Duration};
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
use pyo3::{pyclass, pymethods, Py, Python};
use crate::{
exceptions::rust_errors::{PSQLPyResult, RustPSQLDriverError},
options::{ConnRecyclingMethod, LoadBalanceHosts, SslMode, TargetSessionAttrs},
};
use super::{
connection_pool::ConnectionPool,
utils::{build_manager, build_tls},
};
#[pyclass]
pub struct ConnectionPoolBuilder {
config: tokio_postgres::Config,
max_db_pool_size: Option<usize>,
conn_recycling_method: Option<RecyclingMethod>,
ca_file: Option<String>,
ssl_mode: Option<SslMode>,
prepare: Option<bool>,
}
#[pymethods]
impl ConnectionPoolBuilder {
/// Create new connection pool builder.
#[new]
fn new() -> Self {
ConnectionPoolBuilder {
config: tokio_postgres::Config::new(),
max_db_pool_size: Some(2),
conn_recycling_method: None,
ca_file: None,
ssl_mode: None,
prepare: None,
}
}
/// Build connection pool.
///
/// # Errors
/// May return error if cannot build new connection pool.
fn build(&self) -> PSQLPyResult<ConnectionPool> {
let mgr_config: ManagerConfig;
if let Some(conn_recycling_method) = self.conn_recycling_method.as_ref() {
mgr_config = ManagerConfig {
recycling_method: conn_recycling_method.clone(),
}
} else {
mgr_config = ManagerConfig {
recycling_method: RecyclingMethod::Fast,
};
}
let mgr: Manager = build_manager(
mgr_config,
self.config.clone(),
build_tls(&self.ca_file, &self.ssl_mode)?,
);
let mut db_pool_builder = Pool::builder(mgr);
if let Some(max_db_pool_size) = self.max_db_pool_size {
db_pool_builder = db_pool_builder.max_size(max_db_pool_size);
}
let db_pool = db_pool_builder.build()?;
Ok(ConnectionPool::build(
db_pool,
self.config.clone(),
self.ca_file.clone(),
self.ssl_mode,
self.prepare,
))
}
/// Set `ca_file` for `ssl_mode` in `PostgreSQL`.
fn ca_file(self_: Py<Self>, ca_file: String) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.ca_file = Some(ca_file);
});
self_
}
/// Set size to the connection pool.
///
/// # Error
/// If size more than 2.
fn max_pool_size(self_: Py<Self>, pool_size: usize) -> PSQLPyResult<Py<Self>> {
if pool_size < 2 {
return Err(RustPSQLDriverError::ConnectionPoolConfigurationError(
"Maximum database pool size must be more than 1".into(),
));
}
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.max_db_pool_size = Some(pool_size);
});
Ok(self_)
}
/// Set connection recycling method.
fn conn_recycling_method(
self_: Py<Self>,
conn_recycling_method: ConnRecyclingMethod,
) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.conn_recycling_method = Some(conn_recycling_method.to_internal());
});
self_
}
/// Sets the user to authenticate with.
///
/// Defaults to the user executing this process.
#[must_use]
pub fn user(self_: Py<Self>, user: &str) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.user(user);
});
self_
}
/// Sets the password to authenticate with.
#[must_use]
pub fn password(self_: Py<Self>, password: &str) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.password(password);
});
self_
}
/// Sets the name of the database to connect to.
///
/// Defaults to the user.
#[must_use]
pub fn dbname(self_: Py<Self>, dbname: &str) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.dbname(dbname);
});
self_
}
/// Sets command line options used to configure the server.
#[must_use]
pub fn options(self_: Py<Self>, options: &str) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.options(options);
});
self_
}
/// Sets the value of the `application_name` runtime parameter.
#[must_use]
pub fn application_name(self_: Py<Self>, application_name: &str) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.application_name(application_name);
});
self_
}
/// Sets the SSL configuration.
///
/// Defaults to `prefer`.
#[must_use]
pub fn ssl_mode(self_: Py<Self>, ssl_mode: SslMode) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.ssl_mode = Some(ssl_mode);
self_.config.ssl_mode(ssl_mode.to_internal());
});
self_
}
/// Adds a host to the configuration.
///
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
/// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
/// There must be either no hosts, or the same number of hosts as hostaddrs.
#[must_use]
pub fn host(self_: Py<Self>, host: &str) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.host(host);
});
self_
}
/// Adds a hostaddr to the configuration.
///
/// Multiple hostaddrs can be specified by calling this method multiple times, and each will be tried in order.
/// There must be either no hostaddrs, or the same number of hostaddrs as hosts.
#[must_use]
pub fn hostaddr(self_: Py<Self>, hostaddr: IpAddr) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.hostaddr(hostaddr);
});
self_
}
/// Adds a port to the configuration.
///
/// Multiple ports can be specified by calling this method multiple times. There must either be no ports, in which
/// case the default of 5432 is used, a single port, in which it is used for all hosts, or the same number of ports
/// as hosts.
#[must_use]
pub fn port(self_: Py<Self>, port: u16) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.port(port);
});
self_
}
/// Sets the timeout applied to socket-level connection attempts.
///
/// Note that hostnames can resolve to multiple IP addresses, and this timeout will apply to each address of each
/// host separately. Defaults to no limit.
#[must_use]
pub fn connect_timeout(self_: Py<Self>, connect_timeout: u64) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_
.config
.connect_timeout(Duration::from_secs(connect_timeout));
});
self_
}
/// Sets the TCP user timeout.
///
/// This is ignored for Unix domain socket connections. It is only supported on systems where
/// `TCP_USER_TIMEOUT` is available and will default to the system default if omitted or set to 0;
/// on other systems, it has no effect.
#[must_use]
pub fn tcp_user_timeout(self_: Py<Self>, tcp_user_timeout: u64) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_
.config
.tcp_user_timeout(Duration::from_secs(tcp_user_timeout));
});
self_
}
/// Sets the requirements of the session.
///
/// This can be used to connect to the primary server in a clustered database rather than one of the read-only
/// secondary servers. Defaults to `Any`.
#[must_use]
pub fn target_session_attrs(
self_: Py<Self>,
target_session_attrs: TargetSessionAttrs,
) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_
.config
.target_session_attrs(target_session_attrs.to_internal());
});
self_
}
/// Sets the host load balancing behavior.
///
/// Defaults to `disable`.
#[must_use]
pub fn load_balance_hosts(self_: Py<Self>, load_balance_hosts: LoadBalanceHosts) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_
.config
.load_balance_hosts(load_balance_hosts.to_internal());
});
self_
}
/// Controls the use of TCP keepalive.
///
/// This is ignored for Unix domain socket connections. Defaults to `true`.
#[must_use]
pub fn keepalives(self_: Py<Self>, keepalives: bool) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.keepalives(keepalives);
});
self_
}
/// Sets the amount of idle time before a keepalive packet is sent on the connection.
///
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
#[must_use]
#[cfg(not(target_arch = "wasm32"))]
pub fn keepalives_idle(self_: Py<Self>, keepalives_idle: u64) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_
.config
.keepalives_idle(Duration::from_secs(keepalives_idle));
});
self_
}
/// Sets the time interval between TCP keepalive probes.
/// On Windows, this sets the value of the `tcp_keepalive` struct’s keepaliveinterval field.
///
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
#[must_use]
#[cfg(not(target_arch = "wasm32"))]
pub fn keepalives_interval(self_: Py<Self>, keepalives_interval: u64) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_
.config
.keepalives_interval(Duration::from_secs(keepalives_interval));
});
self_
}
/// Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
///
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
#[must_use]
#[cfg(not(target_arch = "wasm32"))]
pub fn keepalives_retries(self_: Py<Self>, keepalives_retries: u32) -> Py<Self> {
Python::with_gil(|gil| {
let mut self_ = self_.borrow_mut(gil);
self_.config.keepalives_retries(keepalives_retries);
});
self_
}
}