conn._pool = None
if conn.pgconn.transaction_status == TransactionStatus.UNKNOWN:
self._stats[self._RETURNS_BAD] += 1
- conn.close()
+ self._close_connection(conn)
self._nconns -= 1
return True
break
else:
# No client waiting for a connection: close the connection
- conn.close()
+ self._close_connection(conn)
# If we have been asked to wait for pool init, notify the
# waiter if the pool is ready.
if self._pool_full_event:
conn._pool = None
if conn.pgconn.transaction_status == TransactionStatus.UNKNOWN:
self._stats[self._RETURNS_BAD] += 1
- await conn.close()
+ await self._close_connection(conn)
self._nconns -= 1
return True
break
else:
# No client waiting for a connection: close the connection
- await conn.close()
+ await self._close_connection(conn)
# If we have been asked to wait for pool init, notify the
# waiter if the pool is ready.
if self._pool_full_event:
if not self._closed:
return False
- conn._pool = None
- conn.close()
+ self._close_connection(conn)
return True
def open(self, wait: bool = False, timeout: float = 30.0) -> None:
# Close the connections that were still in the pool
for conn in connections:
- conn._pool = None
- conn.close()
+ self._close_connection(conn)
# Signal to eventual clients in the queue that business is closed.
for pos in waiting:
# Check for expired connections
if conn._expire_at <= monotonic():
logger.info("discarding expired connection %s", conn)
- conn._pool = None
- conn.close()
+ self._close_connection(conn)
self.run_task(AddConnection(self))
continue
try:
conn = self._connect()
except Exception as ex:
- logger.warning(f"error connecting in {self.name!r}: {ex}")
+ logger.warning("error connecting in %r: %s", self.name, ex)
if attempt.time_to_give_up(now):
logger.warning(
"reconnection attempt in pool %r failed after %s sec",
# Check if the connection is past its best before date
if conn._expire_at <= monotonic():
logger.info("discarding expired connection")
- conn._pool = None
- conn.close()
+ self._close_connection(conn)
self.run_task(AddConnection(self))
return
# between here and entering the lock. Therefore we will make another
# check later.
if self._closed:
- conn.close()
+ self._close_connection(conn)
return
# Critical section: if there is a client waiting give it the connection
# this connection while the main process is closing the pool.
# Now that we are in the critical section we know for real.
if self._closed:
- conn.close()
+ self._close_connection(conn)
return
while self._waiting:
ex,
conn,
)
- conn._pool = None
- conn.close()
+ self._close_connection(conn)
elif status == TransactionStatus.ACTIVE:
# Connection returned during an operation. Bad... just close it.
logger.warning("closing returned connection: %s", conn)
- conn._pool = None
- conn.close()
+ self._close_connection(conn)
if self._reset:
try:
f"connection left in status {sname} by reset function {self._reset}: discarded"
)
except Exception as ex:
- logger.warning(f"error resetting connection: {ex}")
- conn._pool = None
- conn.close()
+ logger.warning("error resetting connection: %s", ex)
+ self._close_connection(conn)
+
+ def _close_connection(self, conn: CT) -> None:
+ conn._pool = None
+ conn.close()
def _shrink_pool(self) -> None:
to_close: CT | None = None
nconns_min,
self.max_idle,
)
- to_close._pool = None
- to_close.close()
+ self._close_connection(to_close)
def _get_measures(self) -> dict[str, int]:
rv = super()._get_measures()
if not self._closed:
return False
- conn._pool = None
- await conn.close()
+ await self._close_connection(conn)
return True
async def open(self, wait: bool = False, timeout: float = 30.0) -> None:
# Close the connections that were still in the pool
for conn in connections:
- conn._pool = None
- await conn.close()
+ await self._close_connection(conn)
# Signal to eventual clients in the queue that business is closed.
for pos in waiting:
# Check for expired connections
if conn._expire_at <= monotonic():
logger.info("discarding expired connection %s", conn)
- conn._pool = None
- await conn.close()
+ await self._close_connection(conn)
self.run_task(AddConnection(self))
continue
try:
conn = await self._connect()
except Exception as ex:
- logger.warning(f"error connecting in {self.name!r}: {ex}")
+ logger.warning("error connecting in %r: %s", self.name, ex)
if attempt.time_to_give_up(now):
logger.warning(
"reconnection attempt in pool %r failed after %s sec",
# Check if the connection is past its best before date
if conn._expire_at <= monotonic():
logger.info("discarding expired connection")
- conn._pool = None
- await conn.close()
+ await self._close_connection(conn)
self.run_task(AddConnection(self))
return
# between here and entering the lock. Therefore we will make another
# check later.
if self._closed:
- await conn.close()
+ await self._close_connection(conn)
return
# Critical section: if there is a client waiting give it the connection
# this connection while the main process is closing the pool.
# Now that we are in the critical section we know for real.
if self._closed:
- await conn.close()
+ await self._close_connection(conn)
return
while self._waiting:
ex,
conn,
)
- conn._pool = None
- await conn.close()
+ await self._close_connection(conn)
elif status == TransactionStatus.ACTIVE:
# Connection returned during an operation. Bad... just close it.
logger.warning("closing returned connection: %s", conn)
- conn._pool = None
- await conn.close()
+ await self._close_connection(conn)
if self._reset:
try:
f" {self._reset}: discarded"
)
except Exception as ex:
- logger.warning(f"error resetting connection: {ex}")
- conn._pool = None
- await conn.close()
+ logger.warning("error resetting connection: %s", ex)
+ await self._close_connection(conn)
+
+ async def _close_connection(self, conn: ACT) -> None:
+ conn._pool = None
+ await conn.close()
async def _shrink_pool(self) -> None:
to_close: ACT | None = None
nconns_min,
self.max_idle,
)
- to_close._pool = None
- await to_close.close()
+ await self._close_connection(to_close)
def _get_measures(self) -> dict[str, int]:
rv = super()._get_measures()