diff --git a/conveyor/config.py b/conveyor/config.py index 5896388..d3baa4f 100644 --- a/conveyor/config.py +++ b/conveyor/config.py @@ -12,6 +12,7 @@ import asyncio import os +import threading import aiohttp import aiohttp.web @@ -26,10 +27,7 @@ async def session_close(app): http_session = app.get("http.session") if http_session is not None: await http_session.close() - boto_session = app.get("boto.session") - if boto_session is not None: - await boto_session.close() - + app.get("boto.session")().close() async def cancel_tasks(app): for task in app["tasks"]: @@ -51,7 +49,15 @@ def configure(): loop=asyncio.get_event_loop(), headers={"User-Agent": "conveyor"}, ) - app["boto.session"] = aiobotocore_get_session() + + # https://github.com/boto/botocore/issues/2047#issuecomment-1251318969 + _aio_session_cache = threading.local() + def _cached_aiobotocore_session(): + if not hasattr(_aio_session_cache, "session"): + _aio_session_cache.session = aiobotocore_get_session() + return _aio_session_cache.session + app["boto.session"] = _cached_aiobotocore_session + app.on_shutdown.append(session_close) app["tasks"] = [] diff --git a/conveyor/tasks.py b/conveyor/tasks.py index e051bc0..245af1a 100644 --- a/conveyor/tasks.py +++ b/conveyor/tasks.py @@ -23,7 +23,7 @@ async def redirects_refresh_task(app): try: try: bucket = app["settings"]["docs_bucket"] - session = app["boto.session"] + session = app["boto.session"]() etag = app["redirects"].get('_ETag') async with session.create_client('s3', config=ANON_CONFIG) as s3: try: diff --git a/conveyor/views.py b/conveyor/views.py index bf52921..37f660f 100644 --- a/conveyor/views.py +++ b/conveyor/views.py @@ -114,7 +114,7 @@ async def fetch_key(s3, request, bucket, key): async def index(request): bucket = request.app["settings"]["docs_bucket"] - session = request.app["boto.session"] + session = request.app["boto.session"]() path = "index.html" async with session.create_client('s3', config=ANON_CONFIG) as s3: @@ -165,7 +165,7 @@ async def documentation(request): path += "index.html" bucket = request.app["settings"]["docs_bucket"] - session = request.app["boto.session"] + session = request.app["boto.session"]() async with session.create_client('s3', config=ANON_CONFIG) as s3: try: diff --git a/tests/test_config.py b/tests/test_config.py index 47e8a60..41eb56b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -24,7 +24,7 @@ async def close(): app = { "http.session": pretend.stub(close=close), - "boto.session": pretend.stub(close=close), + "boto.session": lambda: pretend.stub(close=close), } await session_close(app)