Skip to content

perf(auth): cache push_context's resolved user per session (#57) - #91

Merged
nycomp merged 1 commit into
mainfrom
perf/push-context-user-cache
Oct 4, 2026
Merged

nycomp merged 1 commit into
mainfrom
perf/push-context-user-cache

Conversation

@nycomp

@nycomp nycomp commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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-wide before_request hook, so every request from a session-carrying browser paid 1–2 synchronous auth API calls before the route handler ran:

  1. sessions.from_session() / logins.from_session()
  2. users[user_id].get()

The resolved user is now cached in-process, keyed by ("session"|"login", session_id), for user_cache_ttl (instance attribute, default USER_CACHE_TTL_SECONDS = 60). A warm request makes zero upstream calls.

Semantics and trade-offs

  • Freshness: a user's g.user may be up to 60s stale within the TTL window; user_id-keyed authorization is unaffected.
  • Revocation lag: a session revoked server-side — not through logout() — keeps resolving for up to the TTL. Client-initiated logout evicts immediately, as do stale-session NotFoundError paths.
  • No shared mutable state: the cache stores user.to_resource() and rehydrates a fresh campus.model.User per hit, so callers mutating flask.g.user cannot poison the cache.
  • Multi-worker: each worker keeps its own small dict; races are benign (both threads fetch, last write wins), so no lock is held on the request path.
  • Devices are cached with the login-session branch (g.device); the auth-session branch caches device=None as before.

Tests

tests/unit/test_push_context_cache.py (9 cases): first-request resolution counts, warm requests make no upstream calls, ttl = 0 forces refetch, g.user mutation 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__.py pre-exist on main (I001 import sort, ARG002 scope, F841 token).

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.
@nycomp
nycomp merged commit 3525566 into main Oct 4, 2026
2 checks passed
@nycomp
nycomp deleted the perf/push-context-user-cache branch October 7, 2026 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(auth): push_context makes 1-2 auth API calls on every request — cache or lazy-resolve the request user

2 participants