Repository navigation
perf(auth): cache push_context's resolved user per session (#57) - #91
Merged
Merged
Conversation
push_context() runs as an app-wide before_request hook, so a session-carrying browser paid 1-2 synchronous auth API calls before every route — enough to wedge sync workers under load (campus-admin WORKER TIMEOUT, campus-profile#22 slow authenticated pages). The resolved user is now cached in-process keyed by session id for user_cache_ttl (default 60s): warm requests make zero upstream calls. logout() and stale-session NotFoundError paths evict immediately; the resource dict is rehydrated per hit so callers cannot poison the cache by mutating flask.g.user. Trade-off: a session revoked server-side — not through logout() — keeps resolving for up to the TTL window.
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.
Fixes #57. Root cause of the slow authenticated pages in nyjc-computing/campus-profile#22 and a contributor to the campus-admin WORKER TIMEOUT incident (nyjc-computing/campus-admin#13).
What
push_context()runs as an app-widebefore_requesthook, so every request from a session-carrying browser paid 1–2 synchronous auth API calls before the route handler ran:sessions.from_session()/logins.from_session()users[user_id].get()The resolved user is now cached in-process, keyed by
("session"|"login", session_id), foruser_cache_ttl(instance attribute, defaultUSER_CACHE_TTL_SECONDS = 60). A warm request makes zero upstream calls.Semantics and trade-offs
g.usermay be up to 60s stale within the TTL window;user_id-keyed authorization is unaffected.logout()— keeps resolving for up to the TTL. Client-initiated logout evicts immediately, as do stale-sessionNotFoundErrorpaths.user.to_resource()and rehydrates a freshcampus.model.Userper hit, so callers mutatingflask.g.usercannot poison the cache.g.device); the auth-session branch cachesdevice=Noneas before.Tests
tests/unit/test_push_context_cache.py(9 cases): first-request resolution counts, warm requests make no upstream calls,ttl = 0forces refetch,g.usermutation does not poison the cache, logout eviction, and stale session eviction for both branches. Full suite: 238 passed.Ruff (repo config) is clean on both touched files; the 3 remaining findings in
auth/v1/__init__.pypre-exist onmain(I001 import sort, ARG002scope, F841token).