Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect_fn kwarg to Pool to better support GCP's CloudSQL #1170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion asyncpg/_testbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,15 @@ def create_pool(dsn=None, *,
pool_class=pg_pool.Pool,
connection_class=pg_connection.Connection,
record_class=asyncpg.Record,
connect_fn=pg_connection.connect,
**connect_kwargs):
return pool_class(
dsn,
min_size=min_size, max_size=max_size,
max_queries=max_queries, loop=loop, setup=setup, init=init,
max_inactive_connection_lifetime=max_inactive_connection_lifetime,
connection_class=connection_class,
record_class=record_class,
record_class=record_class, connect_fn=connect_fn,
**connect_kwargs)


Expand Down
12 changes: 9 additions & 3 deletions asyncpg/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class Pool:

__slots__ = (
'_queue', '_loop', '_minsize', '_maxsize',
'_init', '_connect_args', '_connect_kwargs',
'_init', '_connect_fn', '_connect_args', '_connect_kwargs',
'_holders', '_initialized', '_initializing', '_closing',
'_closed', '_connection_class', '_record_class', '_generation',
'_setup', '_max_queries', '_max_inactive_connection_lifetime'
Expand All @@ -329,6 +329,7 @@ def __init__(self, *connect_args,
loop,
connection_class,
record_class,
connect_fn,
**connect_kwargs):

if len(connect_args) > 1:
Expand Down Expand Up @@ -388,6 +389,7 @@ def __init__(self, *connect_args,
self._init = init
self._connect_args = connect_args
self._connect_kwargs = connect_kwargs
self._connect_fn = connect_fn

self._setup = setup
self._max_queries = max_queries
Expand Down Expand Up @@ -503,7 +505,7 @@ def set_connect_args(self, dsn=None, **connect_kwargs):
self._connect_kwargs = connect_kwargs

async def _get_new_connection(self):
con = await connection.connect(
con = await self._connect_fn(
*self._connect_args,
loop=self._loop,
connection_class=self._connection_class,
Expand Down Expand Up @@ -1097,6 +1099,10 @@ def create_pool(dsn=None, *,
or :meth:`Connection.set_type_codec() <\
asyncpg.connection.Connection.set_type_codec>`.

:param coroutine connect_fn:
A coroutine with signature identical to :func:`~asyncpg.connection.connect`. This can be used to add custom
authentication or ssl logic when creating a connection, as is required by GCP's cloud-sql-python-connector.

:param loop:
An asyncio event loop instance. If ``None``, the default
event loop will be used.
Expand Down Expand Up @@ -1127,7 +1133,7 @@ def create_pool(dsn=None, *,
return Pool(
dsn,
connection_class=connection_class,
record_class=record_class,
record_class=record_class, connect_fn=connection.connect,
min_size=min_size, max_size=max_size,
max_queries=max_queries, loop=loop, setup=setup, init=init,
max_inactive_connection_lifetime=max_inactive_connection_lifetime,
Expand Down