fix: reuse persistent aiohttp.ClientSession in GiphyService (#26) #66
No reviewers
Labels
No Label
ai-changes-requested
ai-pr-opened
ai-reviewed
ai-reviewing
ai-working
in-next-release
status/in-progress
status/pr-open
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/major-domo-v2#66
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "ai/major-domo-v2-26"
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
Closes #26.
Both
get_disappointment_gifandget_gifpreviously created a newaiohttp.ClientSessionper invocation, incurring a TCP handshake and DNS resolution on every GIF fetch. This PR stores a singleClientSessionon theGiphyServiceinstance and reuses it across calls.Changes
services/giphy_service.pyself._session: Optional[aiohttp.ClientSession] = Nonein__init___get_session()lazy initialiser — creates the session on first use, recreates it if closedasync with aiohttp.ClientSession() as session:in bothget_disappointment_gifandget_gifwithsession = self._get_session()async with session.get(...) as resp:— only the outer session lifetime changesTest Results
924 tests passed, 3 skipped. The 6 failures in
test_commands_profile_images.pyare pre-existing and unrelated to this change (those files were already modified onmainbefore this branch was created).AI Code Review
Files Reviewed
services/giphy_service.py(modified)Findings
Correctness
_get_session()checks bothNoneand.closedbefore constructing a new session, which handles the case where the session was previously closed externally.async with session.get(...) as resp:) are preserved correctly — only the outer session lifetime changes, as described.Optionalis already imported fromtypingandaiohttpwas already imported — no new import issues.Security
quote(). No new attack surface introduced.Style & Conventions
_get_session()is a synchronous method but is always called from within async methods (get_disappointment_gif,get_gif), so the event loop is guaranteed to be running whenaiohttp.ClientSession()is constructed. This is safe.Suggestions
close()method: The sharedClientSessionis never explicitly closed. aiohttp will emitResourceWarning: Unclosed client sessionat process exit. For a long-running bot singleton this is an accepted trade-off, but an explicit cleanup method would be clean: The bot's shutdown hook (or cog teardown) could then call this. Not a blocker — the OS reclaims resources at container exit either way.Verdict: APPROVED
The optimization is correct and well-scoped. Reusing a persistent
ClientSessionis the right pattern for a long-running service and eliminates the per-call TCP handshake overhead. The only notable omission is explicit cleanup on shutdown, which is a minor concern for a Docker-based bot.Automated review by Claude PR Reviewer