Skip to content

Commit

Permalink
Fix pool maxsize unlimited (#426)
Browse files Browse the repository at this point in the history
* fix: allow pool.maxsize to be unlimited

* tests: add test for unlimited pool size

* add test for pool minsize=1 maxsize=0

* Skip minsize validation for unlimited size pools

* Make maxsize property optional

* add changelog note

Co-authored-by: Richard Schwab <[email protected]>
  • Loading branch information
d-ryzhykau and Nothing4You authored Feb 2, 2022
1 parent 0824b26 commit e33f429
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ To be included in 1.0.0 (unreleased)
* Support PyMySQL up to version 1.0.2 #643
* Bump minimal PyMySQL version to 1.0.0 #713
* Align % formatting in Cursor.executemany() with Cursor.execute(), literal % now need to be doubled in Cursor.executemany() #714
* Fixed unlimited Pool size not working, this is now working as documented by passing maxsize=0 to create_pool #119


0.0.22 (2021-11-14)
Expand Down
6 changes: 3 additions & 3 deletions aiomysql/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class Pool(asyncio.AbstractServer):
def __init__(self, minsize, maxsize, echo, pool_recycle, loop, **kwargs):
if minsize < 0:
raise ValueError("minsize should be zero or greater")
if maxsize < minsize:
if maxsize < minsize and maxsize != 0:
raise ValueError("maxsize should be not less than minsize")
self._minsize = minsize
self._loop = loop
self._conn_kwargs = kwargs
self._acquiring = 0
self._free = collections.deque(maxlen=maxsize)
self._free = collections.deque(maxlen=maxsize or None)
self._cond = asyncio.Condition()
self._used = set()
self._terminated = set()
Expand Down Expand Up @@ -182,7 +182,7 @@ async def _fill_free_pool(self, override_min):
if self._free:
return

if override_min and self.size < self.maxsize:
if override_min and (not self.maxsize or self.size < self.maxsize):
self._acquiring += 1
try:
conn = await connect(echo=self._echo, loop=self._loop,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,21 @@ async def test_pool_drops_connection_with_exception(pool_creator, loop):
async with pool.get() as conn:
cur = await conn.cursor()
await cur.execute('SELECT 1;')


@pytest.mark.run_loop
async def test_pool_maxsize_unlimited(pool_creator, loop):
pool = await pool_creator(minsize=0, maxsize=0)

async with pool.acquire() as conn:
cur = await conn.cursor()
await cur.execute('SELECT 1;')


@pytest.mark.run_loop
async def test_pool_maxsize_unlimited_minsize_1(pool_creator, loop):
pool = await pool_creator(minsize=1, maxsize=0)

async with pool.acquire() as conn:
cur = await conn.cursor()
await cur.execute('SELECT 1;')

0 comments on commit e33f429

Please sign in to comment.