Defer Typesense client setup for conversation search#7662
Defer Typesense client setup for conversation search#7662tianmind-studio wants to merge 5 commits into
Conversation
Greptile SummaryThis PR defers Typesense client construction from module-import time to first use, so importing
Confidence Score: 4/5Safe to merge — the lazy-init change is self-contained, the tests cover the main scenarios, and the identified concerns are non-blocking quality improvements. The core change is straightforward and achieves its goal. The singleton lacks a threading lock (benign in practice but not future-proof), and the broad exception handler silently changes the type of config errors, making them harder to distinguish from runtime search failures. Neither issue causes incorrect behavior today. backend/utils/conversations/search.py — the singleton initialization pattern and exception-wrapping logic are the two spots worth a second look before this pattern is replicated elsewhere. Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant search_conversations
participant _get_typesense_client
participant typesense.Client
Caller->>search_conversations: search_conversations(uid, query)
search_conversations->>_get_typesense_client: _get_typesense_client()
alt _typesense_client is None
_get_typesense_client->>_get_typesense_client: _get_required_env_var(TYPESENSE_HOST)
_get_typesense_client->>_get_typesense_client: _get_required_env_var(TYPESENSE_HOST_PORT)
_get_typesense_client->>_get_typesense_client: _get_required_env_var(TYPESENSE_API_KEY)
note over _get_typesense_client: raises RuntimeError if any var missing
_get_typesense_client->>typesense.Client: Client(config)
typesense.Client-->>_get_typesense_client: client instance
_get_typesense_client->>_get_typesense_client: "_typesense_client = client (cached)"
else _typesense_client already initialized
note over _get_typesense_client: return cached singleton
end
_get_typesense_client-->>search_conversations: client
search_conversations->>typesense.Client: collections['conversations'].documents.search(params)
typesense.Client-->>search_conversations: results
search_conversations-->>Caller: "{items, total_pages, current_page, per_page}"
|
kodjima33
left a comment
There was a problem hiding this comment.
Lazy Typesense client init — backend refactor with tests, approve only per backend policy.
b3a0c51 to
a02fc14
Compare
|
I rebased and force-pushed this branch onto the current |
kodjima33
left a comment
There was a problem hiding this comment.
re-approved on new sha: backend bug fix (lazy Typesense client init + concurrency lock) — approve only
a02fc14 to
3bd28ca
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Re-approved on new sha: lazy Typesense init — has merge conflicts, please rebase
3bd28ca to
361a79c
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Lazy Typesense init w/ tests — re-approved on new sha
7242fc7 to
703c6b6
Compare
703c6b6 to
d266b5b
Compare
Summary
TYPESENSE_*env varsRuntimeErrorVerification
python -m pytest backend\tests\unit\test_conversation_search_typesense_config.py -q --tb=short->4 passedpython -m py_compile backend\utils\conversations\search.py backend\tests\unit\test_conversation_search_typesense_config.pypython -m black --line-length 120 --skip-string-normalization --check backend\utils\conversations\search.py backend\tests\unit\test_conversation_search_typesense_config.pyPYTHONUTF8=1 python backend\scripts\scan_async_blockers.py-> no new blockers from this PR; existing backlog onlygit diff --check origin/main...HEADscripts/pre-commitvia Git for Windowssh.exewith the backend venv onPATHNotes
main(9ed12aea07).RuntimeError, and concurrent initialization coverage is included.