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

same aiohttp session reuse #138

Merged
merged 9 commits into from
Dec 29, 2024
Merged

same aiohttp session reuse #138

merged 9 commits into from
Dec 29, 2024

Conversation

Grommash9
Copy link
Owner

Session Management Optimization

This MR implements shared session management across all client types to improve performance and resource utilization.

Key Changes

  • Added shared aiohttp.ClientSession management in base AioTxClient
  • Implemented connection lifecycle methods (connect(), disconnect())
  • Added async context manager support (__aenter__, __aexit__)
  • Added connection state validation

Performance Impact

Performance testing shows significant improvements in RPC call times:

Before optimization (individual sessions):

rpc call time: 0.22293 sec
rpc call time: 0.21867 sec
rpc call time: 0.30874 sec
rpc call time: 0.23210 sec
rpc call time: 0.27328 sec
rpc call time: 0.66176 sec
rpc call time: 0.20494 sec
rpc call time: 0.89157 sec  # Peak
Average: ~0.35 sec

After optimization (shared session):

rpc call time: 0.15466 sec
rpc call time: 0.24238 sec
rpc call time: 0.22527 sec
rpc call time: 0.17175 sec
rpc call time: 0.14439 sec
rpc call time: 0.09237 sec  # Best
rpc call time: 0.20504 sec
Average: ~0.18 sec

Key improvements:

  • ~48% reduction in average response time
  • More consistent performance (less variation)
  • Lower peak response times
  • Some requests as fast as 0.09 seconds

Usage Example

# Using with context manager (recommended)
async with AioTxClient("node_url") as client:
    result = await client.get_balance("address")

# Or manually managing connection
client = AioTxClient("node_url")
await client.connect()
try:
    result = await client.get_balance("address")
finally:
    await client.disconnect()

Breaking Changes

  • All client methods now require an active connection
  • Added NotConnectedError for invalid connection state
  • Clients must be explicitly connected before use

Testing

  • All existing tests updated to use new connection lifecycle
  • Added new test cases for connection management
  • Performance tests confirm consistent improvements

Migration Guide

  1. Update client instantiation to use async context manager where possible
  2. Add explicit connect/disconnect calls where needed
  3. Handle potential NotConnectedError exceptions
  4. Review any custom client implementations to ensure they properly use shared session

@Grommash9 Grommash9 force-pushed the same-aiohttp-session-reuse branch from a713fa0 to 98da0ae Compare December 29, 2024 09:50
@Grommash9 Grommash9 merged commit d8ded9a into main Dec 29, 2024
14 checks passed
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.

1 participant