-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace BaseHTTPMiddleware with much faster ASGI equalivalent
- Loading branch information
1 parent
068e108
commit 0c34551
Showing
9 changed files
with
75 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint | ||
from starlette.requests import Request | ||
from fastapi import FastAPI | ||
|
||
from .request_context_plugin import get_request | ||
|
||
class CloseWebSessionsMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint): | ||
response = await call_next(request) | ||
|
||
try: | ||
for db_session in request.state.db_sessions: | ||
try: | ||
db_session.close() | ||
except Exception: | ||
pass | ||
except (KeyError, AttributeError): | ||
pass | ||
# Use basic ASGI middleware instead of BaseHTTPMiddleware as it is significantly faster: https://github.com/tiangolo/fastapi/issues/2696#issuecomment-768224643 | ||
class CloseWebSessionsMiddleware: | ||
def __init__(self, app: FastAPI): | ||
self.app = app | ||
|
||
return response | ||
async def __call__(self, scope, receive, send): | ||
await self.app(scope, receive, send) | ||
|
||
r = get_request() | ||
if r: | ||
try: | ||
for db_session in r.state.db_sessions: | ||
try: | ||
db_session.close() | ||
except Exception: | ||
pass | ||
except (KeyError, AttributeError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import logging | ||
|
||
from fastapi import HTTPException | ||
from fastapi import FastAPI, HTTPException | ||
from fastapi.exception_handlers import http_exception_handler | ||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint | ||
from starlette.requests import Request | ||
|
||
from ..server.utils import detail_from_exception | ||
from .request_context_plugin import get_request | ||
|
||
|
||
class ExceptionHandlerMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint): | ||
# Use basic ASGI middleware instead of BaseHTTPMiddleware as it is significantly faster: https://github.com/tiangolo/fastapi/issues/2696#issuecomment-768224643 | ||
class ExceptionHandlerMiddleware: | ||
def __init__(self, app: FastAPI): | ||
self.app = app | ||
|
||
async def __call__(self, scope, receive, send): | ||
try: | ||
return await call_next(request) | ||
await self.app(scope, receive, send) | ||
except Exception as e: | ||
logging.exception("An error occurred in FastAPI") | ||
return await http_exception_handler( | ||
request, | ||
get_request(), # type: ignore | ||
e if isinstance(e, HTTPException) else HTTPException(status_code=500, detail=detail_from_exception(e)), | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Optional | ||
|
||
from starlette.requests import HTTPConnection, Request | ||
from starlette_context import context | ||
from starlette_context.plugins.base import Plugin | ||
|
||
|
||
def get_request() -> Request | None: | ||
return context.get("request") | ||
|
||
|
||
class RequestContextPlugin(Plugin): | ||
# The returned value will be inserted in the context with this key | ||
key = "request" | ||
|
||
async def process_request(self, request: Request | HTTPConnection) -> Optional[Request | HTTPConnection]: | ||
return request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters