WP-02: Persistent Browser Instance #89
Labels
No Label
ai-changes-requested
ai-failed
ai-merged
ai-pr-opened
ai-reviewed
ai-reviewing
ai-reviewing
ai-working
bug
enhancement
evolution
performance
phase-0
phase-1a
phase-1b
phase-1c
phase-1d
security
tech-debt
todo
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/paper-dynasty-database#89
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
Replace per-request Chromium launch/teardown with a persistent browser that lives for the lifetime of the API process. Eliminates ~1.0-1.5s spawn overhead per render.
Repo:
databasePhase: 0 (Render Pipeline Optimization)
Dependencies: None (can run in parallel with WP-01)
Complexity: M
Current State
app/routers_v2/players.pylines 801-826:async with async_playwright() as p:block creates and destroys a browser per requestImplementation
_browserand_playwrightglobals toplayers.pyget_browser()— lazy-init withis_connected()auto-reconnectshutdown_browser()— clean teardown for API shutdownasync with async_playwright()block with page-per-request pattern:finallyblock to prevent memory leaksFiles
database/app/routers_v2/players.py— persistent browser, page-per-requestTests
get_browser()returns a connected browserget_browser()returns same instance on second callget_browser()relaunches if browser disconnectedshutdown_browser()cleanly closes browser and playwrightAcceptance Criteria
Plan reference:
docs/prd-evolution/PHASE0_PROJECT_PLAN.mdWP-02PR opened: #97
Approach: Added
get_browser()(lazy-init withis_connected()auto-reconnect) andshutdown_browser()toplayers.pyas module-level globals. Replaced theasync with async_playwright()per-request block with a page-per-request pattern — browser persists, each render gets a fresh page closed in afinallyblock.shutdown_browser()is wired into a FastAPIlifespancontext manager inmain.pyfor clean teardown on API shutdown.