Use context managers for aio connectors

Not sure why we didn't do this initially, but this ensures that we
always close all connectors properly, and also gives much clearer scope
regarding their life-cycles.
This commit is contained in:
Hugo Osvaldo Barrera 2021-08-16 19:15:18 +02:00
parent 54e829262d
commit cf1d082628
2 changed files with 52 additions and 63 deletions

View file

@ -63,8 +63,5 @@ async def aio_session(event_loop):
@pytest.fixture
async def aio_connector(event_loop):
conn = aiohttp.TCPConnector(limit_per_host=16)
try:
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
yield conn
finally:
await conn.close()

View file

@ -127,27 +127,25 @@ def sync(ctx, collections, force_delete):
from .tasks import sync_collection
async def main(collections):
conn = aiohttp.TCPConnector(limit_per_host=16)
tasks = []
for pair_name, collections in collections:
async for collection, config in prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
):
tasks.append(
sync_collection(
collection=collection,
general=config,
force_delete=force_delete,
connector=conn,
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
tasks = []
for pair_name, collections in collections:
async for collection, config in prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
):
tasks.append(
sync_collection(
collection=collection,
general=config,
force_delete=force_delete,
connector=conn,
)
)
)
await asyncio.gather(*tasks)
await conn.close()
await asyncio.gather(*tasks)
asyncio.run(main(collections))
@ -166,28 +164,26 @@ def metasync(ctx, collections):
from .tasks import prepare_pair
async def main(collections):
conn = aiohttp.TCPConnector(limit_per_host=16)
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
for pair_name, collections in collections:
collections = prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
)
for pair_name, collections in collections:
collections = prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
)
await asyncio.gather(
*[
metasync_collection(
collection=collection,
general=config,
connector=conn,
)
async for collection, config in collections
]
)
await conn.close()
await asyncio.gather(
*[
metasync_collection(
collection=collection,
general=config,
connector=conn,
)
async for collection, config in collections
]
)
asyncio.run(main(collections))
@ -213,18 +209,15 @@ def discover(ctx, pairs, list):
config = ctx.config
async def main():
conn = aiohttp.TCPConnector(limit_per_host=16)
for pair_name in pairs or config.pairs:
await discover_collections(
status_path=config.general["status_path"],
pair=config.get_pair(pair_name),
from_cache=False,
list_collections=list,
connector=conn,
)
await conn.close()
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
for pair_name in pairs or config.pairs:
await discover_collections(
status_path=config.general["status_path"],
pair=config.get_pair(pair_name),
from_cache=False,
list_collections=list,
connector=conn,
)
asyncio.run(main())
@ -267,14 +260,13 @@ def repair(ctx, collection, repair_unsafe_uid):
click.confirm("Do you want to continue?", abort=True)
async def main():
conn = aiohttp.TCPConnector(limit_per_host=16)
await repair_collection(
ctx.config,
collection,
repair_unsafe_uid=repair_unsafe_uid,
connector=conn,
)
await conn.close()
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
await repair_collection(
ctx.config,
collection,
repair_unsafe_uid=repair_unsafe_uid,
connector=conn,
)
asyncio.run(main())