Skip to content

Commit 3b4c7be

Browse files
committed
Forward connection pool parameters from django to psycopg
1 parent d4e4485 commit 3b4c7be

File tree

7 files changed

+27
-10
lines changed

7 files changed

+27
-10
lines changed

procrastinate/contrib/django/django_connector.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,19 @@ def get_worker_connector(self) -> connector.BaseAsyncConnector:
166166
"""
167167
alias = settings.settings.DATABASE_ALIAS
168168

169+
kwargs, pool_kwargs = utils.connector_params(alias)
169170
if utils.package_is_installed("psycopg") and utils.package_is_version(
170171
"psycopg", 3
171172
):
172173
from procrastinate import psycopg_connector
173174

174175
return psycopg_connector.PsycopgConnector(
175-
kwargs=utils.connector_params(alias)
176+
kwargs=kwargs, pool_kwargs=pool_kwargs
176177
)
177178
if utils.package_is_installed("aiopg"):
178179
from procrastinate.contrib.aiopg import aiopg_connector
179180

180-
return aiopg_connector.AiopgConnector(**utils.connector_params(alias))
181+
return aiopg_connector.AiopgConnector(**kwargs)
181182

182183
raise django_exceptions.ImproperlyConfigured(
183184
"You must install either psycopg(3) or aiopg to use "

procrastinate/contrib/django/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.db import connections
88

99

10-
def connector_params(alias: str = "default") -> dict[str, Any]:
10+
def connector_params(alias: str = "default") -> tuple[dict[str, Any], dict[str, Any]]:
1111
"""
1212
Returns parameters for in a format that is suitable to be passed to a
1313
connector constructor (see `howto/django`).
@@ -27,7 +27,7 @@ def connector_params(alias: str = "default") -> dict[str, Any]:
2727
params = wrapper.get_connection_params()
2828
params.pop("cursor_factory", None)
2929
params.pop("context", None)
30-
return params
30+
return params, wrapper.settings_dict.get("OPTIONS", {}).get("pool", {})
3131

3232

3333
def package_is_installed(name: str) -> bool:

procrastinate/psycopg_connector.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(
4949
pool_factory: Callable[
5050
..., psycopg_pool.AsyncConnectionPool
5151
] = psycopg_pool.AsyncConnectionPool,
52+
pool_kwargs: dict[str, Any] | None = None,
5253
**kwargs: Any,
5354
):
5455
"""
@@ -93,6 +94,7 @@ def __init__(
9394
self._json_loads = json_loads
9495
self._json_dumps = json_dumps
9596
self._pool_args = kwargs
97+
self._pool_kwargs = pool_kwargs
9698
self._sync_connector: connector.BaseConnector | None = None
9799

98100
def get_sync_connector(self) -> connector.BaseConnector:
@@ -140,24 +142,27 @@ async def open_async(
140142
self._pool_externally_set = True
141143
self._async_pool = pool
142144
else:
143-
self._async_pool = await self._create_pool(self._pool_args)
145+
self._async_pool = await self._create_pool(
146+
self._pool_args, self._pool_kwargs
147+
)
144148

145149
await self._async_pool.open(wait=True) # type: ignore
146150

147151
@wrap_exceptions()
148152
async def _create_pool(
149-
self,
150-
pool_args: dict[str, Any],
153+
self, pool_args: dict[str, Any], pool_kwargs: dict[str, Any] | None
151154
) -> psycopg_pool.AsyncConnectionPool:
155+
pool_kwargs = pool_kwargs if pool_kwargs is not None else {}
152156
return self._pool_factory(
153-
**pool_args,
157+
kwargs=pool_args.get("kwargs", {}),
154158
# Not specifying open=False raises a warning and will be deprecated.
155159
# It makes sense, as we can't really make async I/Os in a constructor.
156160
open=False,
157161
# Enables a check that will ensure the connections returned when
158162
# using the pool are still alive. If they have been closed by the
159163
# database, they will be seamlessly replaced by a new connection.
160164
check=psycopg_pool.AsyncConnectionPool.check_connection,
165+
**pool_kwargs,
161166
)
162167

163168
@wrap_exceptions()

tests/acceptance/django_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"ENGINE": "django.db.backends.postgresql",
99
"NAME": os.environ.get("PGDATABASE", "procrastinate"),
1010
"TEST": {"NAME": "procrastinate_django_test"},
11+
"OPTIONS": {"pool": {"min_size": 1, "max_size": 4}},
1112
},
1213
}
1314
INSTALLED_APPS = [

tests/unit/contrib/django/test_django_connector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from procrastinate import exceptions
88
from procrastinate.contrib.django import django_connector as django_connector_module
9+
from procrastinate.psycopg_connector import PsycopgConnector
910

1011

1112
def test_wrap_exceptions__no_cause():
@@ -18,3 +19,10 @@ def test_wrap_exceptions__with_cause():
1819
with pytest.raises(exceptions.ConnectorException):
1920
with django_connector_module.wrap_exceptions():
2021
raise django_db.DatabaseError from psycopg_errors.Error()
22+
23+
24+
def test_get_worker_connector():
25+
django_connector = django_connector_module.DjangoConnector()
26+
worker_connector = django_connector.get_worker_connector()
27+
assert isinstance(worker_connector, PsycopgConnector)
28+
assert worker_connector._pool_kwargs == {"min_size": 1, "max_size": 4}

tests/unit/contrib/django/test_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77

88
def test_connector_params():
9-
assert "dbname" in utils.connector_params()
9+
params, pool_params = utils.connector_params()
10+
assert "dbname" in params
11+
assert pool_params == {"min_size": 1, "max_size": 4}
1012

1113

1214
@pytest.mark.parametrize(

tests/unit/test_psycopg_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def test_open_async_no_pool_specified(mocker, connector):
5050

5151
await connector.open_async()
5252

53-
assert connector._create_pool.call_count == 1
53+
connector._create_pool.assert_called_once_with({}, None)
5454
assert connector._async_pool.open.await_count == 1
5555

5656

0 commit comments

Comments
 (0)