MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / release

Method release

asyncpg/pool.py:223–292  ·  view source on GitHub ↗
(self, timeout: Optional[float])

Source from the content-addressed store, hash-verified

221 return proxy
222
223 async def release(self, timeout: Optional[float]) -> None:
224 if self._in_use is None:
225 raise exceptions.InternalClientError(
226 'PoolConnectionHolder.release() called on '
227 'a free connection holder')
228
229 if self._con.is_closed():
230 # A protocol abort may close the connection without running
231 # Connection._cleanup(). Terminate it to finish cleanup and
232 # return the holder to the pool via _release_on_close().
233 self._con.terminate()
234 return
235
236 self._timeout = None
237
238 if self._con._protocol.queries_count >= self._max_queries:
239 # The connection has reached its maximum utilization limit,
240 # so close it. Connection.close() will call _release().
241 await self._con.close(timeout=timeout)
242 return
243
244 if self._generation != self._pool._generation:
245 # The connection has expired because it belongs to
246 # an older generation (Pool.expire_connections() has
247 # been called.)
248 await self._con.close(timeout=timeout)
249 return
250
251 try:
252 budget = timeout
253
254 if self._con._protocol._is_cancelling():
255 # If the connection is in cancellation state,
256 # wait for the cancellation
257 budget = await self._con._protocol._wait_for_cancellation(
258 budget)
259
260 # The background cancellation may have timed out and terminated
261 # the connection while we were waiting. In that case cleanup
262 # has already returned the holder to the pool.
263 if self._con is None:
264 return
265 if self._con.is_closed():
266 self._con.terminate()
267 return
268
269 if self._pool._reset is not None:
270 async with compat.timeout(budget):
271 await self._con._reset()
272 await self._pool._reset(self._con)
273 else:
274 await self._con.reset(timeout=budget)
275 except (Exception, asyncio.CancelledError) as ex:
276 # If the `reset` call failed, terminate the connection.
277 # A new one will be created when `acquire` is called
278 # again.
279 try:
280 # An exception in `reset` is most likely caused by

Callers

nothing calls this directly

Calls 7

_releaseMethod · 0.95
is_closedMethod · 0.80
_resetMethod · 0.80
terminateMethod · 0.45
closeMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected