-
Notifications
You must be signed in to change notification settings - Fork 730
fix: test concurrent cache-miss #4340
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { TimeoutError, generateUUIDv4, timeout } from '@crowd/common' | ||
|
|
||
| import { acquireLock, releaseLock } from './mutex' | ||
| import { RedisClient } from './types' | ||
|
|
||
| export interface SingleFlightOptions { | ||
| lockTtlSeconds: number | ||
| waitTimeoutSeconds: number | ||
| } | ||
|
|
||
| // 100-300ms jittered, matching mutex.ts's own retry spacing. | ||
| const randomPollDelayMs = () => Math.floor(Math.random() * 200) + 100 | ||
|
|
||
| /** | ||
| * Ensures only one caller per `lockKey` actually runs `compute()` at a time. A caller | ||
| * that loses the initial race polls `readCached()` and retries a non-blocking lock | ||
| * acquisition every ~100-300ms — so it returns as soon as either the holder finishes | ||
| * and populates the cache, or the lock frees up (holder crashed/released), without | ||
| * waiting out the full timeout in either case. If Redis itself is unreachable, lock | ||
| * acquisition fails open (proceeds unlocked) rather than failing the caller. | ||
| * | ||
| * `waitTimeoutSeconds` should be >= `lockTtlSeconds`: otherwise a waiter can give up | ||
| * and run `compute()` unlocked while the original holder is still legitimately working | ||
| * within its own TTL window, reproducing the exact stampede this helper exists to avoid. | ||
| */ | ||
| export async function withSingleFlight<T>( | ||
| client: RedisClient, | ||
| lockKey: string, | ||
| { lockTtlSeconds, waitTimeoutSeconds }: SingleFlightOptions, | ||
| readCached: () => Promise<T | null>, | ||
| compute: () => Promise<T>, | ||
| ): Promise<T> { | ||
| const token = generateUUIDv4() | ||
| const deadline = Date.now() + waitTimeoutSeconds * 1000 | ||
| let holdsLock = false | ||
|
ulemons marked this conversation as resolved.
|
||
|
|
||
|
ulemons marked this conversation as resolved.
|
||
| for (;;) { | ||
| try { | ||
| await acquireLock(client, lockKey, token, lockTtlSeconds, 0) | ||
| holdsLock = true | ||
| break | ||
| } catch (error) { | ||
| if (!(error instanceof TimeoutError)) { | ||
| // Redis unavailable for locking — fail open, compute unprotected. | ||
| break | ||
| } | ||
| } | ||
|
|
||
| const cached = await readCached() | ||
| if (cached !== null) { | ||
| return cached | ||
| } | ||
|
|
||
| if (Date.now() >= deadline) { | ||
| break | ||
|
ulemons marked this conversation as resolved.
|
||
| } | ||
|
|
||
| await timeout(randomPollDelayMs()) | ||
| } | ||
|
|
||
| try { | ||
| if (holdsLock) { | ||
| // Whoever held the lock right before us may have finished and populated the | ||
| // cache between our last poll and winning it — avoid a redundant compute. | ||
| const cached = await readCached() | ||
| if (cached !== null) { | ||
| return cached | ||
| } | ||
| } | ||
|
|
||
| return await compute() | ||
| } finally { | ||
| if (holdsLock) { | ||
| await releaseLock(client, lockKey, token) | ||
|
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
|
||
| } | ||
|
ulemons marked this conversation as resolved.
|
||
| } | ||
|
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
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.
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.
Compute locks survive invalidation
Medium Severity
Single-flight and background refresh now take Redis mutexes on unprefixed cache key strings via
acquireLock, butMemberQueryCacheinvalidation still only deletes locks under themembers-refresh-lockcache prefix. After cache invalidation, leftover compute locks can block repopulation for up to the 90s TTL.Additional Locations (2)
services/libs/data-access-layer/src/members/base.ts#L668-L671services/libs/data-access-layer/src/members/queryCache.ts#L118-L140Reviewed by Cursor Bugbot for commit 60042d7. Configure here.