mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-06-27 23:25:22 -04:00
perf(email): run blocking IMAP routes in threadpool
Fixes #4232 Convert email search and archive handlers from async def to sync def so FastAPI runs their blocking IMAP I/O in the threadpool instead of the event loop.
This commit is contained in:
@@ -1087,7 +1087,10 @@ def setup_email_routes():
|
|||||||
return {"contacts": [], "error": "Mail operation failed"}
|
return {"contacts": [], "error": "Mail operation failed"}
|
||||||
|
|
||||||
@router.get("/search")
|
@router.get("/search")
|
||||||
async def search_emails(
|
# Sync def: the body is blocking IMAP I/O with no awaits. As `async def` it ran
|
||||||
|
# directly on the event loop and stalled the whole app during a search; as a sync
|
||||||
|
# def FastAPI runs it in a threadpool, keeping the loop responsive.
|
||||||
|
def search_emails(
|
||||||
q: str = Query(""),
|
q: str = Query(""),
|
||||||
folder: str = Query("INBOX"),
|
folder: str = Query("INBOX"),
|
||||||
limit: int = Query(50),
|
limit: int = Query(50),
|
||||||
@@ -1736,7 +1739,9 @@ def setup_email_routes():
|
|||||||
return {"success": False, "error": "Mail operation failed"}
|
return {"success": False, "error": "Mail operation failed"}
|
||||||
|
|
||||||
@router.post("/archive/{uid}")
|
@router.post("/archive/{uid}")
|
||||||
async def archive_email(uid: str, folder: str = Query("INBOX"), account_id: str | None = Query(None), owner: str = Depends(require_owner)):
|
# Sync def: blocking IMAP I/O with no awaits — see search_emails above. Runs in a
|
||||||
|
# threadpool instead of blocking the event loop.
|
||||||
|
def archive_email(uid: str, folder: str = Query("INBOX"), account_id: str | None = Query(None), owner: str = Depends(require_owner)):
|
||||||
"""Move email to Archive folder."""
|
"""Move email to Archive folder."""
|
||||||
try:
|
try:
|
||||||
with _imap(account_id, owner=owner) as conn:
|
with _imap(account_id, owner=owner) as conn:
|
||||||
|
|||||||
Reference in New Issue
Block a user