-
Notifications
You must be signed in to change notification settings - Fork 1
Fiddle: update database connection middleware to support streaming apis #133
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 |
|---|---|---|
|
|
@@ -10,4 +10,3 @@ COPY uv.lock . | |
| RUN make install | ||
|
|
||
| COPY . . | ||
| RUN make type-check | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,21 @@ | ||
| from starlette.middleware.base import BaseHTTPMiddleware | ||
| from starlette.middleware.base import RequestResponseEndpoint | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
| from starlette.types import ASGIApp | ||
| from starlette.types import Receive | ||
| from starlette.types import Scope | ||
| from starlette.types import Send | ||
|
|
||
| from core.store.database import Database | ||
|
|
||
|
|
||
| class DatabaseConnectionMiddleware(BaseHTTPMiddleware): | ||
| class DatabaseConnectionMiddleware: | ||
| def __init__(self, app: ASGIApp, database: Database) -> None: | ||
| super().__init__(app=app) | ||
| self.app = app | ||
| self.database = database | ||
|
|
||
| # NOTE(krishan711): see note in database.py about why this can cause problems with concurrent operations | ||
| async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response: | ||
| # NOTE(krishan711): hack to prevent running this for streaming endpoints because streaming | ||
| # endpoints return a response with a generator inside it so this middleware wouldn't work | ||
| if request.scope['path'].endswith('-streamed'): | ||
| return await call_next(request) | ||
| # isReadonly = request.method in {'GET', 'OPTIONS', 'HEAD'} | ||
| # NOTE(krishan711): raw ASGI (not BaseHTTPMiddleware) so the DB connection stays open across streaming body | ||
| async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
| if scope['type'] != 'http': | ||
| await self.app(scope, receive, send) | ||
| return | ||
| async with self.database.create_context_connection(): | ||
| response = await call_next(request) | ||
| return response | ||
| await self.app(scope, receive, send) | ||
|
Comment on lines
+19
to
+21
|
||
Uh oh!
There was an error while loading. Please reload this page.