Skip to content

Reuse aiohttp sessions per client instance#384

Open
silentgeckoaudit3801 wants to merge 6 commits into
Quantarq:mainfrom
silentgeckoaudit3801:refactor/reuse-aiohttp-sessions-258
Open

Reuse aiohttp sessions per client instance#384
silentgeckoaudit3801 wants to merge 6 commits into
Quantarq:mainfrom
silentgeckoaudit3801:refactor/reuse-aiohttp-sessions-258

Conversation

@silentgeckoaudit3801

Copy link
Copy Markdown

Summary

  • reuse one lazy aiohttp.ClientSession per StellarClient instance for Horizon/RPC calls
  • reuse one lazy default-header session per APIRequest instance
  • expose close() and async context-manager helpers for both clients
  • convert the FastAPI get_stellar_client dependency to yield and close the client after each request
  • add static regression coverage that blocks per-call session construction from returning

Testing

  • Not run locally: this workspace avoids installing/running project dependencies or invasive toolchains.
  • Added static regression checks under quantara/web_app/tests/test_aiohttp_session_reuse_static.py for CI.

Closes #258



def test_stellar_client_reuses_one_session_and_exposes_close():
assert "self._session: aiohttp.ClientSession | None = None" in BLOCKCHAIN_CALL

def test_stellar_client_reuses_one_session_and_exposes_close():
assert "self._session: aiohttp.ClientSession | None = None" in BLOCKCHAIN_CALL
assert "async def _get_session(self) -> aiohttp.ClientSession" in BLOCKCHAIN_CALL
def test_stellar_client_reuses_one_session_and_exposes_close():
assert "self._session: aiohttp.ClientSession | None = None" in BLOCKCHAIN_CALL
assert "async def _get_session(self) -> aiohttp.ClientSession" in BLOCKCHAIN_CALL
assert "self._session = aiohttp.ClientSession()" in BLOCKCHAIN_CALL
assert "self._session: aiohttp.ClientSession | None = None" in BLOCKCHAIN_CALL
assert "async def _get_session(self) -> aiohttp.ClientSession" in BLOCKCHAIN_CALL
assert "self._session = aiohttp.ClientSession()" in BLOCKCHAIN_CALL
assert "async def close(self) -> None" in BLOCKCHAIN_CALL
assert "async def _get_session(self) -> aiohttp.ClientSession" in BLOCKCHAIN_CALL
assert "self._session = aiohttp.ClientSession()" in BLOCKCHAIN_CALL
assert "async def close(self) -> None" in BLOCKCHAIN_CALL
assert "await self._session.close()" in BLOCKCHAIN_CALL
assert "async def _get_session(self) -> aiohttp.ClientSession" in API_REQUEST
assert "self._session = aiohttp.ClientSession(headers=self.DEFAULT_HEADER)" in API_REQUEST
assert "async def close(self) -> None" in API_REQUEST
assert API_REQUEST.count("aiohttp.ClientSession(") == 1
assert "self._session = aiohttp.ClientSession(headers=self.DEFAULT_HEADER)" in API_REQUEST
assert "async def close(self) -> None" in API_REQUEST
assert API_REQUEST.count("aiohttp.ClientSession(") == 1
assert "async with aiohttp.ClientSession" not in API_REQUEST


def test_fastapi_dependency_closes_stellar_client_after_request():
assert "async def get_stellar_client() -> AsyncIterator[StellarClient]" in DEPENDENCIES

def test_fastapi_dependency_closes_stellar_client_after_request():
assert "async def get_stellar_client() -> AsyncIterator[StellarClient]" in DEPENDENCIES
assert "yield client" in DEPENDENCIES
def test_fastapi_dependency_closes_stellar_client_after_request():
assert "async def get_stellar_client() -> AsyncIterator[StellarClient]" in DEPENDENCIES
assert "yield client" in DEPENDENCIES
assert "await client.close()" in DEPENDENCIES No newline at end of file
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 this pull request may close these issues.

aiohttp.ClientSession per StellarClient instance

2 participants