Skip to content

Commit bd9e5b3

Browse files
Switch from single connection to pool.
1 parent a09016c commit bd9e5b3

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

db_wrapper/client/async_client.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
Dict)
1313

1414
import aiopg
15-
from psycopg2.extras import register_uuid, RealDictRow
15+
from psycopg2.extras import register_uuid, RealDictCursor, RealDictRow # type: ignore
1616
from psycopg2 import sql
1717

18-
from db_wrapper.connection import ConnectionParameters, connect
18+
from db_wrapper.connection import ConnectionParameters, get_pool
1919

2020
# add uuid support to psycopg2 & Postgres
2121
register_uuid()
@@ -33,18 +33,19 @@ class AsyncClient:
3333
"""
3434

3535
_connection_params: ConnectionParameters
36-
_connection: aiopg.Connection
36+
_pool: aiopg.Pool
3737

3838
def __init__(self, connection_params: ConnectionParameters) -> None:
3939
self._connection_params = connection_params
4040

4141
async def connect(self) -> None:
42-
"""Connect to the database."""
43-
self._connection = await connect(self._connection_params)
42+
"""Create a database connection pool."""
43+
self._pool = await get_pool(self._connection_params)
4444

4545
async def disconnect(self) -> None:
46-
"""Disconnect from the database."""
47-
await self._connection.close()
46+
"""Close database connection pool."""
47+
self._pool.close()
48+
await self._pool.wait_closed()
4849

4950
# PENDS python 3.9 support in pylint
5051
# pylint: disable=unsubscriptable-object
@@ -81,7 +82,7 @@ async def execute(
8182
Returns:
8283
None
8384
"""
84-
async with self._connection.cursor() as cursor:
85+
with (await self._pool.cursor(cursor_factory=RealDictCursor) ) as cursor:
8586
await self._execute_query(cursor, query, params)
8687

8788
# PENDS python 3.9 support in pylint
@@ -101,7 +102,7 @@ async def execute_and_return(
101102
Returns:
102103
List containing all the rows that matched the query.
103104
"""
104-
async with self._connection.cursor() as cursor:
105+
with (await self._pool.cursor(cursor_factory=RealDictCursor) ) as cursor:
105106
await self._execute_query(cursor, query, params)
106107

107108
result: List[RealDictRow] = await cursor.fetchall()

db_wrapper/connection.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ async def _try_connect(
4141
dsn = f"dbname={database} user={user} password={password} " \
4242
f"host={host} port={port}"
4343

44+
# return await aiopg.create_pool(dsn)
45+
4446
# PENDS python 3.9 support in pylint
4547
# pylint: disable=unsubscriptable-object
46-
connection: Optional[aiopg.Connection] = None
48+
pool: Optional[aiopg.Connection] = None
4749

4850
LOGGER.info(f"Attempting to connect to database {database} as "
4951
f"{user}@{host}:{port}...")
5052

51-
while connection is None:
53+
while pool is None:
5254
try:
53-
connection = await aiopg.connect(
54-
dsn,
55-
cursor_factory=RealDictCursor)
55+
pool = await aiopg.create_pool(dsn)
5656
except psycopg2OpError as err:
5757
print(type(err))
5858
if retries > 12:
@@ -67,7 +67,7 @@ async def _try_connect(
6767
await asyncio.sleep(5)
6868
return await _try_connect(connection_params, retries + 1)
6969

70-
return connection
70+
return pool
7171

7272

7373
def _sync_try_connect(
@@ -112,10 +112,10 @@ def _sync_try_connect(
112112

113113
# PENDS python 3.9 support in pylint
114114
# pylint: disable=unsubscriptable-object
115-
async def connect(
115+
async def get_pool(
116116
connection_params: ConnectionParameters
117-
) -> aiopg.Connection:
118-
"""Establish database connection."""
117+
) -> aiopg.Pool:
118+
"""Establish database connection pool."""
119119
return await _try_connect(connection_params)
120120

121121

0 commit comments

Comments
 (0)