perf(backend): Redis pooling, SSE session release, N+1 fixes, composite index (#96) #173
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/96-hotpath-perf"
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?
Summary
Four verified, independent hot-path inefficiencies, batched.
(a) Redis connection pooling
Per-call
aioredis.from_url(...)meant a connect+teardown on every authenticated request (auth/session.py) and per-publish (bot_pubsub.py). Now module-levelConnectionPool.from_url(...)built once (DB 0); helpers get clients off the pool.from_urlnow appears only in the one-time pool constructions (1 async pool insession.py; sync + async pools inbot_pubsub.py). Pool-backed clients don't tear down the shared pool onaclose().(b) SSE no longer holds a pooled DB session
session_record_streamreadguild_idup front andawait db.close()before returning theStreamingResponse— the generator closes over the plain string, so long-lived streams no longer pin a pooled connection.create_async_enginegiven explicitpool_size=10, max_overflow=20(commented) instead of the exhaustion-prone 5+10 default.(c) N+1 → grouped aggregates (all O(1))
bot_session_timeslotsand_auto_close_voting_async: oneselect(Vote.time_slot_id, Vote.availability, func.count()).group_by(...)instead of N per-slot queries; scores/tie-breaks unchanged.list_milestones: oneNPCAppearance … IN (npc_ids)fetch bucketed in Python (the reappearance synthesis needs the individual rows, so IN-fetch not count) — 1+N → 2 queries; "NPC reappeared" output identical.(d) Composite index
New migration
e5f6g7h8i9j0(down_revisiond4e5f6g7h8i9, the current head) addsix_sessions_status_confirmed_timeonsessions (status, confirmed_time)— thepoll_session_remindersminute-loop and the bot upcoming-session endpoint filter on these. MatchingIndex(...)added to theSessionmodel. Upgrade + downgrade both implemented.Verification
Backend full suite 395 passed; migration applies up and down cleanly (verified
d4e5f6g7h8i9 → e5f6g7h8i9j0 → downgrade → re-upgrade); confirmed the new revision chains from main's single head (no split heads). ruff 0.4.4 clean.Note: the dev test host has no live Redis (auth tests inject the user directly), so the pooled client's end-to-end round-trip isn't exercised there; the pool construction is import-safe (lazy) and the suite is green.
Closes #96
🤖 Generated with Claude Code