Skip to content

Commit 5f36edd

Browse files
committed
Started implementing chain to create pool
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 8b0c3ad commit 5f36edd

File tree

4 files changed

+283
-4
lines changed

4 files changed

+283
-4
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ profile = "black"
5353
multi_line_output = 3
5454

5555
[tool.black]
56-
line-length = 89
56+
line-length = 120
5757

5858
[tool.mypy]
5959
strict = true
@@ -113,7 +113,7 @@ ignore = [
113113
]
114114
exclude = [".venv/"]
115115
mccabe = { max-complexity = 10 }
116-
line-length = 89
116+
line-length = 120
117117

118118
[tool.ruff.per-file-ignores]
119119
"python/psqlpy/*" = ["PYI021"]

python/psqlpy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from psqlpy._internal import (
22
Connection,
33
ConnectionPool,
4+
ConnectionPoolBuilder,
45
ConnRecyclingMethod,
56
Cursor,
67
IsolationLevel,
@@ -30,4 +31,5 @@
3031
"TargetSessionAttrs",
3132
"SslMode",
3233
"KeepaliveConfig",
34+
"ConnectionPoolBuilder",
3335
]

python/psqlpy/_internal/__init__.pyi

Lines changed: 278 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import types
22
from enum import Enum
3-
from typing import Any, Callable, List, Optional, Sequence, TypeVar
3+
from ipaddress import IPv4Address, IPv6Address
4+
from typing import Any, Callable, List, Optional, Sequence, TypeVar, Union
45

56
from typing_extensions import Self
67

@@ -1200,3 +1201,279 @@ def connect(
12001201
- `max_db_pool_size`: maximum size of the connection pool.
12011202
- `conn_recycling_method`: how a connection is recycled.
12021203
"""
1204+
1205+
class ConnectionPoolBuilder:
1206+
"""Builder for `ConnectionPool`."""
1207+
1208+
def __init__(self: Self) -> None:
1209+
"""Initialize new instance of `ConnectionPoolBuilder`."""
1210+
def build(self: Self) -> ConnectionPool:
1211+
"""
1212+
Build `ConnectionPool`.
1213+
1214+
### Returns:
1215+
`ConnectionPool`
1216+
"""
1217+
def max_pool_size(self: Self, pool_size: int) -> Self:
1218+
"""
1219+
Set maximum connection pool size.
1220+
1221+
### Parameters:
1222+
- `pool_size`: size of the pool, must be more than 1.
1223+
1224+
### Returns:
1225+
`ConnectionPoolBuilder`
1226+
"""
1227+
def conn_recycling_method(
1228+
self: Self,
1229+
conn_recycling_method: ConnRecyclingMethod,
1230+
) -> Self:
1231+
"""
1232+
Set connection recycling method.
1233+
1234+
Connection recycling method is how a connection is recycled.
1235+
1236+
### Parameters:
1237+
- `conn_recycling_method`: ConnRecyclingMethod enum.
1238+
1239+
### Returns:
1240+
`ConnectionPoolBuilder`
1241+
"""
1242+
def user(self: Self, user: str) -> Self:
1243+
"""
1244+
Set username to `PostgreSQL`.
1245+
1246+
### Parameters:
1247+
- `user`: username of the PostgreSQL user.
1248+
1249+
### Returns:
1250+
`ConnectionPoolBuilder`
1251+
"""
1252+
def password(self: Self, password: str) -> Self:
1253+
"""
1254+
Set password for `PostgreSQL`.
1255+
1256+
### Parameters:
1257+
- `password`: password for the `PostgreSQL` user.
1258+
1259+
### Returns:
1260+
`ConnectionPoolBuilder`
1261+
"""
1262+
def dbname(self: Self, dbname: str) -> Self:
1263+
"""
1264+
Set database name for the `PostgreSQL`.
1265+
1266+
### Parameters:
1267+
- `dbname`: database for the `PostgreSQL`.
1268+
1269+
### Returns:
1270+
`ConnectionPoolBuilder`
1271+
"""
1272+
def options(self: Self, options: str) -> Self:
1273+
"""
1274+
Set command line options used to configure the server.
1275+
1276+
### Parameters:
1277+
- `options`: command line options
1278+
1279+
### Returns:
1280+
`ConnectionPoolBuilder`
1281+
"""
1282+
def application_name(self: Self, application_name: str) -> Self:
1283+
"""
1284+
Set the value of the `application_name` runtime parameter.
1285+
1286+
### Parameters:
1287+
- `application_name`: `application_name` runtime parameter
1288+
1289+
### Returns:
1290+
`ConnectionPoolBuilder`
1291+
"""
1292+
def ssl_mode(self: Self, ssl_mode: SslMode) -> Self:
1293+
"""
1294+
Set the SSL configuration.
1295+
1296+
### Parameters:
1297+
- `ssl_mode`: mode for TLS.
1298+
1299+
### Returns:
1300+
`ConnectionPoolBuilder`
1301+
"""
1302+
def host(self: Self, host: str) -> Self:
1303+
"""
1304+
Add a host to the configuration.
1305+
1306+
Multiple hosts can be specified by calling this method multiple times,
1307+
and each will be tried in order.
1308+
On Unix systems, a host starting with a `/` is interpreted
1309+
as a path to a directory containing Unix domain sockets.
1310+
There must be either no hosts,
1311+
or the same number of hosts as hostaddrs.
1312+
1313+
### Parameters:
1314+
- `host`: host to `PostgreSQL`.
1315+
1316+
### Returns:
1317+
`ConnectionPoolBuilder`
1318+
"""
1319+
def hostaddr(self: Self, hostaddr: Union[IPv4Address, IPv6Address]) -> Self:
1320+
"""
1321+
Add a hostaddr to the configuration.
1322+
1323+
Multiple hostaddrs can be specified by calling
1324+
this method multiple times, and each will be tried in order.
1325+
There must be either no hostaddrs,
1326+
or the same number of hostaddrs as hosts.
1327+
1328+
### Parameters:
1329+
- `hostaddr`: hostaddr to `PostgreSQL`.
1330+
1331+
### Returns:
1332+
`ConnectionPoolBuilder`
1333+
"""
1334+
def port(self: Self, port: int) -> Self:
1335+
"""
1336+
Add a port to the configuration.
1337+
1338+
Multiple ports can be specified by calling this method multiple times.
1339+
There must either be no ports,
1340+
in which case the default of 5432 is used,
1341+
a single port, in which it is used for all hosts,
1342+
or the same number of ports as hosts.
1343+
1344+
### Parameters:
1345+
- `port`: port for hosts to `PostgreSQL`.
1346+
1347+
### Returns:
1348+
`ConnectionPoolBuilder`
1349+
"""
1350+
def connect_timeout(self: Self, connect_timeout: int) -> Self:
1351+
"""
1352+
Set the timeout applied to socket-level connection attempts.
1353+
1354+
Note that hostnames can resolve to multiple IP addresses,
1355+
and this timeout will apply to each address of each
1356+
host separately. Defaults to no limit.
1357+
1358+
### Parameters:
1359+
- `connect_timeout`: connection timeout to `PostgreSQL`.
1360+
1361+
### Returns:
1362+
`ConnectionPoolBuilder`
1363+
"""
1364+
def tcp_user_timeout(self: Self, tcp_user_timeout: int) -> Self:
1365+
"""
1366+
Set the TCP user timeout.
1367+
1368+
This is ignored for Unix domain socket connections.
1369+
It is only supported on systems where TCP_USER_TIMEOUT is available
1370+
and will default to the system default if omitted or set to 0;
1371+
on other systems, it has no effect.
1372+
1373+
### Parameters:
1374+
- `tcp_user_timeout`: tcp_user_timeout to `PostgreSQL`.
1375+
1376+
### Returns:
1377+
`ConnectionPoolBuilder`
1378+
"""
1379+
def target_session_attrs(
1380+
self: Self,
1381+
target_session_attrs: TargetSessionAttrs,
1382+
) -> Self:
1383+
"""
1384+
Set the requirements of the session.
1385+
1386+
This can be used to connect to the primary server in a
1387+
clustered database rather than one of the read-only
1388+
secondary servers. Defaults to `Any`.
1389+
1390+
### Parameters:
1391+
- `target_session_attrs`: target_session_attrs for `PostgreSQL`.
1392+
1393+
### Returns:
1394+
`ConnectionPoolBuilder`
1395+
"""
1396+
def load_balance_hosts(
1397+
self: Self,
1398+
load_balance_hosts: LoadBalanceHosts,
1399+
) -> Self:
1400+
"""
1401+
Set the host load balancing behavior.
1402+
1403+
Defaults to `disable`.
1404+
1405+
### Parameters:
1406+
- `load_balance_hosts`: load_balance_hosts for `PostgreSQL`.
1407+
1408+
### Returns:
1409+
`ConnectionPoolBuilder`
1410+
"""
1411+
def keepalives(
1412+
self: Self,
1413+
keepalives: bool,
1414+
) -> Self:
1415+
"""
1416+
Control the use of TCP keepalive.
1417+
1418+
This is ignored for Unix domain socket connections.
1419+
1420+
Defaults to `true`.
1421+
1422+
### Parameters:
1423+
- `keepalives`: boolean value for keepalives.
1424+
1425+
### Returns:
1426+
`ConnectionPoolBuilder`
1427+
"""
1428+
def keepalives_idle(
1429+
self: Self,
1430+
keepalives_idle: int,
1431+
) -> Self:
1432+
"""
1433+
Set the amount of idle time before a keepalive packet is sent on the connection.
1434+
1435+
This is ignored for Unix domain sockets,
1436+
or if the `keepalives` option is disabled.
1437+
1438+
Defaults to 2 hours.
1439+
1440+
### Parameters:
1441+
- `keepalives_idle`: number in secs for idle.
1442+
1443+
### Returns:
1444+
`ConnectionPoolBuilder`
1445+
"""
1446+
def keepalives_interval(
1447+
self: Self,
1448+
keepalives_interval: int,
1449+
) -> Self:
1450+
"""
1451+
Sets the time interval between TCP keepalive probes.
1452+
1453+
On Windows, this sets the value of the
1454+
tcp_keepalive struct keepalive interval field.
1455+
1456+
This is ignored for Unix domain sockets,
1457+
or if the `keepalives` option is disabled.
1458+
1459+
### Parameters:
1460+
- `keepalives_interval`: number in secs for interval.
1461+
1462+
### Returns:
1463+
`ConnectionPoolBuilder`
1464+
"""
1465+
def keepalives_retries(
1466+
self: Self,
1467+
keepalives_retries: int,
1468+
) -> Self:
1469+
"""
1470+
Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
1471+
1472+
This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
1473+
1474+
### Parameters:
1475+
- `keepalives_retries`: number of retries.
1476+
1477+
### Returns:
1478+
`ConnectionPoolBuilder`
1479+
"""

src/driver/connection_pool_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl ConnectionPoolBuilder {
5959
///
6060
/// # Error
6161
/// If size more than 2.
62-
fn max_db_pool_size(self_: Py<Self>, pool_size: usize) -> RustPSQLDriverPyResult<Py<Self>> {
62+
fn max_pool_size(self_: Py<Self>, pool_size: usize) -> RustPSQLDriverPyResult<Py<Self>> {
6363
if pool_size < 2 {
6464
return Err(RustPSQLDriverError::ConnectionPoolConfigurationError(
6565
"Maximum database pool size must be more than 1".into(),

0 commit comments

Comments
 (0)