Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

share aiohttp.ClientSession between clients #797

Closed
adamko147 opened this issue Apr 11, 2020 · 4 comments · Fixed by #844
Closed

share aiohttp.ClientSession between clients #797

adamko147 opened this issue Apr 11, 2020 · 4 comments · Fixed by #844

Comments

@adamko147
Copy link

According to documentation at https://docs.aiohttp.org/en/stable/client_reference.html#client-reference it's suggested to use single ClientSession for app

Session encapsulates a connection pool (connector instance) and supports keepalives by default. Unless you are connecting to a large, unknown number of different servers over the lifetime of your application, it is suggested you use a single session for the lifetime of your application to benefit from connection pooling.

Is there any way how to achieve sharing single ClientSession in aiobotocore accross multiple clients? I'm running multiple requests to AWS resources and each client uses own ClientSession.

Thanks

@dazza-codes
Copy link
Contributor

dazza-codes commented Apr 14, 2020

My 2c - good question - but aiobotocore is kinda tied to botocore design patterns (AFAIK) and it has to stick with the patterns in aiobotocore.endpoint.AioEndpointCreator to instantiate the aiohttp.ClientSession on the endpoint_instance.http_session.

In botocore, if you want to try to override things by first creating a single client and then using it everywhere else (for a given AWS service, of course), you could register a handler for the create-client event. But aiobotocore is not using the same event registration/handler pattern for clients to ensure that it uses the aiohttp client (AFAICT).

Details are in

  • aiobotocore.session.AioSession.create_client
  • aiobotocore.endpoint.AioEndpointCreator

Some related notes on botocore that delve into how it constructs clients and connection pools is at https://gitlab.com/dazza-codes/aio-aws/-/wikis/botocore-clients

@adamko147
Copy link
Author

adamko147 commented Apr 15, 2020

Thanks @dazza-codes for your reply. I can see the pattern in botocore. Unless aiobotocore wants to strictly follow that, it would be interesting to find out how to share the http_session. maybe optional parameter to create_client or new create-aio-session event? I could try to do the work, if you think it's worth to try. Thanks

@dazza-codes
Copy link
Contributor

dazza-codes commented Apr 15, 2020

See also #749 and #791 (using connection-args maybe)

@thehesiod
Copy link
Collaborator

aiobotocore follows botocore, which my default uses a session per client, however, it allows you to pass in your own session class, see https://github.com/boto/botocore/blob/develop/botocore/endpoint.py#L280 so you can try using that however it doesn't seemed to be hooked up all the way in botocore. Fixing a small bug of this in #844

@thehesiod thehesiod linked a pull request Jan 12, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants