Skip to content

Use a shared aiohttp.ClientSession rather than creating a new one per-request #2788

Closed

Description

Problem

aiohttp.ClientSession is expensive to initialise and should be shared application-wide rather than re-created per request.

Description

In order to close the session we'll likely need to create a Gunicorn worker subclass that calls close on the session (wrapped in async_to_sync).

Follow the pattern from https://github.com/WordPress/openverse/blob/980ef96e0e16fc758f78be9f2aadf595f75948f6/api/api/utils/aiohttp.py for how to manage the session object and register its close.

Relevant before moving to ASGI Django because check_dead_links already uses this and creates a new session per request rather than re-using one.

Once we move to ASGI Django, we'll be able to swap out the close handler out of the Gunicorn worker subclass and into the ASGI lifecycle as demonstrated in the linked example above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

Type

No type

Projects

  • Status

    ✅ Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions