From 6ec03c3d3494f91a59accfced180b03eb02478a1 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 30 Dec 2020 15:37:59 +0200 Subject: [PATCH] Revert "Add a django middleware to cleanup db connections." This ended up being useless because of the way startlette and fastapi do thread pools. The middleware is called in one thread, the path in another, and the dependency in yet another. This reverts commit 473448246f61a540110142f261e5aa2650b3cd12. --- etebase_fastapi/main.py | 2 -- etebase_fastapi/middleware.py | 15 --------------- 2 files changed, 17 deletions(-) delete mode 100644 etebase_fastapi/middleware.py diff --git a/etebase_fastapi/main.py b/etebase_fastapi/main.py index c07d975..8e8469c 100644 --- a/etebase_fastapi/main.py +++ b/etebase_fastapi/main.py @@ -5,7 +5,6 @@ from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.trustedhost import TrustedHostMiddleware -from .middleware import DjangoDbConnectionCleanupMiddleware from .exceptions import CustomHttpException from .msgpack import MsgpackResponse from .routers.authentication import authentication_router @@ -43,7 +42,6 @@ def create_application(prefix="", middlewares=[]): app.include_router(test_reset_view_router, prefix=f"{BASE_PATH}/test/authentication") - app.add_middleware(DjangoDbConnectionCleanupMiddleware) app.add_middleware( CORSMiddleware, allow_origin_regex="https?://.*", diff --git a/etebase_fastapi/middleware.py b/etebase_fastapi/middleware.py deleted file mode 100644 index 06e347b..0000000 --- a/etebase_fastapi/middleware.py +++ /dev/null @@ -1,15 +0,0 @@ -from starlette.types import ASGIApp, Receive, Scope, Send -from django.db import close_old_connections, reset_queries - - -class DjangoDbConnectionCleanupMiddleware: - def __init__(self, app: ASGIApp): - self.app = app - - async def __call__(self, scope: Scope, receive: Receive, send: Send): - reset_queries() - close_old_connections() - try: - await self.app(scope, receive, send) - finally: - close_old_connections()