with self._lock:
assert not self._pool_full_event
- if len(self._pool) >= self._nconns:
+ if len(self._pool) >= self._min_size:
return
self._pool_full_event = threading.Event()
self._add_to_pool(conn)
if growing:
with self._lock:
- if self._nconns < self._max_size and self._waiting:
+ # Keep on growing if the pool is not full yet, or if there are
+ # clients waiting and the pool can extend.
+ if self._nconns < self._min_size or (
+ self._nconns < self._max_size and self._waiting
+ ):
self._nconns += 1
logger.info("growing pool %r to %s", self.name, self._nconns)
self.run_task(AddConnection(self, growing=True))
# If we have been asked to wait for pool init, notify the
# waiter if the pool is full.
- if self._pool_full_event and len(self._pool) >= self._nconns:
+ if self._pool_full_event and len(self._pool) >= self._min_size:
self._pool_full_event.set()
def _reset_connection(self, conn: Connection[Any]) -> None:
async with self._lock:
assert not self._pool_full_event
- if len(self._pool) >= self._nconns:
+ if len(self._pool) >= self._min_size:
return
self._pool_full_event = asyncio.Event()
await self._add_to_pool(conn)
if growing:
async with self._lock:
- if self._nconns < self._max_size and self._waiting:
+ # Keep on growing if the pool is not full yet, or if there are
+ # clients waiting and the pool can extend.
+ if self._nconns < self._min_size or (
+ self._nconns < self._max_size and self._waiting
+ ):
self._nconns += 1
logger.info("growing pool %r to %s", self.name, self._nconns)
self.run_task(AddConnection(self, growing=True))
# If we have been asked to wait for pool init, notify the
# waiter if the pool is full.
- if self._pool_full_event and len(self._pool) >= self._nconns:
+ if self._pool_full_event and len(self._pool) >= self._min_size:
self._pool_full_event.set()
async def _reset_connection(self, conn: AsyncConnection[Any]) -> None:
@pytest.mark.slow
-def test_reconnect_retry(proxy):
+def test_reconnect_after_grow_failed(proxy):
+ # Retry reconnection after a failed connection attempt has put the pool
+ # in grow mode. See issue #370.
proxy.stop()
ev = Event()
ev.set()
with pool.ConnectionPool(
- proxy.client_dsn, reconnect_timeout=1.0, reconnect_failed=failed
+ proxy.client_dsn, min_size=4, reconnect_timeout=1.0, reconnect_failed=failed
) as p:
assert ev.wait(timeout=2)
with p.connection(timeout=2) as conn:
conn.execute("select 1")
+ p.wait(timeout=3.0)
+ assert len(p._pool) == p.min_size == 4
+
@pytest.mark.slow
def test_uniform_use(dsn):
@pytest.mark.slow
-async def test_reconnect_retry(proxy):
+async def test_reconnect_after_grow_failed(proxy):
+ # Retry reconnection after a failed connection attempt has put the pool
+ # in grow mode. See issue #370.
proxy.stop()
ev = asyncio.Event()
ev.set()
async with pool.AsyncConnectionPool(
- proxy.client_dsn, reconnect_timeout=1.0, reconnect_failed=failed
+ proxy.client_dsn, min_size=4, reconnect_timeout=1.0, reconnect_failed=failed
) as p:
await asyncio.wait_for(ev.wait(), 2.0)
async with p.connection(timeout=2) as conn:
await conn.execute("select 1")
+ await p.wait(timeout=3.0)
+ assert len(p._pool) == p.min_size == 4
+
@pytest.mark.slow
async def test_uniform_use(dsn):