feat: track per-user tool access times with in-memory cache and periodic DB flush#94
Merged
feat: track per-user tool access times with in-memory cache and periodic DB flush#94
Conversation
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.
What type of PR is this?
Which issue(s) this PR fixes
N/A — new feature
What this PR does / why we need it
Adds per-user tool usage tracking: an in-memory map records the last access time for each
(user_id, tool)pair, flushed to DB every 10 minutes. A query API returns results directly from memory with zero DB overhead.Changes
memoria-api/src/auth.rsToolUsageBatcherstruct: in-memoryHashMap<(user_id, tool), (DateTime, dirty)>mark_used()— records access with current timestampget_user_tool_usage(user_id)— returns all tool timestamps from memoryrebuild_from_db()— restores cache frommem_tool_usagetable on startupflush()— batch upserts dirty entries to DB; only clears dirty flags after all writes succeed (retry-safe)spawn_tool_usage_flusher()— background task, 10-minute intervalAuthUserextractor readsX-Tool-Nameheader, filters empty values, callsmark_usedon both auth pathsmemoria-api/src/state.rsAppStategainstool_usage_batcher: Arc<ToolUsageBatcher>init_auth_pool()callsrebuild_from_db()then starts the flushermemoria-api/src/lib.rs+routes/memory.rsGET /v1/tool-usage— returns current user tool access times from memorymemoria-storage/src/store.rsmigrate()createsmem_tool_usagetable withPRIMARY KEY (user_id, tool_name)Testing
auth::tests): mark/query, overwrite updates time, empty tool filteredtest_tool_usage_tracking): empty state, recording, empty header ignored, multiple tools, user isolationINSERT ON DUPLICATE KEY UPDATEverified against MatrixOne directly