Performance: Connection lifecycle management
HIGH IMPACT
This concept affects how efficiently the server handles client connections, impacting request latency and server resource usage.
from fastapi import FastAPI from fastapi import Depends app = FastAPI() async def get_db(): db = open_db_connection() try: yield db finally: db.close() @app.get("/") async def read_root(db=Depends(get_db)): data = db.query("SELECT * FROM table") return data
from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): # Opening a new database connection per request without closing db = open_db_connection() data = db.query("SELECT * FROM table") return data
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Opening new DB connection per request without closing | N/A | N/A | N/A | [X] Bad |
| Using dependency injection with connection close in finally block | N/A | N/A | N/A | [OK] Good |
| WebSocket without disconnect handling | N/A | N/A | N/A | [X] Bad |
| WebSocket with disconnect exception handling | N/A | N/A | N/A | [OK] Good |