Skip to content

feat(async-client): add session pooling for connection reuse TAV-4105#154

Closed
tinosattavily wants to merge 2 commits into
masterfrom
feat/TAV-4105-session-pooling
Closed

feat(async-client): add session pooling for connection reuse TAV-4105#154
tinosattavily wants to merge 2 commits into
masterfrom
feat/TAV-4105-session-pooling

Conversation

@tinosattavily
Copy link
Copy Markdown
Contributor

Summary

  • Add async session pooling: Replace per-request httpx.AsyncClient creation with persistent client for TCP connection reuse
  • Add lifecycle management: close(), __aenter__, __aexit__ for proper resource cleanup
  • Simplify codebase: Extract duplicated code into helper methods, consolidate method pairs
  • Add edge case tests: 9 new tests for session pooling in test_session_pooling.py
  • Fix test fixture bug: test_errors.py fixture crash when TAVILY_API_KEY unset

Performance

Client No Pooling With Pooling Speedup
Sync ~1000ms ~300ms 3x faster
Async ~600ms ~280ms 2-10x faster

Code Reduction

  • Before: 786 lines
  • After: 476 lines
  • Reduction: 39% smaller

Changes

tavily/async_tavily.py

  • _client_creator lambda → self._client persistent instance
  • Added _handle_error_response() - centralized error handling
  • Added _post() - centralized POST + timeout logic
  • Added _handle_stream_error() - streaming error handling
  • Consolidated _search/search, _extract/extract, etc. into single methods

tests/test_session_pooling.py (NEW)

  • Context manager tests (sync + async)
  • close() method tests
  • Multiple sequential requests tests
  • Concurrent requests test

tests/test_errors.py

  • Fixed set_api_key fixture that crashed when env var was not set

Test plan

  • All 30 unit tests pass
  • Local integration tests pass (34/34)
  • Session pooling comparison benchmarks verified
  • Context manager usage verified
  • close() method verified

Fixes TAV-4105

🤖 Generated with Claude Code

- Replace per-request client creation with persistent httpx.AsyncClient
- Add close(), __aenter__, __aexit__ for lifecycle management
- Extract duplicated error handling into _handle_error_response()
- Add _post() helper for centralized request logic
- Consolidate _method/method pairs into single public methods
- Add test_session_pooling.py with 9 edge case tests
- Fix test_errors.py fixture crash when TAVILY_API_KEY unset

Performance: 2-10x faster for sequential requests (TCP connection reuse)
Code reduction: 786 → 476 lines (39% smaller)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@tinosattavily tinosattavily added feature New feature or enhancement priority-medium Medium priority task size-l Large change labels Jan 28, 2026
@konstantinosanagn
Copy link
Copy Markdown

Closing to create a new PR with reverted code simplification changes (keeping only session pooling)

@tinosattavily
Copy link
Copy Markdown
Contributor Author

Closing to create a new PR with reverted code simplification (keeping only session pooling feature)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or enhancement priority-medium Medium priority task size-l Large change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants