feat(sessions): add BufferedFirestoreSessionService#160
Open
enesdemirag wants to merge 4 commits into
Open
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
Signed the CLA. |
Add a Firestore-backed session service that buffers events in memory and flushes them in a single transaction per session, collapsing the repeated session-doc/state-doc updates and per-event transactions from N to 1. Mirrors the data model of the builtin google.adk.integrations.firestore.FirestoreSessionService (collection hierarchy, app/user/session state scoping, optimistic concurrency via a revision field, idempotent event docs keyed by event.id) and adds: - in-memory per-session buffering with count/interval/explicit/shutdown flush - durable_mode to persist every event immediately - exponential backoff with jitter on retryable errors - start()/stop()/flush() ADK lifecycle hooks Gated behind the new optional [firestore] extra. Unit tests use an in-memory fake AsyncClient (no external services).
- Remove unused _SessionLockKey type alias - Move import copy to module level (stdlib, no reason to lazy-import) - Store self._firestore in __init__ to avoid repeated guarded imports inside create_session / _persist_batch - Add sessions_collection, events_collection, app_state_collection, and user_state_collection constructor params (keyword-only, with defaults) so developers can customise the Firestore collection layout without subclassing
…erarchy
Add flat_layout=True constructor parameter so developers can store sessions
directly in root_collection/{session_id} instead of the default nested
root/{app}/users/{user}/sessions/{session_id} path.
Useful when the session id already encodes the user (e.g. {phone}-{date}),
matching an existing flat Firestore collection layout. list_sessions adds a
userId field filter automatically in flat mode.
…yink - Drop `from __future__ import annotations`; use X | None syntax directly (requires Python >=3.10, already in project metadata) - Remove Optional import; all annotations now use built-in union syntax - Remove vague section-header comments - Simplify is_retryable_error return (single return name in _RETRYABLE_ERROR_NAMES) - Update firestore extra version range to match ADK's own constraint (>=2.11,<3)
3f311ca to
c411073
Compare
Author
|
Okay I completed the CLA. Waiting for a review. |
Author
|
This buffered approach was my need and I think many more people may want to use it as well. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #159
Summary
Adds
BufferedFirestoreSessionServicetogoogle.adk_community.sessions.The builtin
FirestoreSessionServicewrites one Firestore transaction per event. This service buffers events in memory and flushes them all in a single transaction — collapsing N session-doc + state-doc updates into 1. The event-document count is unchanged; what improves is write amplification and optimistic-lock contention.Set
durable_mode=Truefor per-event writes (same as builtin) — making this a drop-in replacement with the same data layout.Changes
src/google/adk_community/sessions/firestore_session_service.pysrc/google/adk_community/sessions/__init__.py(export)pyproject.toml([firestore]extra,>=2.11,<3matching ADK's own constraint)tests/unittests/sessions/test_firestore_session_service.py(20 tests)Constructor options
clientfirestore.AsyncClientroot_collectionadk-sessionsessions_collectionsessionsevents_collectioneventsapp_state_collectionapp_statesuser_state_collectionuser_statesflat_layoutFalseTrue→ sessions atroot/{session_id}(no{app}/users/{user}nesting)durable_modeFalseTrue→ per-event writes, same as builtinbuffer_max_events10flush_interval_seconds120.0max_retry_attempts5retry_base_delay_seconds0.5Testing plan
20 passed (in-memory fake
AsyncClient, no external services):create/get/list/delete, 9 events → 1 transaction (batched flush), threshold + interval + explicit + stop flush, durable mode (per-event), retry then success, permanent error no retry, events during flush survive,
get_sessionmerges buffered events, app/user/session/temp state delta scoping.Formatted with
pyink --config pyproject.toml+isort.Checklist
pyink+isort)[firestore]optional extra inpyproject.tomlsessions/__init__.py@overrideon all overridden methods