feat(weaviate): implement persistent connection management
Closes #227
π Description
This PR fixes the issue where the Weaviate client automatically connects and disconnects when the LLM gives a response. The previous implementation was creating a new connection and closing it on every request, which was inefficient and could cause performance issues
π§ Changes Made
- Implemented persistent Weaviate client connection management
- Added
_connectedflag andensure_connected()to reuse active connections - Updated
get_weaviate_client()to avoid closing connections per request - Introduced
close_weaviate_client()for graceful shutdown cleanup - Fixed logging to use lazy
%formatting - Integrated Weaviate client cleanup into application lifespan (
backend/main.py)
π· Screenshots or Visual Changes (if applicable)
None
π€ Collaboration
None
β Checklist
- [x] I have read the contributing guidelines.
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] I have added necessary documentation (if applicable).
- [x] Any dependent changes have been merged and published in downstream modules.
Summary by CodeRabbit
- Refactor
- Improved database client lifecycle to reuse persistent connections with coordinated startup handling, reducing connection overhead and contention.
- Added graceful shutdown teardown to close the database client reliably and reset internal state, with improved logging and error handling for clearer diagnostics.
βοΈ Tip: You can customize this high-level summary in your review settings.
[!WARNING]
Rate limit exceeded
@vaishcodescape has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 13 minutes and 52 seconds before requesting another review.
β How to resolve this issue?
After the wait time has elapsed, a review can be triggered using the
@coderabbitai reviewcommand as a PR comment. Alternatively, push new commits to this PR.We recommend that you space out your commits to avoid hitting the rate limit.
π¦ How do rate limits work?
CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.
π₯ Commits
Reviewing files that changed from the base of the PR and between f9ae80790157bcb51d14bf5abbaaf0a41642b045 and d8b94b9327dfc43c104e56a1421edc6275c5f910.
π Files selected for processing (1)
backend/app/database/weaviate/client.py
π Walkthrough
Walkthrough
Persistent connection management for the Weaviate client was added: ensure_connected() reuses a global connection, get_weaviate_client() now relies on it, and close_weaviate_client() is invoked during FastAPI shutdown to close the client and reset internal state. Logging and docstrings adjusted.
Changes
| Cohort / File(s) | Summary |
|---|---|
Connection Management Enhancement backend/app/database/weaviate/client.py |
Added global connection state and _connect_lock; implemented async def ensure_connected() to establish/reuse a persistent Weaviate connection; added async def close_weaviate_client() to close client and reset state; updated get_weaviate_client() to use ensure_connected() and switched to parameterized logging; minor docstring edits. |
Application Lifecycle Integration backend/main.py |
Imported and invoked close_weaviate_client() in FastAPI lifespan shutdown to ensure graceful teardown of the Weaviate client. |
Sequence Diagram(s)
sequenceDiagram
participant App as Application
participant Lifespan as FastAPI lifespan
participant EC as ensure_connected()
participant WC as Weaviate Client
participant State as Global state (_client/_connected)
App->>Lifespan: Run application
App->>EC: First DB operation -> ensure_connected()
EC->>State: Check _connected (False)
EC->>WC: client.connect()
WC-->>EC: Connected
EC->>State: Set _client and _connected = True
EC-->>App: Return connected client
App->>EC: Subsequent DB operation -> ensure_connected()
EC->>State: Check _connected (True)
EC-->>App: Return existing client
Lifespan->>WC: Shutdown -> close_weaviate_client()
WC->>State: Check _client/_connected
alt Connected
WC->>WC: client.close()
WC->>State: Reset _client and _connected = False
end
WC-->>Lifespan: Closed
Estimated code review effort
π― 3 (Moderate) | β±οΈ ~20 minutes
Poem
π° I fixed the hops between connect and end,
Now one warm tunnel, no loop to mend.
Weaviate stays, steady and near,
I whisper "close" when shutdown's clear.
Hoorayβless latency, more cheer!
π₯ Pre-merge checks | β 5
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title 'feat(weaviate): implement persistent connection management' directly and clearly summarizes the main change: implementing persistent connection management for Weaviate. |
| Linked Issues check | β Passed | The PR successfully addresses all coding requirements from issue #227: eliminates per-operation connect/disconnect cycles, implements persistent connection reuse via ensure_connected(), ensures thread-safe connection establishment with locking, and provides graceful shutdown cleanup via close_weaviate_client(). |
| Out of Scope Changes check | β Passed | All changes are directly in scope: global state and connection management logic in weaviate/client.py, integration into FastAPI lifespan in main.py, and logging improvements align with issue #227 requirements. |
| Docstring Coverage | β Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
βοΈ Tip: You can configure your own custom pre-merge checks in the settings.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
Hey! @smokeyScraper @chandansgowda I have added the required enhancements now this should work properly and the connection should stay stable also I took care of the lock issues and race conditions which were constantly occurring have a look !