@@ -313,7 +313,7 @@ class Pool:
313
313
314
314
__slots__ = (
315
315
'_queue' , '_loop' , '_minsize' , '_maxsize' ,
316
- '_init' , '_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,8 +324,9 @@ 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 ,
@@ -385,11 +386,14 @@ def __init__(self, *connect_args,
385
386
self ._closing = False
386
387
self ._closed = False
387
388
self ._generation = 0
388
- self ._init = init
389
+
390
+ self ._connect = connect if connect is not None else connection .connect
389
391
self ._connect_args = connect_args
390
392
self ._connect_kwargs = connect_kwargs
391
393
392
394
self ._setup = setup
395
+ self ._init = init
396
+
393
397
self ._max_queries = max_queries
394
398
self ._max_inactive_connection_lifetime = \
395
399
max_inactive_connection_lifetime
@@ -503,13 +507,25 @@ def set_connect_args(self, dsn=None, **connect_kwargs):
503
507
self ._connect_kwargs = connect_kwargs
504
508
505
509
async def _get_new_connection (self ):
506
- con = await connection . connect (
510
+ con = await self . _connect (
507
511
* self ._connect_args ,
508
512
loop = self ._loop ,
509
513
connection_class = self ._connection_class ,
510
514
record_class = self ._record_class ,
511
515
** self ._connect_kwargs ,
512
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
+ )
513
529
514
530
if self ._init is not None :
515
531
try :
@@ -1017,6 +1033,7 @@ def create_pool(dsn=None, *,
1017
1033
max_size = 10 ,
1018
1034
max_queries = 50000 ,
1019
1035
max_inactive_connection_lifetime = 300.0 ,
1036
+ connect = None ,
1020
1037
setup = None ,
1021
1038
init = None ,
1022
1039
loop = None ,
@@ -1099,6 +1116,13 @@ def create_pool(dsn=None, *,
1099
1116
Number of seconds after which inactive connections in the
1100
1117
pool will be closed. Pass ``0`` to disable this mechanism.
1101
1118
1119
+ :param coroutine connect:
1120
+ A coroutine that is called instead of
1121
+ :func:`~asyncpg.connection.connect` whenever the pool needs to make a
1122
+ new connection. Must return an instance of type specified by
1123
+ *connection_class* or :class:`~asyncpg.connection.Connection` if
1124
+ *connection_class* was not specified.
1125
+
1102
1126
:param coroutine setup:
1103
1127
A coroutine to prepare a connection right before it is returned
1104
1128
from :meth:`Pool.acquire() <pool.Pool.acquire>`. An example use
@@ -1139,12 +1163,21 @@ def create_pool(dsn=None, *,
1139
1163
1140
1164
.. versionchanged:: 0.22.0
1141
1165
Added the *record_class* parameter.
1166
+
1167
+ .. versionchanged:: 0.30.0
1168
+ Added the *connect* parameter.
1142
1169
"""
1143
1170
return Pool (
1144
1171
dsn ,
1145
1172
connection_class = connection_class ,
1146
1173
record_class = record_class ,
1147
- min_size = min_size , max_size = max_size ,
1148
- max_queries = max_queries , loop = loop , setup = setup , init = init ,
1174
+ min_size = min_size ,
1175
+ max_size = max_size ,
1176
+ max_queries = max_queries ,
1177
+ loop = loop ,
1178
+ connect = connect ,
1179
+ setup = setup ,
1180
+ init = init ,
1149
1181
max_inactive_connection_lifetime = max_inactive_connection_lifetime ,
1150
- ** connect_kwargs )
1182
+ ** connect_kwargs ,
1183
+ )
0 commit comments