Skip to content
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

fix: Use AsyncSession for user management #4491

Merged
merged 7 commits into from
Nov 16, 2024

Conversation

cbornet
Copy link
Collaborator

@cbornet cbornet commented Nov 11, 2024

Use AsyncSession for user management

@cbornet cbornet requested a review from ogabrielluiz November 11, 2024 11:57
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Nov 11, 2024
@github-actions github-actions bot added the bug Something isn't working label Nov 11, 2024
@cbornet cbornet force-pushed the async-user-2 branch 3 times, most recently from 45ad310 to 5e4b3a9 Compare November 11, 2024 12:57
Comment on lines +53 to +60
query: SelectOfScalar = select(ApiKey).options(selectinload(ApiKey.user)).where(ApiKey.api_key == api_key)
api_key_object: ApiKey | None = (await session.exec(query)).first()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why get the user here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because here:
https://github.com/langflow-ai/langflow/blob/main/src/backend/base/langflow/services/auth/utils.py#L71-L72
the ApiKey.user is referenced but lazy-loading doesn't work with AsyncSession.

Copy link
Collaborator Author

@cbornet cbornet Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified check_key to return the user instead of the ApiKey.
This way the result is easier to check.
PTAL.

Copy link
Contributor

@ogabrielluiz ogabrielluiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 11, 2024
@@ -431,7 +431,7 @@ def get_variable(name: str, field: str):

return get_variable

def list_key_names(self):
async def list_key_names(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this one async. Is it OK ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but for backwards compatibility we have to keep it a sync version of it with this name and create a new one. This one then can just call run_until_complete or something like that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Reverted to sync version and added list_variables_sync in VariableService

Comment on lines +53 to +60
query: SelectOfScalar = select(ApiKey).options(selectinload(ApiKey.user)).where(ApiKey.api_key == api_key)
api_key_object: ApiKey | None = (await session.exec(query)).first()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because here:
https://github.com/langflow-ai/langflow/blob/main/src/backend/base/langflow/services/auth/utils.py#L71-L72
the ApiKey.user is referenced but lazy-loading doesn't work with AsyncSession.

Copy link

codspeed-hq bot commented Nov 11, 2024

CodSpeed Performance Report

Merging #4491 will degrade performances by 78.78%

Comparing cbornet:async-user-2 (3b68be7) with main (2881346)

Summary

❌ 6 regressions
✅ 9 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main cbornet:async-user-2 Change
test_create_starter_projects 658.3 ms 966.6 ms -31.9%
test_initialize_services 140 ms 423.9 ms -66.97%
test_initialize_super_user 142 ms 669.4 ms -78.78%
test_invalid_run_with_input_type_chat 13.8 ms 26.6 ms -48.06%
test_successful_run_with_input_type_any 205.6 ms 297.6 ms -30.92%
test_successful_run_with_output_type_any 205.3 ms 245.6 ms -16.4%

@cbornet cbornet force-pushed the async-user-2 branch 2 times, most recently from bd750a5 to 0a3000d Compare November 12, 2024 10:37
@cbornet cbornet enabled auto-merge (squash) November 16, 2024 00:38
@cbornet cbornet merged commit 6573ca1 into langflow-ai:main Nov 16, 2024
30 checks passed
@cbornet cbornet deleted the async-user-2 branch November 16, 2024 01:19
diogocabral pushed a commit to headlinevc/langflow that referenced this pull request Nov 26, 2024
* Use AsyncSession for user management

* Simplify check_key

* Don't trigger blockbuster on settings service initialize

* Fix mypy

* Fix api key update_total_uses

* Fix auto-login

* Revert making CustomComponent.list_key_names async
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants