File tree Expand file tree Collapse file tree 4 files changed +34
-0
lines changed
Expand file tree Collapse file tree 4 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ pytest = "*"
1414pytest-cov = " *"
1515
1616[packages ]
17+ aiohttp = " *"
1718cachetools = " *"
1819fastapi = " *"
1920gunicorn = " *"
Original file line number Diff line number Diff line change 1313from .data import data_source
1414from .router .v1 import V1
1515from .router .v2 import V2
16+ from .utils .httputils import setup_client_session , teardown_client_session
1617
1718# ############
1819# FastAPI App
2829 version = "2.0.1" ,
2930 docs_url = "/" ,
3031 redoc_url = "/docs" ,
32+ on_startup = [setup_client_session ],
33+ on_shutdown = [teardown_client_session ],
3134)
3235
3336# #####################
Original file line number Diff line number Diff line change 1+ import logging
2+
3+ from aiohttp import ClientSession
4+
5+
6+ # Singleton aiohttp.ClientSession instance.
7+ client_session : ClientSession
8+
9+
10+ LOGGER = logging .getLogger (__name__ )
11+
12+
13+ async def setup_client_session ():
14+ """Set up the application-global aiohttp.ClientSession instance.
15+
16+ aiohttp recommends that only one ClientSession exist for the lifetime of an application.
17+ See: https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request
18+
19+ """
20+ global client_session
21+ LOGGER .info ("Setting up global aiohttp.ClientSession." )
22+ client_session = ClientSession ()
23+
24+
25+ async def teardown_client_session ():
26+ """Close the application-global aiohttp.ClientSession.
27+ """
28+ global client_session
29+ LOGGER .info ("Closing global aiohttp.ClientSession." )
30+ await client_session .close ()
You can’t perform that action at this time.
0 commit comments