@@ -210,7 +210,12 @@ async def release(self, timeout):
210
210
if budget is not None :
211
211
budget -= time .monotonic () - started
212
212
213
- await self ._con .reset (timeout = budget )
213
+ if self ._pool ._reset is not None :
214
+ async with compat .timeout (budget ):
215
+ await self ._con ._reset ()
216
+ await self ._pool ._reset (self ._con )
217
+ else :
218
+ await self ._con .reset (timeout = budget )
214
219
except (Exception , asyncio .CancelledError ) as ex :
215
220
# If the `reset` call failed, terminate the connection.
216
221
# A new one will be created when `acquire` is called
@@ -313,7 +318,7 @@ class Pool:
313
318
314
319
__slots__ = (
315
320
'_queue' , '_loop' , '_minsize' , '_maxsize' ,
316
- '_init' , '_connect' , '_connect_args' , '_connect_kwargs' ,
321
+ '_init' , '_connect' , '_reset' , ' _connect_args' , '_connect_kwargs' ,
317
322
'_holders' , '_initialized' , '_initializing' , '_closing' ,
318
323
'_closed' , '_connection_class' , '_record_class' , '_generation' ,
319
324
'_setup' , '_max_queries' , '_max_inactive_connection_lifetime'
@@ -327,6 +332,7 @@ def __init__(self, *connect_args,
327
332
connect = None ,
328
333
setup = None ,
329
334
init = None ,
335
+ reset = None ,
330
336
loop ,
331
337
connection_class ,
332
338
record_class ,
@@ -393,6 +399,7 @@ def __init__(self, *connect_args,
393
399
394
400
self ._setup = setup
395
401
self ._init = init
402
+ self ._reset = reset
396
403
397
404
self ._max_queries = max_queries
398
405
self ._max_inactive_connection_lifetime = \
@@ -1036,6 +1043,7 @@ def create_pool(dsn=None, *,
1036
1043
connect = None ,
1037
1044
setup = None ,
1038
1045
init = None ,
1046
+ reset = None ,
1039
1047
loop = None ,
1040
1048
connection_class = connection .Connection ,
1041
1049
record_class = protocol .Record ,
@@ -1125,7 +1133,7 @@ def create_pool(dsn=None, *,
1125
1133
1126
1134
:param coroutine setup:
1127
1135
A coroutine to prepare a connection right before it is returned
1128
- from :meth:`Pool.acquire() <pool.Pool.acquire> `. An example use
1136
+ from :meth:`Pool.acquire()`. An example use
1129
1137
case would be to automatically set up notifications listeners for
1130
1138
all connections of a pool.
1131
1139
@@ -1137,6 +1145,25 @@ def create_pool(dsn=None, *,
1137
1145
or :meth:`Connection.set_type_codec() <\
1138
1146
asyncpg.connection.Connection.set_type_codec>`.
1139
1147
1148
+ :param coroutine reset:
1149
+ A coroutine to reset a connection before it is returned to the pool by
1150
+ :meth:`Pool.release()`. The function is supposed
1151
+ to reset any changes made to the database session so that the next
1152
+ acquirer gets the connection in a well-defined state.
1153
+
1154
+ The default implementation calls :meth:`Connection.reset() <\
1155
+ asyncpg.connection.Connection.reset>`, which runs the following::
1156
+
1157
+ SELECT pg_advisory_unlock_all();
1158
+ CLOSE ALL;
1159
+ UNLISTEN *;
1160
+ RESET ALL;
1161
+
1162
+ The exact reset query is determined by detected server capabilities,
1163
+ and a custom *reset* implementation can obtain the default query
1164
+ by calling :meth:`Connection.get_reset_query() <\
1165
+ asyncpg.connection.Connection.get_reset_query>`.
1166
+
1140
1167
:param loop:
1141
1168
An asyncio event loop instance. If ``None``, the default
1142
1169
event loop will be used.
@@ -1165,7 +1192,7 @@ def create_pool(dsn=None, *,
1165
1192
Added the *record_class* parameter.
1166
1193
1167
1194
.. versionchanged:: 0.30.0
1168
- Added the *connect* parameter .
1195
+ Added the *connect* and *reset* parameters .
1169
1196
"""
1170
1197
return Pool (
1171
1198
dsn ,
@@ -1178,6 +1205,7 @@ def create_pool(dsn=None, *,
1178
1205
connect = connect ,
1179
1206
setup = setup ,
1180
1207
init = init ,
1208
+ reset = reset ,
1181
1209
max_inactive_connection_lifetime = max_inactive_connection_lifetime ,
1182
1210
** connect_kwargs ,
1183
1211
)
0 commit comments