@@ -313,7 +313,7 @@ class Pool:
313
313
314
314
__slots__ = (
315
315
'_queue' , '_loop' , '_minsize' , '_maxsize' ,
316
- '_init' , '_connect_fn ' , '_connect_args' , '_connect_kwargs' ,
316
+ '_init' , '_connect ' , '_connect_args' , '_connect_kwargs' ,
317
317
'_holders' , '_initialized' , '_initializing' , '_closing' ,
318
318
'_closed' , '_connection_class' , '_record_class' , '_generation' ,
319
319
'_setup' , '_max_queries' , '_max_inactive_connection_lifetime'
@@ -324,12 +324,12 @@ def __init__(self, *connect_args,
324
324
max_size ,
325
325
max_queries ,
326
326
max_inactive_connection_lifetime ,
327
- setup ,
328
- init ,
327
+ connect = None ,
328
+ setup = None ,
329
+ init = None ,
329
330
loop ,
330
331
connection_class ,
331
332
record_class ,
332
- connect_fn ,
333
333
** connect_kwargs ):
334
334
335
335
if len (connect_args ) > 1 :
@@ -386,12 +386,14 @@ def __init__(self, *connect_args,
386
386
self ._closing = False
387
387
self ._closed = False
388
388
self ._generation = 0
389
- self ._init = init
389
+
390
+ self ._connect = connect if connect is not None else connection .connect
390
391
self ._connect_args = connect_args
391
392
self ._connect_kwargs = connect_kwargs
392
- self ._connect_fn = connect_fn
393
393
394
394
self ._setup = setup
395
+ self ._init = init
396
+
395
397
self ._max_queries = max_queries
396
398
self ._max_inactive_connection_lifetime = \
397
399
max_inactive_connection_lifetime
@@ -505,13 +507,25 @@ def set_connect_args(self, dsn=None, **connect_kwargs):
505
507
self ._connect_kwargs = connect_kwargs
506
508
507
509
async def _get_new_connection (self ):
508
- con = await self ._connect_fn (
510
+ con = await self ._connect (
509
511
* self ._connect_args ,
510
512
loop = self ._loop ,
511
513
connection_class = self ._connection_class ,
512
514
record_class = self ._record_class ,
513
515
** self ._connect_kwargs ,
514
516
)
517
+ if not isinstance (con , self ._connection_class ):
518
+ good = self ._connection_class
519
+ good_n = f'{ good .__module__ } .{ good .__name__ } '
520
+ bad = type (con )
521
+ if bad .__module__ == "builtins" :
522
+ bad_n = bad .__name__
523
+ else :
524
+ bad_n = f'{ bad .__module__ } .{ bad .__name__ } '
525
+ raise exceptions .InterfaceError (
526
+ "expected pool connect callback to return an instance of "
527
+ f"'{ good_n } ', got " f"'{ bad_n } '"
528
+ )
515
529
516
530
if self ._init is not None :
517
531
try :
@@ -1003,6 +1017,7 @@ def create_pool(dsn=None, *,
1003
1017
max_size = 10 ,
1004
1018
max_queries = 50000 ,
1005
1019
max_inactive_connection_lifetime = 300.0 ,
1020
+ connect = None ,
1006
1021
setup = None ,
1007
1022
init = None ,
1008
1023
loop = None ,
@@ -1085,6 +1100,13 @@ def create_pool(dsn=None, *,
1085
1100
Number of seconds after which inactive connections in the
1086
1101
pool will be closed. Pass ``0`` to disable this mechanism.
1087
1102
1103
+ :param coroutine connect:
1104
+ A coroutine that is called instead of
1105
+ :func:`~asyncpg.connection.connect` whenever the pool needs to make a
1106
+ new connection. Must return an instance of type specified by
1107
+ *connection_class* or :class:`~asyncpg.connection.Connection` if
1108
+ *connection_class* was not specified.
1109
+
1088
1110
:param coroutine setup:
1089
1111
A coroutine to prepare a connection right before it is returned
1090
1112
from :meth:`Pool.acquire() <pool.Pool.acquire>`. An example use
@@ -1099,10 +1121,6 @@ def create_pool(dsn=None, *,
1099
1121
or :meth:`Connection.set_type_codec() <\
1100
1122
asyncpg.connection.Connection.set_type_codec>`.
1101
1123
1102
- :param coroutine connect_fn:
1103
- A coroutine with signature identical to :func:`~asyncpg.connection.connect`. This can be used to add custom
1104
- authentication or ssl logic when creating a connection, as is required by GCP's cloud-sql-python-connector.
1105
-
1106
1124
:param loop:
1107
1125
An asyncio event loop instance. If ``None``, the default
1108
1126
event loop will be used.
@@ -1129,12 +1147,21 @@ def create_pool(dsn=None, *,
1129
1147
1130
1148
.. versionchanged:: 0.22.0
1131
1149
Added the *record_class* parameter.
1150
+
1151
+ .. versionchanged:: 0.30.0
1152
+ Added the *connect* parameter.
1132
1153
"""
1133
1154
return Pool (
1134
1155
dsn ,
1135
1156
connection_class = connection_class ,
1136
- record_class = record_class , connect_fn = connection .connect ,
1137
- min_size = min_size , max_size = max_size ,
1138
- max_queries = max_queries , loop = loop , setup = setup , init = init ,
1157
+ record_class = record_class ,
1158
+ min_size = min_size ,
1159
+ max_size = max_size ,
1160
+ max_queries = max_queries ,
1161
+ loop = loop ,
1162
+ connect = connect ,
1163
+ setup = setup ,
1164
+ init = init ,
1139
1165
max_inactive_connection_lifetime = max_inactive_connection_lifetime ,
1140
- ** connect_kwargs )
1166
+ ** connect_kwargs ,
1167
+ )
0 commit comments