Skip to content

Harden Firestore cleanup: configurable concurrency and explicit RPC timeouts #1180

Description

@groupthinking

Problem

CodeRabbit's review of #1170 raised two hardening suggestions for
FirestoreStateService.cleanup_old_states() that are correct in principle but
sit outside the scope of that PR, which was a pure latency change. Filing them
here so they are not lost.

1. Make CLEANUP_DELETE_CONCURRENCY environment-configurable

src/youtube_extension/services/cloud/firestore_state.py currently hardcodes:

CLEANUP_DELETE_CONCURRENCY = 16

Suggestion was int(os.getenv("CLEANUP_DELETE_CONCURRENCY", "16")).

Deferred because:

  • The equivalent constant merged in ad7e2c1 (TAG_WRITE_CONCURRENCY = 8 in
    intelligent_cache.py) is also a plain module constant. Making only this one
    configurable is inconsistent.
  • A bare int(os.getenv(...)) raises ValueError at import time on a
    malformed value, turning a typo into an unimportable module. It needs a
    validating parse with a fallback, which is its own change with its own tests.

If this is done, do both constants together behind one shared helper.

2. Pass an explicit timeout to doc.reference.delete()

Suggestion was to bound each delete with a deadline so a hung RPC cannot occupy
a pool worker indefinitely.

Deferred because:

  • There is no existing timeout constant in the module (grep -n 'timeout\|TIMEOUT'
    returns nothing), so this means inventing a deadline policy rather than reusing one.
  • Any value chosen would sit on top of the Firestore async client's own retry and
    deadline configuration; picking one without knowing that baseline risks
    converting slow-but-successful deletes into tallied failures.
  • The pre-existing sequential loop had no timeout either, so this is not a
    regression introduced by perf: delete expired Firestore states concurrently under a bound #1170.

The right fix is probably a module-level deadline derived from the client config,
applied consistently to all Firestore calls in the service, not just cleanup.

Acceptance criteria

  • Concurrency constants parsed from env with validation and a safe fallback, applied to both firestore_state.py and intelligent_cache.py
  • A documented timeout policy for Firestore RPCs in firestore_state.py, with tests covering the timeout-as-failure path
  • No behaviour change when the env vars are unset

Scope

src/youtube_extension/services/cloud/firestore_state.py,
src/youtube_extension/backend/services/intelligent_cache.py, and their unit tests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions