-
Notifications
You must be signed in to change notification settings - Fork 715
fix: read user session from db if the cache is missing #8846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR implements a fallback mechanism to read user sessions from the database when the cache is missing, improving session reliability across distributed deployments.
Key Changes:
- Converted
extract_auth_strto async to support database lookups for session tokens when cache misses occur - Refactored
FromRequesttrait implementation forAuthExtractorto use async futures instead of synchronousReadyfutures - Added
extract_basic_auth_strsynchronous variant for code paths that cannot be async (rate limiting middleware) - Updated all callers of
extract_auth_strto await the async version - Included minor code improvements:
as_deref()simplification andIngester {}direct instantiation
Implementation:
The session retrieval now follows a cache-aside pattern: check in-memory cache first, and on miss, fetch from database and populate cache. This ensures sessions remain accessible even when cache is cleared or in multi-node scenarios where cache synchronization might lag.
Confidence Score: 4/5
- This PR is safe to merge with minor considerations around error handling
- The implementation correctly adds DB fallback for session caching with proper async conversion. The main concern is the use of
.unwrap()on line 38 ofsession.rswhich could panic if the DB returns invalid UTF-8, though this is unlikely in practice. All async conversions are properly implemented and the synchronous variant maintains backward compatibility for rate limiting middleware. - Pay attention to
src/service/db/session.rsline 38 which uses.unwrap()on UTF-8 conversion
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| src/common/utils/auth.rs | 4/5 | Converted extract_auth_str to async to read user sessions from DB when cache misses occur; converted FromRequest implementation to async future; added extract_basic_auth_str for synchronous paths |
| src/service/db/session.rs | 5/5 | Implements cache-aside pattern with DB fallback for session retrieval - returns cached session or fetches from DB on cache miss |
Sequence Diagram
sequenceDiagram
participant Client
participant AuthExtractor
participant extract_auth_str
participant Cache as USER_SESSIONS Cache
participant DB as Database
participant Handler
Client->>AuthExtractor: HTTP Request with auth cookie
AuthExtractor->>extract_auth_str: Extract auth from request (async)
extract_auth_str->>extract_auth_str: Check auth_tokens cookie
alt session token found
extract_auth_str->>Cache: Check cache for session
alt cache hit
Cache-->>extract_auth_str: Return cached bearer token
else cache miss
extract_auth_str->>DB: session::get(session_key)
DB-->>extract_auth_str: Return bearer token from DB
extract_auth_str->>Cache: Cache the token
end
extract_auth_str-->>AuthExtractor: Return "Bearer {token}"
else basic/bearer token
extract_auth_str-->>AuthExtractor: Return token as-is
end
AuthExtractor->>Handler: Continue with authenticated request
7 files reviewed, 1 comment
|
| Status | Total | Passed | Failed | Skipped | Flaky | Pass Rate | Duration |
|---|---|---|---|---|---|---|---|
| All tests passed | 364 | 344 | 0 | 19 | 1 | 95% | 4m 41s |
|
| Status | Total | Passed | Failed | Skipped | Flaky | Pass Rate | Duration |
|---|---|---|---|---|---|---|---|
| All tests passed | 364 | 343 | 0 | 19 | 2 | 94% | 4m 43s |
|
| Status | Total | Passed | Failed | Skipped | Flaky | Pass Rate | Duration |
|---|---|---|---|---|---|---|---|
| All tests passed | 364 | 344 | 0 | 19 | 1 | 95% | 4m 38s |
PR Type
Bug fix, Enhancement
Description
Make AuthExtractor async, return Future
Extract auth from session stored in DB
Add async extract_auth_str helper
Improve session cache miss handling
Diagram Walkthrough
File Walkthrough
4 files
Make auth extraction async; support session tokens; refactor extractorSimplify policy extraction with as_derefAdjust ingester usage and feature gateUse basic-auth-only extractor for ratelimit rules3 files
Use async auth extraction in chat streamUse async auth extraction in logout handlerRead sessions from DB on cache miss and cache results