beda-software / fhir-py

FHIR Client for python
MIT License
174 stars 32 forks source link

Create aiohttp.ClientSession once per application rather than per request #93

Open dmitryashutov opened 1 year ago

dmitryashutov commented 1 year ago

In the AsyncClient._do_request we create the aiohttp.ClientSession instance for every single request ( https://github.com/beda-software/fhir-py/blob/e8410653689700fcd16cbd24edb0c64d63379af8/fhirpy/base/lib.py#L124)

Maybe it's not a big problem but the aiohttp documentation says:

Don’t create a session per request. Most likely you need a session per application which performs all requests altogether. More complex cases may require a session per site, e.g. one for Github and other one for Facebook APIs. Anyway making a session for every request is a very bad idea. A session contains a connection pool inside. Connection reusage and keep-alives (both are on by default) may speed up total performance.

It makes sense to follow the recommendation above and provide the API for closing the session during the application cleanup.

mkizesov commented 1 year ago

Also we need to keep in mind this https://docs.aiohttp.org/en/stable/client_advanced.html#graceful-shutdown

jerkos commented 1 year ago

Hello, We are using your library at our company, and we really like it. I' am interested in contributing for this issue. Are you open for it ? ClientSession object should be owned by the fhirClient (if so, client should receive args/kwargs to customize its creation) ? Or should it be created outside of the lib ? What do you think ?