Add a django middleware to cleanup db connections.
This severely impacts performance, though without it we are getting django.db.utils.InterfaceError once connections in the pool go stale.master
parent
a7fdb4a108
commit
473448246f
@ -0,0 +1,15 @@
|
|||||||
|
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()
|
Loading…
Reference in New Issue