mirror of
https://github.com/langgenius/dify.git
synced 2026-09-08 02:43:49 +08:00
Merge branch 'main' into jzh
This commit is contained in:
commit
c964708ebe
@ -5,6 +5,7 @@ This module provides integration with Weaviate vector database for storing and r
|
|||||||
document embeddings used in retrieval-augmented generation workflows.
|
document embeddings used in retrieval-augmented generation workflows.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import atexit
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@ -37,6 +38,32 @@ _weaviate_client: weaviate.WeaviateClient | None = None
|
|||||||
_weaviate_client_lock = threading.Lock()
|
_weaviate_client_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
def _shutdown_weaviate_client() -> None:
|
||||||
|
"""
|
||||||
|
Best-effort shutdown hook to close the module-level Weaviate client.
|
||||||
|
|
||||||
|
This is registered with atexit so that HTTP/gRPC resources are released
|
||||||
|
when the Python interpreter exits.
|
||||||
|
"""
|
||||||
|
global _weaviate_client
|
||||||
|
|
||||||
|
# Ensure thread-safety when accessing the shared client instance
|
||||||
|
with _weaviate_client_lock:
|
||||||
|
client = _weaviate_client
|
||||||
|
_weaviate_client = None
|
||||||
|
|
||||||
|
if client is not None:
|
||||||
|
try:
|
||||||
|
client.close()
|
||||||
|
except Exception:
|
||||||
|
# Best-effort cleanup; log at debug level and ignore errors.
|
||||||
|
logger.debug("Failed to close Weaviate client during shutdown", exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
|
# Register the shutdown hook once per process.
|
||||||
|
atexit.register(_shutdown_weaviate_client)
|
||||||
|
|
||||||
|
|
||||||
class WeaviateConfig(BaseModel):
|
class WeaviateConfig(BaseModel):
|
||||||
"""
|
"""
|
||||||
Configuration model for Weaviate connection settings.
|
Configuration model for Weaviate connection settings.
|
||||||
@ -85,18 +112,6 @@ class WeaviateVector(BaseVector):
|
|||||||
self._client = self._init_client(config)
|
self._client = self._init_client(config)
|
||||||
self._attributes = attributes
|
self._attributes = attributes
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
"""
|
|
||||||
Destructor to properly close the Weaviate client connection.
|
|
||||||
Prevents connection leaks and resource warnings.
|
|
||||||
"""
|
|
||||||
if hasattr(self, "_client") and self._client is not None:
|
|
||||||
try:
|
|
||||||
self._client.close()
|
|
||||||
except Exception as e:
|
|
||||||
# Ignore errors during cleanup as object is being destroyed
|
|
||||||
logger.warning("Error closing Weaviate client %s", e, exc_info=True)
|
|
||||||
|
|
||||||
def _init_client(self, config: WeaviateConfig) -> weaviate.WeaviateClient:
|
def _init_client(self, config: WeaviateConfig) -> weaviate.WeaviateClient:
|
||||||
"""
|
"""
|
||||||
Initializes and returns a connected Weaviate client.
|
Initializes and returns a connected Weaviate client.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user