Skip to content

Commit b32a42b

Browse files
committed
Added more parameters to configure connection
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 95ad0b1 commit b32a42b

File tree

4 files changed

+162
-23
lines changed

4 files changed

+162
-23
lines changed

src/driver/common_options.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use deadpool_postgres::RecyclingMethod;
22
use pyo3::pyclass;
3+
use tokio_postgres::config::LoadBalanceHosts;
34

45
#[pyclass]
56
#[derive(Clone, Copy)]
@@ -19,3 +20,22 @@ impl ConnRecyclingMethod {
1920
}
2021
}
2122
}
23+
24+
#[pyclass]
25+
#[derive(Clone, Copy)]
26+
pub enum ConnLoadBalanceHosts {
27+
/// Make connection attempts to hosts in the order provided.
28+
Disable,
29+
/// Make connection attempts to hosts in a random order.
30+
Random,
31+
}
32+
33+
impl ConnLoadBalanceHosts {
34+
#[must_use]
35+
pub fn to_internal(&self) -> LoadBalanceHosts {
36+
match self {
37+
ConnLoadBalanceHosts::Disable => LoadBalanceHosts::Disable,
38+
ConnLoadBalanceHosts::Random => LoadBalanceHosts::Random,
39+
}
40+
}
41+
}

src/driver/connection_pool.rs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::runtime::tokio_runtime;
22
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod};
33
use pyo3::{pyclass, pyfunction, pymethods, PyAny};
4-
use std::{str::FromStr, vec};
4+
use std::{time::Duration, vec};
55
use tokio_postgres::{NoTls, Row};
66

77
use crate::{
@@ -10,7 +10,11 @@ use crate::{
1010
value_converter::{convert_parameters, PythonDTO, QueryParameter},
1111
};
1212

13-
use super::{common_options::ConnRecyclingMethod, connection::Connection};
13+
use super::{
14+
common_options::{ConnLoadBalanceHosts, ConnRecyclingMethod},
15+
connection::Connection,
16+
utils::build_connection_config,
17+
};
1418

1519
/// Make new connection pool.
1620
///
@@ -25,6 +29,16 @@ pub fn connect(
2529
host: Option<String>,
2630
port: Option<u16>,
2731
db_name: Option<String>,
32+
options: Option<String>,
33+
application_name: Option<String>,
34+
connect_timeout: Option<Duration>,
35+
tcp_user_timeout: Option<Duration>,
36+
keepalives: Option<bool>,
37+
keepalives_idle: Option<Duration>,
38+
keepalives_interval: Option<Duration>,
39+
keepalives_retries: Option<u32>,
40+
load_balance_hosts: Option<ConnLoadBalanceHosts>,
41+
2842
max_db_pool_size: Option<usize>,
2943
conn_recycling_method: Option<ConnRecyclingMethod>,
3044
) -> RustPSQLDriverPyResult<ConnectionPool> {
@@ -36,27 +50,23 @@ pub fn connect(
3650
}
3751
}
3852

39-
let mut pg_config: tokio_postgres::Config;
40-
if let Some(dsn_string) = dsn {
41-
pg_config = tokio_postgres::Config::from_str(&dsn_string)?;
42-
} else {
43-
pg_config = tokio_postgres::Config::new();
44-
if let (Some(password), Some(username)) = (password, username) {
45-
pg_config.password(&password);
46-
pg_config.user(&username);
47-
}
48-
if let Some(host) = host {
49-
pg_config.host(&host);
50-
}
51-
52-
if let Some(port) = port {
53-
pg_config.port(port);
54-
}
55-
56-
if let Some(db_name) = db_name {
57-
pg_config.dbname(&db_name);
58-
}
59-
}
53+
let pg_config = build_connection_config(
54+
dsn,
55+
username,
56+
password,
57+
host,
58+
port,
59+
db_name,
60+
options,
61+
application_name,
62+
connect_timeout,
63+
tcp_user_timeout,
64+
keepalives,
65+
keepalives_idle,
66+
keepalives_interval,
67+
keepalives_retries,
68+
load_balance_hosts,
69+
)?;
6070

6171
let mgr_config: ManagerConfig;
6272
if let Some(conn_recycling_method) = conn_recycling_method {
@@ -98,6 +108,15 @@ impl ConnectionPool {
98108
host: Option<String>,
99109
port: Option<u16>,
100110
db_name: Option<String>,
111+
options: Option<String>,
112+
application_name: Option<String>,
113+
connect_timeout: Option<Duration>,
114+
tcp_user_timeout: Option<Duration>,
115+
keepalives: Option<bool>,
116+
keepalives_idle: Option<Duration>,
117+
keepalives_interval: Option<Duration>,
118+
keepalives_retries: Option<u32>,
119+
load_balance_hosts: Option<ConnLoadBalanceHosts>,
101120
max_db_pool_size: Option<usize>,
102121
conn_recycling_method: Option<ConnRecyclingMethod>,
103122
) -> RustPSQLDriverPyResult<Self> {
@@ -108,6 +127,15 @@ impl ConnectionPool {
108127
host,
109128
port,
110129
db_name,
130+
options,
131+
application_name,
132+
connect_timeout,
133+
tcp_user_timeout,
134+
keepalives,
135+
keepalives_idle,
136+
keepalives_interval,
137+
keepalives_retries,
138+
load_balance_hosts,
111139
max_db_pool_size,
112140
conn_recycling_method,
113141
)

src/driver/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod connection_pool;
44
pub mod cursor;
55
pub mod transaction;
66
pub mod transaction_options;
7+
pub mod utils;

src/driver/utils.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use std::{str::FromStr, time::Duration};
2+
3+
use crate::exceptions::rust_errors::RustPSQLDriverPyResult;
4+
5+
use super::common_options::ConnLoadBalanceHosts;
6+
7+
/// Create new config.
8+
///
9+
/// # Errors
10+
/// May return Err Result if cannot build new config.
11+
#[allow(clippy::too_many_arguments)]
12+
pub fn build_connection_config(
13+
dsn: Option<String>,
14+
username: Option<String>,
15+
password: Option<String>,
16+
host: Option<String>,
17+
port: Option<u16>,
18+
db_name: Option<String>,
19+
options: Option<String>,
20+
application_name: Option<String>,
21+
connect_timeout: Option<Duration>,
22+
tcp_user_timeout: Option<Duration>,
23+
keepalives: Option<bool>,
24+
keepalives_idle: Option<Duration>,
25+
keepalives_interval: Option<Duration>,
26+
keepalives_retries: Option<u32>,
27+
load_balance_hosts: Option<ConnLoadBalanceHosts>,
28+
) -> RustPSQLDriverPyResult<tokio_postgres::Config> {
29+
if let Some(dsn_string) = dsn {
30+
return Ok(tokio_postgres::Config::from_str(&dsn_string)?);
31+
}
32+
33+
let mut pg_config = tokio_postgres::Config::new();
34+
35+
if let (Some(password), Some(username)) = (password, username) {
36+
pg_config.password(&password);
37+
pg_config.user(&username);
38+
}
39+
if let Some(host) = host {
40+
pg_config.host(&host);
41+
}
42+
43+
if let Some(port) = port {
44+
pg_config.port(port);
45+
}
46+
47+
if let Some(db_name) = db_name {
48+
pg_config.dbname(&db_name);
49+
}
50+
51+
if let Some(options) = options {
52+
pg_config.options(&options);
53+
}
54+
55+
if let Some(application_name) = application_name {
56+
pg_config.application_name(&application_name);
57+
}
58+
59+
if let Some(connect_timeout) = connect_timeout {
60+
pg_config.connect_timeout(connect_timeout);
61+
}
62+
63+
if let Some(tcp_user_timeout) = tcp_user_timeout {
64+
pg_config.tcp_user_timeout(tcp_user_timeout);
65+
}
66+
67+
if let Some(keepalives) = keepalives {
68+
if keepalives {
69+
pg_config.keepalives(keepalives);
70+
71+
if let Some(keepalives_idle) = keepalives_idle {
72+
pg_config.keepalives_idle(keepalives_idle);
73+
}
74+
75+
if let Some(keepalives_interval) = keepalives_interval {
76+
pg_config.keepalives_interval(keepalives_interval);
77+
}
78+
79+
if let Some(keepalives_retries) = keepalives_retries {
80+
pg_config.keepalives_retries(keepalives_retries);
81+
}
82+
}
83+
}
84+
85+
if let Some(load_balance_hosts) = load_balance_hosts {
86+
pg_config.load_balance_hosts(load_balance_hosts.to_internal());
87+
}
88+
89+
Ok(pg_config)
90+
}

0 commit comments

Comments
 (0)