refactor: reuse single session when getting variables from db#8814
Conversation
- Updated `get_variables` method in `CustomComponent` to accept an optional session parameter, allowing for session reuse and reducing connection pool exhaustion. - Modified `update_params_with_load_from_db_fields` to pass the session when calling `get_variables`. - Adjusted `get_instance_results` to support session management for database operations. - Increased connection pool size and max overflow in settings for improved performance under load.
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe changes introduce optional session management in several asynchronous functions related to custom component variable loading, allowing shared database sessions to be passed through the call stack. Additionally, default database connection pool sizes are increased in the settings. No changes to the public API signatures except for added optional parameters. Changes
Sequence Diagram(s)sequenceDiagram
participant API as API Endpoint
participant Session as session_scope()
participant Loader as update_params_with_load_from_db_fields
participant Component as CustomComponent.get_variables
participant DB as Database
API->>Session: async with session_scope()
Session->>Loader: update_params_with_load_from_db_fields(..., session)
Loader->>Component: get_variables(..., session)
Component->>DB: Fetch variable using session
DB-->>Component: Return variable
Component-->>Loader: Return variable
Loader-->>Session: Return updated params
Session-->>API: Return response
Suggested labels
Suggested reviewers
✨ Finishing Touches🧪 Generate Unit Tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Pull Request Overview
This PR enhances database session management by enabling session reuse during load operations and updates the connection pool settings to prevent exhaustion and improve performance.
- Introduces an optional
sessionparameter inget_variables,get_instance_results, andupdate_params_with_load_from_db_fieldsto allow reuse of a single database session. - Wraps all
load_from_dbcalls incustom_component_updatewith a singlesession_scope. - Increases
pool_sizefrom 20 to 30 andmax_overflowfrom 30 to 50 in the database settings.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/backend/base/langflow/services/settings/base.py | Updated pool_size and max_overflow values in both class attributes and settings |
| src/backend/base/langflow/interface/initialize/loading.py | Added session parameter to get_instance_results and update_params_with_load_from_db_fields |
| src/backend/base/langflow/custom/custom_component/custom_component.py | Extended get_variables to accept an optional session and added fallback logic |
| src/backend/base/langflow/api/v1/endpoints.py | Wrapped load_from_db operations in a single session_scope context |
Comments suppressed due to low confidence (4)
src/backend/base/langflow/services/settings/base.py:90
- The docstring suggests max_overflow should be 2x pool_size (i.e., 60), but it's set to 50. Consider updating the docstring to reflect the intended ratio or adjusting the value to match the recommendation.
Should be 2x the pool_size for optimal performance under load."""
src/backend/base/langflow/custom/custom_component/custom_component.py:407
- [nitpick] Update the function docstring to include a description for the new 'session' parameter, specifying its type and usage.
async def get_variables(self, name: str, field: str, session=None):
src/backend/base/langflow/interface/initialize/loading.py:60
- [nitpick] Add a brief docstring or inline comment to describe the purpose and expected type of the optional 'session' parameter.
session=None,
src/backend/base/langflow/interface/initialize/loading.py:114
- Consider adding unit tests to cover the new 'session' parameter path in update_params_with_load_from_db_fields to ensure session reuse behaves as expected.
session=None,
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/backend/base/langflow/api/v1/endpoints.py (1)
720-726: Excellent session management implementation with minor formatting issue.The use of
session_scope()context manager ensures all database operations for loading parameters share a single session, which aligns perfectly with the PR's goal of reducing connection pool exhaustion.Fix the whitespace on the blank line:
- +src/backend/base/langflow/interface/initialize/loading.py (1)
60-64: Session parameter propagation implemented correctly with minor formatting issue.The session parameter is properly added to the function signature and forwarded to
update_params_with_load_from_db_fields, enabling consistent session management across the call stack.Fix the line length issue by breaking the long line:
- custom_params = await update_params_with_load_from_db_fields( - custom_component, custom_params, vertex.load_from_db_fields, fallback_to_env_vars=fallback_to_env_vars, session=session - ) + custom_params = await update_params_with_load_from_db_fields( + custom_component, + custom_params, + vertex.load_from_db_fields, + fallback_to_env_vars=fallback_to_env_vars, + session=session + )
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/backend/base/langflow/api/v1/endpoints.py(2 hunks)src/backend/base/langflow/custom/custom_component/custom_component.py(2 hunks)src/backend/base/langflow/interface/initialize/loading.py(2 hunks)src/backend/base/langflow/services/settings/base.py(2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`src/backend/**/*.py`: Run make format_backend to format Python code early and often Run make lint to check for linting issues in backend Python code
src/backend/**/*.py: Run make format_backend to format Python code early and often
Run make lint to check for linting issues in backend Python code
📄 Source: CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
List of files the instruction was applied to:
src/backend/base/langflow/api/v1/endpoints.pysrc/backend/base/langflow/custom/custom_component/custom_component.pysrc/backend/base/langflow/services/settings/base.pysrc/backend/base/langflow/interface/initialize/loading.py
`src/backend/**/*component*.py`: In your Python component class, set the `icon` attribute to a string matching the frontend icon mapping exactly (case-sensitive).
src/backend/**/*component*.py: In your Python component class, set theiconattribute to a string matching the frontend icon mapping exactly (case-sensitive).
📄 Source: CodeRabbit Inference Engine (.cursor/rules/icons.mdc)
List of files the instruction was applied to:
src/backend/base/langflow/custom/custom_component/custom_component.py
🧠 Learnings (1)
src/backend/base/langflow/api/v1/endpoints.py (9)
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/__init__.py : Update __init__.py with alphabetical imports when adding new components
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Implement async component methods using async def and await for asynchronous operations
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Add new backend components to the appropriate subdirectory under src/backend/base/langflow/components/
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Use asyncio.Queue for non-blocking queue operations in async components and handle timeouts appropriately
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Use asyncio.create_task for background work in async components and ensure proper cleanup on cancellation
Learnt from: ogabrielluiz
PR: langflow-ai/langflow#0
File: :0-0
Timestamp: 2025-06-26T19:43:18.260Z
Learning: In langflow custom components, the `module_name` parameter is now propagated through template building functions to add module metadata and code hashes to frontend nodes for better component tracking and debugging.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-06-30T14:41:58.837Z
Learning: Applies to {src/backend/tests/**/*.py,tests/**/*.py} : Use predefined JSON flows and utility functions for flow testing (e.g., 'create_flow', 'build_flow', 'get_build_events', 'consume_and_assert_stream').
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/tests/unit/**/*.py : Test component integration within flows using create_flow, build_flow, and get_build_events utilities
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-06-30T14:41:58.837Z
Learning: Applies to {src/backend/tests/**/*.py,tests/**/*.py} : Test component configuration updates by asserting changes in build config dictionaries.
🧬 Code Graph Analysis (1)
src/backend/base/langflow/custom/custom_component/custom_component.py (4)
src/backend/tests/unit/services/variable/test_service.py (1)
session(23-28)src/backend/base/langflow/services/variable/service.py (1)
get_variable(56-80)src/backend/base/langflow/services/variable/base.py (1)
get_variable(25-36)src/backend/base/langflow/services/deps.py (1)
session_scope(157-179)
🪛 Ruff (0.11.9)
src/backend/base/langflow/api/v1/endpoints.py
720-720: Blank line contains whitespace
Remove whitespace from blank line
(W293)
src/backend/base/langflow/custom/custom_component/custom_component.py
428-428: Blank line contains whitespace
Remove whitespace from blank line
(W293)
432-432: Unnecessary else after return statement
Remove unnecessary else
(RET505)
434-434: Redefining argument with the local name session
(PLR1704)
src/backend/base/langflow/interface/initialize/loading.py
63-63: Line too long (127 > 120)
(E501)
🪛 Pylint (3.3.7)
src/backend/base/langflow/custom/custom_component/custom_component.py
[refactor] 429-435: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it
(R1705)
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: Validate PR
- GitHub Check: Update Starter Projects
- GitHub Check: Optimize new Python code in this PR
- GitHub Check: Run Ruff Check and Format
- GitHub Check: Ruff Style Check (3.13)
🔇 Additional comments (5)
src/backend/base/langflow/services/settings/base.py (2)
85-88: LGTM! Pool size increases support enhanced session management.The increased
pool_size(20→30) andmax_overflow(30→50) values align well with the PR's goal of improving database session management under load and reducing connection pool exhaustion.
107-108: Configuration consistency maintained.The
db_connection_settingsdictionary correctly reflects the updated pool values, ensuring consistency between individual attributes and the centralized configuration.src/backend/base/langflow/api/v1/endpoints.py (1)
46-46: Session management import added correctly.The addition of
session_scopeimport supports the enhanced database session management approach implemented in this PR.src/backend/base/langflow/custom/custom_component/custom_component.py (1)
407-407: Session parameter addition enhances database session management.The optional
sessionparameter enables external session management, supporting the PR's goal of reducing connection pool exhaustion through session reuse.src/backend/base/langflow/interface/initialize/loading.py (1)
114-121: Session forwarding to custom component implemented correctly.The session parameter is properly propagated to
custom_component.get_variables, completing the session management chain from the API endpoint to the database operations.
|
|
||
| if session is not None: | ||
| # Use the provided session instead of creating a new one | ||
| return await variable_service.get_variable(user_id=user_id, name=name, field=field, session=session) |
There was a problem hiding this comment.
I would probably lean towards just building the session within the method by default. Reasoning that any future users of this method also should err on the side of retrieving all variables at once rather than building a session per variable.
| database_connection_retry: bool = False | ||
| """If True, Langflow will retry to connect to the database if it fails.""" | ||
| pool_size: int = 20 | ||
| pool_size: int = 30 |
There was a problem hiding this comment.
Would revert these changes
- Added a new async `get_variables` method in `CustomComponent` to maintain backward compatibility with the deprecated method, ensuring it calls the existing `get_variable` method with session management. - This change enhances the robustness of the component while preserving existing functionality.
- Eliminated the unused `session_scope` import from the `endpoints.py` file to streamline the code and improve clarity. This change contributes to maintaining a clean and efficient codebase.
- Modified the `variables` method to call the new `get_variables` method for improved clarity and consistency. This change maintains backward compatibility while encouraging the use of the updated async method.
…ave session in update_build_config - Replaced instances of the deprecated `get_variable` method with the new `get_variables` method in `LMStudioEmbeddingsComponent`, `LMStudioModelComponent`, and `ChatOllamaComponent`. This change enhances code clarity and maintains consistency across components while ensuring backward compatibility.
Issue
POST http://localhost:3000/api/v1/custom_component/update
Error: "QueuePool limit of size 20 overflow 30 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/20/3o7r)"
Repo Steps
Description
This pull request introduces improvements to database session management and updates to connection pool settings to enhance performance and prevent connection pool exhaustion. The most important changes include the introduction of session reuse in database operations, updates to method signatures to support session passing, and adjustments to database connection pool configurations.
Database Session Management Enhancements:
src/backend/base/langflow/api/v1/endpoints.py: Introduced the use of a single database session withincustom_component_updateto handle allload_from_dboperations, preventing connection pool exhaustion. ([src/backend/base/langflow/api/v1/endpoints.pyL720-R725](https://github.com/langflow-ai/langflow/pull/8814/files#diff-2cc70b3b7a7be90cba6b85c839b082b0cc844c917644fdc2c9fafba56ba8b607L720-R725))src/backend/base/langflow/custom/custom_component/custom_component.py: Updated theget_variablesmethod to optionally accept a session parameter, allowing for session reuse. A fallback mechanism was added to create a new session for backward compatibility. ([[1]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-010449088e429212d46c7455250d37f399c727ed8e5ab77b99cb155242e879f3L407-R407),[[2]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-010449088e429212d46c7455250d37f399c727ed8e5ab77b99cb155242e879f3R428-R433))src/backend/base/langflow/interface/initialize/loading.py: Modifiedget_instance_resultsandupdate_params_with_load_from_db_fieldsto accept an optional session parameter, enabling session reuse during database operations. ([[1]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-0a870bc1d9300592df1438dde8700c07b2781027dda82cb0dc85372e7365fe09R60-R63),[[2]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-0a870bc1d9300592df1438dde8700c07b2781027dda82cb0dc85372e7365fe09R114-R121))Database Connection Pool Configuration:
src/backend/base/langflow/services/settings/base.py: Increasedpool_sizefrom 20 to 30 andmax_overflowfrom 30 to 50 to handle higher concurrent database connections and improve performance under high load scenarios. ([[1]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-c1d45d91203864dca90286f0f322de7fd8f9dd9acadb9c6e0fbb21cc7bf75fc3L85-R88),[[2]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-c1d45d91203864dca90286f0f322de7fd8f9dd9acadb9c6e0fbb21cc7bf75fc3L107-R108))Summary by CodeRabbit