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

feat: Add AsyncSession support for non-blocking db operations #4408

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

cbornet
Copy link
Collaborator

@cbornet cbornet commented Nov 5, 2024

  • Creates an async engine in db service
  • Provides functions to get an AsyncSession
  • Refactors api_key endpoints to use the AsyncSession.

It uses:

  • aiosqlite for sqlite
  • psycopg for postgresql

I tested manually that it works on PostgreSQL

@cbornet cbornet requested a review from ogabrielluiz November 5, 2024 14:33
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 5, 2024
@cbornet cbornet marked this pull request as draft November 5, 2024 14:33
@dosubot dosubot bot added the enhancement New feature or request label Nov 5, 2024
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Nov 5, 2024
await session.commit()
return unmasked_api_key

unmasked_api_key = asyncio.run(aapi_key())
Copy link
Collaborator Author

@cbornet cbornet Nov 5, 2024

Choose a reason for hiding this comment

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

Since create_api_key and delete_api_key are now coroutines, we run the command in an event loop.
I think it's OK as it's a CLI and so a kind of entry point.
But please comment if you think there could be issues like conflicts with other event loops.
The other solution would be to duplicate create_api_key and delete_api_key to have both a sync and an async version.

@@ -3,7 +3,7 @@

from fastapi import APIRouter, Depends, HTTPException, Response

from langflow.api.utils import CurrentActiveUser, DbSession
from langflow.api.utils import AsyncDbSession, CurrentActiveUser, DbSession
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note: we can't replace atm the DbSession by AsyncDbSession in save_store_api_key as it gets the current user with the sync session and so we can't modify it with the async session.

@cbornet cbornet marked this pull request as ready for review November 5, 2024 14:55
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Nov 5, 2024
url_components = self.database_url.split("://", maxsplit=1)
if url_components[0].startswith("sqlite"):
database_url = "sqlite+aiosqlite://"
kwargs = {}
Copy link
Collaborator Author

@cbornet cbornet Nov 5, 2024

Choose a reason for hiding this comment

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

aiosqlite doesn't have a connection pool.
I don't think that's a big issue but please comment if you think it is.

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Nov 5, 2024
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

Copy link
Contributor

Choose a reason for hiding this comment

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

these changes to the starter projects are wrong because of a bug we had. You should merge with main and run the backend again to remove them

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 5, 2024
@italojohnny
Copy link
Member

Is the greenlet dependency missing?

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Nov 5, 2024
@cbornet cbornet force-pushed the async-db branch 2 times, most recently from c046fea to 28a8c0e Compare November 5, 2024 22:51
@cbornet
Copy link
Collaborator Author

cbornet commented Nov 6, 2024

Is the greenlet dependency missing?

Oh right, even though there are now built wheels of greenlet for M1, it seems sqlalchemy doesn't pull them by default. Will fix.

@ogabrielluiz ogabrielluiz merged commit e853a13 into langflow-ai:main Nov 6, 2024
27 of 28 checks passed
@cbornet cbornet deleted the async-db branch November 7, 2024 08:42
joaoguilhermeS pushed a commit that referenced this pull request Nov 7, 2024
* Add AsyncSession support for non-blocking db operations

* Use sqlalchemy extras
diogocabral pushed a commit to headlinevc/langflow that referenced this pull request Nov 26, 2024
…ow-ai#4408)

* Add AsyncSession support for non-blocking db operations

* Use sqlalchemy extras
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants