Skip to content

Commit f85a092

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

File tree

5 files changed

+319
-67
lines changed

5 files changed

+319
-67
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 159 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import types
22
from enum import Enum
3-
from typing import Any, Callable, Optional, TypeVar
3+
from typing import Any, Callable, List, Optional, TypeVar
44

55
from typing_extensions import Self
66

@@ -104,6 +104,14 @@ class IsolationLevel(Enum):
104104
RepeatableRead = 3
105105
Serializable = 4
106106

107+
class ConnLoadBalanceHosts(Enum):
108+
"""Load balancing configuration."""
109+
110+
# Make connection attempts to hosts in the order provided.
111+
Disable = (1,)
112+
# Make connection attempts to hosts in a random order.
113+
Random = (2,)
114+
107115
class ReadVariant(Enum):
108116
"""Class for Read Variant for transaction."""
109117

@@ -869,8 +877,23 @@ class ConnectionPool:
869877
username: Optional[str] = None,
870878
password: Optional[str] = None,
871879
host: Optional[str] = None,
880+
hosts: Optional[List[str]] = None,
872881
port: Optional[int] = None,
882+
ports: Optional[List[int]] = None,
873883
db_name: Optional[str] = None,
884+
options: Optional[str] = None,
885+
application_name: Optional[str] = None,
886+
connect_timeout_sec: Optional[int] = None,
887+
connect_timeout_nanosec: Optional[int] = None,
888+
tcp_user_timeout_sec: Optional[int] = None,
889+
tcp_user_timeout_nanosec: Optional[int] = None,
890+
keepalives: Optional[bool] = None,
891+
keepalives_idle_sec: Optional[int] = None,
892+
keepalives_idle_nanosec: Optional[int] = None,
893+
keepalives_interval_sec: Optional[int] = None,
894+
keepalives_interval_nanosec: Optional[int] = None,
895+
keepalives_retries: Optional[int] = None,
896+
load_balance_hosts: Optional[ConnLoadBalanceHosts] = None,
874897
max_db_pool_size: int = 2,
875898
conn_recycling_method: Optional[ConnRecyclingMethod] = None,
876899
) -> None:
@@ -879,22 +902,67 @@ class ConnectionPool:
879902
It connects to the database and create pool.
880903
881904
You cannot set the minimum size for the connection
882-
pool, by default it is 1.
905+
pool, by it is 0.
906+
`ConnectionPool` doesn't create connections on startup.
907+
It makes new connection on demand.
883908
884-
This connection pool can:
885-
- Startup itself with `startup` method
886-
- Execute queries and return `QueryResult` class as a result
887-
- Create new instance of `Transaction`
909+
If you specify `dsn` parameter then `username`, `password`,
910+
`host`, `hosts`, `port`, `ports`, `db_name` and `target_session_attrs`
911+
parameters will be ignored.
888912
889913
### Parameters:
890-
- `dsn`: full dsn connection string.
914+
- `dsn`: Full dsn connection string.
891915
`postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
892-
- `username`: username of the user in postgres
893-
- `password`: password of the user in postgres
894-
- `host`: host of postgres
895-
- `port`: port of postgres
896-
- `db_name`: name of the database in postgres
897-
- `max_db_pool_size`: maximum size of the connection pool
916+
- `username`: Username of the user in the PostgreSQL
917+
- `password`: Password of the user in the PostgreSQL
918+
- `host`: Host of the PostgreSQL
919+
- `hosts`: Hosts of the PostgreSQL
920+
- `port`: Port of the PostgreSQL
921+
- `ports`: Ports of the PostgreSQL
922+
- `db_name`: Name of the database in PostgreSQL
923+
- `target_session_attrs`: Specifies requirements of the session.
924+
- `options`: Command line options used to configure the server
925+
- `application_name`: Sets the application_name parameter on the server.
926+
- `connect_timeout_sec`: The time limit in seconds applied to each socket-level
927+
connection attempt.
928+
Note that hostnames can resolve to multiple IP addresses,
929+
and this limit is applied to each address. Defaults to no timeout.
930+
- `connect_timeout_nanosec`: nanosec for connection timeout,
931+
can be used only with connect_timeout_sec.
932+
- `tcp_user_timeout_sec`: The time limit that
933+
transmitted data may remain unacknowledged
934+
before a connection is forcibly closed.
935+
This is ignored for Unix domain socket connections.
936+
It is only supported on systems where TCP_USER_TIMEOUT
937+
is available and will default to the system default if omitted
938+
or set to 0; on other systems, it has no effect.
939+
- `tcp_user_timeout_nanosec`: nanosec for cp_user_timeout,
940+
can be used only with tcp_user_timeout_sec.
941+
- `keepalives`: Controls the use of TCP keepalive.
942+
This option is ignored when connecting with Unix sockets.
943+
Defaults to on.
944+
- `keepalives_idle_sec`: The number of seconds of inactivity after
945+
which a keepalive message is sent to the server.
946+
This option is ignored when connecting with Unix sockets.
947+
Defaults to 2 hours.
948+
- `keepalives_idle_nanosec`: Nanosec for keepalives_idle_sec.
949+
- `keepalives_interval_sec`: The time interval between TCP keepalive probes.
950+
This option is ignored when connecting with Unix sockets.
951+
- `keepalives_interval_nanosec`: Nanosec for keepalives_interval_sec.
952+
- `keepalives_retries`: The maximum number of TCP keepalive probes
953+
that will be sent before dropping a connection.
954+
This option is ignored when connecting with Unix sockets.
955+
- `load_balance_hosts`: Controls the order in which the client tries to connect
956+
to the available hosts and addresses.
957+
Once a connection attempt is successful no other
958+
hosts and addresses will be tried.
959+
This parameter is typically used in combination with multiple host names
960+
or a DNS record that returns multiple IPs.
961+
If set to disable, hosts and addresses will be tried in the order provided.
962+
If set to random, hosts will be tried in a random order, and the IP addresses
963+
resolved from a hostname will also be tried in a random order.
964+
Defaults to disable.
965+
- `max_db_pool_size`: maximum size of the connection pool.
898966
- `conn_recycling_method`: how a connection is recycled.
899967
"""
900968
async def execute(
@@ -945,21 +1013,91 @@ def connect(
9451013
username: Optional[str] = None,
9461014
password: Optional[str] = None,
9471015
host: Optional[str] = None,
1016+
hosts: Optional[List[str]] = None,
9481017
port: Optional[int] = None,
1018+
ports: Optional[List[int]] = None,
9491019
db_name: Optional[str] = None,
1020+
options: Optional[str] = None,
1021+
application_name: Optional[str] = None,
1022+
connect_timeout_sec: Optional[int] = None,
1023+
connect_timeout_nanosec: Optional[int] = None,
1024+
tcp_user_timeout_sec: Optional[int] = None,
1025+
tcp_user_timeout_nanosec: Optional[int] = None,
1026+
keepalives: Optional[bool] = None,
1027+
keepalives_idle_sec: Optional[int] = None,
1028+
keepalives_idle_nanosec: Optional[int] = None,
1029+
keepalives_interval_sec: Optional[int] = None,
1030+
keepalives_interval_nanosec: Optional[int] = None,
1031+
keepalives_retries: Optional[int] = None,
1032+
load_balance_hosts: Optional[ConnLoadBalanceHosts] = None,
9501033
max_db_pool_size: int = 2,
9511034
conn_recycling_method: Optional[ConnRecyclingMethod] = None,
9521035
) -> ConnectionPool:
953-
"""Create new connection pool.
1036+
"""Create new PostgreSQL connection pool.
1037+
1038+
It connects to the database and create pool.
1039+
1040+
You cannot set the minimum size for the connection
1041+
pool, by it is 0.
1042+
`ConnectionPool` doesn't create connections on startup.
1043+
It makes new connection on demand.
1044+
1045+
If you specify `dsn` parameter then `username`, `password`,
1046+
`host`, `hosts`, `port`, `ports`, `db_name` and `target_session_attrs`
1047+
parameters will be ignored.
9541048
9551049
### Parameters:
956-
- `dsn`: full dsn connection string.
1050+
- `dsn`: Full dsn connection string.
9571051
`postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
958-
- `username`: username of the user in postgres
959-
- `password`: password of the user in postgres
960-
- `host`: host of postgres
961-
- `port`: port of postgres
962-
- `db_name`: name of the database in postgres
963-
- `max_db_pool_size`: maximum size of the connection pool
1052+
- `username`: Username of the user in the PostgreSQL
1053+
- `password`: Password of the user in the PostgreSQL
1054+
- `host`: Host of the PostgreSQL
1055+
- `hosts`: Hosts of the PostgreSQL
1056+
- `port`: Port of the PostgreSQL
1057+
- `ports`: Ports of the PostgreSQL
1058+
- `db_name`: Name of the database in PostgreSQL
1059+
- `target_session_attrs`: Specifies requirements of the session.
1060+
- `options`: Command line options used to configure the server
1061+
- `application_name`: Sets the application_name parameter on the server.
1062+
- `connect_timeout_sec`: The time limit in seconds applied to each socket-level
1063+
connection attempt.
1064+
Note that hostnames can resolve to multiple IP addresses,
1065+
and this limit is applied to each address. Defaults to no timeout.
1066+
- `connect_timeout_nanosec`: nanosec for connection timeout,
1067+
can be used only with connect_timeout_sec.
1068+
- `tcp_user_timeout_sec`: The time limit that
1069+
transmitted data may remain unacknowledged
1070+
before a connection is forcibly closed.
1071+
This is ignored for Unix domain socket connections.
1072+
It is only supported on systems where TCP_USER_TIMEOUT
1073+
is available and will default to the system default if omitted
1074+
or set to 0; on other systems, it has no effect.
1075+
- `tcp_user_timeout_nanosec`: nanosec for cp_user_timeout,
1076+
can be used only with tcp_user_timeout_sec.
1077+
- `keepalives`: Controls the use of TCP keepalive.
1078+
This option is ignored when connecting with Unix sockets.
1079+
Defaults to on.
1080+
- `keepalives_idle_sec`: The number of seconds of inactivity after
1081+
which a keepalive message is sent to the server.
1082+
This option is ignored when connecting with Unix sockets.
1083+
Defaults to 2 hours.
1084+
- `keepalives_idle_nanosec`: Nanosec for keepalives_idle_sec.
1085+
- `keepalives_interval_sec`: The time interval between TCP keepalive probes.
1086+
This option is ignored when connecting with Unix sockets.
1087+
- `keepalives_interval_nanosec`: Nanosec for keepalives_interval_sec.
1088+
- `keepalives_retries`: The maximum number of TCP keepalive probes
1089+
that will be sent before dropping a connection.
1090+
This option is ignored when connecting with Unix sockets.
1091+
- `load_balance_hosts`: Controls the order in which the client tries to connect
1092+
to the available hosts and addresses.
1093+
Once a connection attempt is successful no other
1094+
hosts and addresses will be tried.
1095+
This parameter is typically used in combination with multiple host names
1096+
or a DNS record that returns multiple IPs.
1097+
If set to disable, hosts and addresses will be tried in the order provided.
1098+
If set to random, hosts will be tried in a random order, and the IP addresses
1099+
resolved from a hostname will also be tried in a random order.
1100+
Defaults to disable.
1101+
- `max_db_pool_size`: maximum size of the connection pool.
9641102
- `conn_recycling_method`: how a connection is recycled.
9651103
"""

src/driver/common_options.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use deadpool_postgres::RecyclingMethod;
22
use pyo3::pyclass;
3-
use tokio_postgres::config::LoadBalanceHosts;
3+
use tokio_postgres::config::{LoadBalanceHosts, TargetSessionAttrs};
44

55
#[pyclass]
66
#[derive(Clone, Copy)]
@@ -39,3 +39,25 @@ impl ConnLoadBalanceHosts {
3939
}
4040
}
4141
}
42+
43+
#[pyclass]
44+
#[derive(Clone, Copy)]
45+
pub enum ConnTargetSessionAttrs {
46+
/// No special properties are required.
47+
Any,
48+
/// The session must allow writes.
49+
ReadWrite,
50+
/// The session allow only reads.
51+
ReadOnly,
52+
}
53+
54+
impl ConnTargetSessionAttrs {
55+
#[must_use]
56+
pub fn to_internal(&self) -> TargetSessionAttrs {
57+
match self {
58+
ConnTargetSessionAttrs::Any => TargetSessionAttrs::Any,
59+
ConnTargetSessionAttrs::ReadWrite => TargetSessionAttrs::ReadWrite,
60+
ConnTargetSessionAttrs::ReadOnly => TargetSessionAttrs::ReadOnly,
61+
}
62+
}
63+
}

src/driver/connection_pool.rs

Lines changed: 46 additions & 18 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::{time::Duration, vec};
4+
use std::vec;
55
use tokio_postgres::{NoTls, Row};
66

77
use crate::{
@@ -11,7 +11,7 @@ use crate::{
1111
};
1212

1313
use super::{
14-
common_options::{ConnLoadBalanceHosts, ConnRecyclingMethod},
14+
common_options::{ConnLoadBalanceHosts, ConnRecyclingMethod, ConnTargetSessionAttrs},
1515
connection::Connection,
1616
utils::build_connection_config,
1717
};
@@ -27,15 +27,22 @@ pub fn connect(
2727
username: Option<String>,
2828
password: Option<String>,
2929
host: Option<String>,
30+
hosts: Option<Vec<String>>,
3031
port: Option<u16>,
32+
ports: Option<Vec<u16>>,
3133
db_name: Option<String>,
34+
target_session_attrs: Option<ConnTargetSessionAttrs>,
3235
options: Option<String>,
3336
application_name: Option<String>,
34-
connect_timeout: Option<Duration>,
35-
tcp_user_timeout: Option<Duration>,
37+
connect_timeout_sec: Option<u64>,
38+
connect_timeout_nanosec: Option<u32>,
39+
tcp_user_timeout_sec: Option<u64>,
40+
tcp_user_timeout_nanosec: Option<u32>,
3641
keepalives: Option<bool>,
37-
keepalives_idle: Option<Duration>,
38-
keepalives_interval: Option<Duration>,
42+
keepalives_idle_sec: Option<u64>,
43+
keepalives_idle_nanosec: Option<u32>,
44+
keepalives_interval_sec: Option<u64>,
45+
keepalives_interval_nanosec: Option<u32>,
3946
keepalives_retries: Option<u32>,
4047
load_balance_hosts: Option<ConnLoadBalanceHosts>,
4148

@@ -55,15 +62,22 @@ pub fn connect(
5562
username,
5663
password,
5764
host,
65+
hosts,
5866
port,
67+
ports,
5968
db_name,
69+
target_session_attrs,
6070
options,
6171
application_name,
62-
connect_timeout,
63-
tcp_user_timeout,
72+
connect_timeout_sec,
73+
connect_timeout_nanosec,
74+
tcp_user_timeout_sec,
75+
tcp_user_timeout_nanosec,
6476
keepalives,
65-
keepalives_idle,
66-
keepalives_interval,
77+
keepalives_idle_sec,
78+
keepalives_idle_nanosec,
79+
keepalives_interval_sec,
80+
keepalives_interval_nanosec,
6781
keepalives_retries,
6882
load_balance_hosts,
6983
)?;
@@ -106,15 +120,22 @@ impl ConnectionPool {
106120
username: Option<String>,
107121
password: Option<String>,
108122
host: Option<String>,
123+
hosts: Option<Vec<String>>,
109124
port: Option<u16>,
125+
ports: Option<Vec<u16>>,
110126
db_name: Option<String>,
127+
target_session_attrs: Option<ConnTargetSessionAttrs>,
111128
options: Option<String>,
112129
application_name: Option<String>,
113-
connect_timeout: Option<Duration>,
114-
tcp_user_timeout: Option<Duration>,
130+
connect_timeout_sec: Option<u64>,
131+
connect_timeout_nanosec: Option<u32>,
132+
tcp_user_timeout_sec: Option<u64>,
133+
tcp_user_timeout_nanosec: Option<u32>,
115134
keepalives: Option<bool>,
116-
keepalives_idle: Option<Duration>,
117-
keepalives_interval: Option<Duration>,
135+
keepalives_idle_sec: Option<u64>,
136+
keepalives_idle_nanosec: Option<u32>,
137+
keepalives_interval_sec: Option<u64>,
138+
keepalives_interval_nanosec: Option<u32>,
118139
keepalives_retries: Option<u32>,
119140
load_balance_hosts: Option<ConnLoadBalanceHosts>,
120141
max_db_pool_size: Option<usize>,
@@ -125,15 +146,22 @@ impl ConnectionPool {
125146
username,
126147
password,
127148
host,
149+
hosts,
128150
port,
151+
ports,
129152
db_name,
153+
target_session_attrs,
130154
options,
131155
application_name,
132-
connect_timeout,
133-
tcp_user_timeout,
156+
connect_timeout_sec,
157+
connect_timeout_nanosec,
158+
tcp_user_timeout_sec,
159+
tcp_user_timeout_nanosec,
134160
keepalives,
135-
keepalives_idle,
136-
keepalives_interval,
161+
keepalives_idle_sec,
162+
keepalives_idle_nanosec,
163+
keepalives_interval_sec,
164+
keepalives_interval_nanosec,
137165
keepalives_retries,
138166
load_balance_hosts,
139167
max_db_pool_size,

0 commit comments

Comments
 (0)