-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Return the link to the connection pool after the node execution is completed #3957
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| from functools import reduce | ||
| from typing import List, Dict | ||
|
|
||
| from django.db import close_old_connections | ||
| from django.db import close_old_connections, connection | ||
| from django.utils import translation | ||
| from django.utils.translation import get_language | ||
| from langchain_core.prompts import PromptTemplate | ||
|
|
@@ -433,6 +433,8 @@ def hand_event_node_result(self, current_node, node_result_future): | |
| return None | ||
| finally: | ||
| current_node.node_chunk.end() | ||
| # 归还链接到连接池 | ||
| connection.close() | ||
|
|
||
| def run_node_async(self, node): | ||
| future = executor.submit(self.run_node, node) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No issues found and no optimizations required for this code. It's structured well, clear, and follows best practices. The |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,8 @@ def is_valid_credential(provider, model_type, model_name, model_credential: Dict | |
|
|
||
| def get_model_by_id(_id, workspace_id): | ||
| model = QuerySet(Model).filter(id=_id).first() | ||
| # 归还链接到连接池 | ||
| connection.close() | ||
| get_authorized_model = DatabaseModelManage.get_model("get_authorized_model") | ||
| if model and model.workspace_id != workspace_id and get_authorized_model is not None: | ||
| model = get_authorized_model(QuerySet(Model).filter(id=_id), workspace_id).first() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an issue with this code that needs to be addressed: The function To fix this, you should ensure that the same database connection is used throughout the entire process of querying and filtering. Here's how you can modify the function: from django.db import connection
def get_model_by_id(_id, workspace_id):
try:
with connection.cursor() as cursor:
# Assuming Model and DatabaseModelManage are models from Django
query = f"SELECT * FROM {Model._meta.db_table} WHERE id=%s AND workspace_id=%s"
cursor.execute(query, (_id, workspace_id))
model_data = cursor.fetchone()
if model_data:
model = Model.objects.create(**model_data)
if model and model.workspace_id != workspace_id:
authorized_model_query = "SELECT * FROM auth_authorized_models WHERE model_id = %s AND workspace_id = %s;"
authorized_cursor = connection.cursor()
authorized_cursor.execute(authorized_model_query, (model.id, workspace_id,))
authorized_model_data = authorized_cursor.fetchone()
if authorized_model_data:
authorized_model = DatabaseModelManage.queryset_class().create(**authorized_model_data)
return authorized_model
return model
except Exception as e:
print(f"An error occurred: {e}")
return None
finally:
# Always use commit=False when using .fetchone() without creating a new object
connection.commit()Key Changes:
By making these adjustments, the code will handle database connections properly and mitigate potential data consistency issues related to different connection contexts. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code contains a few recommendations:
Connection Management: It's important to manage database connections properly, especially when dealing with Django applications. Closing the connection at all times can lead to performance issues and resource leaks. Instead of closing it here, you might want to ensure that any other operations involving databases do not leave open unnecessary connections.
Validation Logic: There's redundancy in handling exceptions for the missing tool library. You should only catch this exception once and raise the appropriate error message without duplicating logic within
is_valid.Here's an improved version of the code:
Key Improvements:
f_libis made using exception handling.These changes help optimize the serializer's logic while ensuring proper database management, which is crucial for maintaining application performance.