@@ -210,7 +210,12 @@ async def release(self, timeout):
210210 if budget is not None :
211211 budget -= time .monotonic () - started
212212
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 )
214219 except (Exception , asyncio .CancelledError ) as ex :
215220 # If the `reset` call failed, terminate the connection.
216221 # A new one will be created when `acquire` is called
@@ -313,7 +318,7 @@ class Pool:
313318
314319 __slots__ = (
315320 '_queue' , '_loop' , '_minsize' , '_maxsize' ,
316- '_init' , '_connect' , '_connect_args' , '_connect_kwargs' ,
321+ '_init' , '_connect' , '_reset' , ' _connect_args' , '_connect_kwargs' ,
317322 '_holders' , '_initialized' , '_initializing' , '_closing' ,
318323 '_closed' , '_connection_class' , '_record_class' , '_generation' ,
319324 '_setup' , '_max_queries' , '_max_inactive_connection_lifetime'
@@ -327,6 +332,7 @@ def __init__(self, *connect_args,
327332 connect = None ,
328333 setup = None ,
329334 init = None ,
335+ reset = None ,
330336 loop ,
331337 connection_class ,
332338 record_class ,
@@ -393,6 +399,7 @@ def __init__(self, *connect_args,
393399
394400 self ._setup = setup
395401 self ._init = init
402+ self ._reset = reset
396403
397404 self ._max_queries = max_queries
398405 self ._max_inactive_connection_lifetime = \
@@ -1036,6 +1043,7 @@ def create_pool(dsn=None, *,
10361043 connect = None ,
10371044 setup = None ,
10381045 init = None ,
1046+ reset = None ,
10391047 loop = None ,
10401048 connection_class = connection .Connection ,
10411049 record_class = protocol .Record ,
@@ -1137,6 +1145,25 @@ def create_pool(dsn=None, *,
11371145 or :meth:`Connection.set_type_codec() <\
11381146 asyncpg.connection.Connection.set_type_codec>`.
11391147
1148+ :param coroutine reset:
1149+ A coroutine to reset a connection before it is returned to the pool by
1150+ :meth:`Pool.release() <pool.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+
11401167 :param loop:
11411168 An asyncio event loop instance. If ``None``, the default
11421169 event loop will be used.
@@ -1165,7 +1192,7 @@ def create_pool(dsn=None, *,
11651192 Added the *record_class* parameter.
11661193
11671194 .. versionchanged:: 0.30.0
1168- Added the *connect* parameter .
1195+ Added the *connect* and *reset* parameters .
11691196 """
11701197 return Pool (
11711198 dsn ,
@@ -1178,6 +1205,7 @@ def create_pool(dsn=None, *,
11781205 connect = connect ,
11791206 setup = setup ,
11801207 init = init ,
1208+ reset = reset ,
11811209 max_inactive_connection_lifetime = max_inactive_connection_lifetime ,
11821210 ** connect_kwargs ,
11831211 )
0 commit comments