Close #709.
psycopg_pool 3.2.1 (unreleased)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Respect timeout on `~ConnectionPool.connection()` when `!check` fails
+ (:ticket:`#709`).
- Use `typing.Self` as a more correct return value annotation of context
managers and other self-returning methods (see :ticket:`708`).
logger.info("pool %r is ready to use", self.name)
def _get_ready_connection(self, timeout: Optional[float]) -> Optional[CT]:
+ if timeout is not None and timeout <= 0.0:
+ raise PoolTimeout()
+
conn: Optional[CT] = None
if self.max_size == 0 or self._nconns < self.max_size:
# Create a new connection for the client
logger.info("pool %r is ready to use", self.name)
async def _get_ready_connection(self, timeout: Optional[float]) -> Optional[ACT]:
+ if timeout is not None and timeout <= 0.0:
+ raise PoolTimeout()
+
conn: Optional[ACT] = None
if self.max_size == 0 or self._nconns < self.max_size:
# Create a new connection for the client
failing to do so will deplete the pool. A depleted pool is a sad pool:
you don't want a depleted pool.
"""
- t0 = monotonic()
if timeout is None:
timeout = self.timeout
- deadline = t0 + timeout
+ deadline = monotonic() + timeout
logger.info("connection requested from %r", self.name)
self._stats[self._REQUESTS_NUM] += 1
def _get_ready_connection(self, timeout: Optional[float]) -> Optional[CT]:
"""Return a connection, if the client deserves one."""
+ if timeout is not None and timeout <= 0.0:
+ raise PoolTimeout()
+
conn: Optional[CT] = None
if self._pool:
# Take a connection ready out of the pool
failing to do so will deplete the pool. A depleted pool is a sad pool:
you don't want a depleted pool.
"""
- t0 = monotonic()
if timeout is None:
timeout = self.timeout
- deadline = t0 + timeout
+ deadline = monotonic() + timeout
logger.info("connection requested from %r", self.name)
self._stats[self._REQUESTS_NUM] += 1
async def _get_ready_connection(self, timeout: Optional[float]) -> Optional[ACT]:
"""Return a connection, if the client deserves one."""
+ if timeout is not None and timeout <= 0.0:
+ raise PoolTimeout()
+
conn: Optional[ACT] = None
if self._pool:
# Take a connection ready out of the pool
assert checked
+@pytest.mark.slow
+def test_check_timeout(pool_cls, dsn):
+ def check(conn):
+ raise Exception()
+
+ t0 = time()
+ with pytest.raises(pool.PoolTimeout):
+ with pool_cls(dsn, check=check, timeout=1.0) as p:
+ with p.connection():
+ assert False
+
+ assert time() - t0 <= 1.5
+
+
@skip_sync
def test_cancellation_in_queue(pool_cls, dsn):
# https://github.com/psycopg/psycopg/issues/509
assert checked
+@pytest.mark.slow
+async def test_check_timeout(pool_cls, dsn):
+ async def check(conn):
+ raise Exception()
+
+ t0 = time()
+ with pytest.raises(pool.PoolTimeout):
+ async with pool_cls(dsn, check=check, timeout=1.0) as p:
+ async with p.connection():
+ assert False
+
+ assert time() - t0 <= 1.5
+
+
@skip_sync
async def test_cancellation_in_queue(pool_cls, dsn):
# https://github.com/psycopg/psycopg/issues/509