Add SETNX cache stampede protection#378
Open
silentgeckoaudit3801 wants to merge 3 commits into
Open
Conversation
|
|
||
|
|
||
| def test_cache_miss_uses_setnx_refresh_claim(): | ||
| assert "claim_key = f\"{key}:refresh-lock\"" in CACHE_SOURCE |
|
|
||
| def test_cache_miss_uses_setnx_refresh_claim(): | ||
| assert "claim_key = f\"{key}:refresh-lock\"" in CACHE_SOURCE | ||
| assert "nx=True" in CACHE_SOURCE |
| def test_cache_miss_uses_setnx_refresh_claim(): | ||
| assert "claim_key = f\"{key}:refresh-lock\"" in CACHE_SOURCE | ||
| assert "nx=True" in CACHE_SOURCE | ||
| assert "ex=_REFRESH_CLAIM_SECONDS" in CACHE_SOURCE |
| assert "claim_key = f\"{key}:refresh-lock\"" in CACHE_SOURCE | ||
| assert "nx=True" in CACHE_SOURCE | ||
| assert "ex=_REFRESH_CLAIM_SECONDS" in CACHE_SOURCE | ||
| assert "_REFRESH_CLAIM_SECONDS = 5" in CACHE_SOURCE |
|
|
||
|
|
||
| def test_concurrent_miss_waits_for_claimer_before_fetching(): | ||
| assert "if not claimed:" in CACHE_SOURCE |
| assert "if not claimed:" in CACHE_SOURCE | ||
| assert "refreshed = await _wait_for_refresh(client, key)" in CACHE_SOURCE | ||
| assert "if refreshed is not None:" in CACHE_SOURCE | ||
| assert "return refreshed" in CACHE_SOURCE |
| assert "refreshed = await _wait_for_refresh(client, key)" in CACHE_SOURCE | ||
| assert "if refreshed is not None:" in CACHE_SOURCE | ||
| assert "return refreshed" in CACHE_SOURCE | ||
| assert "value = await fetch_fn()" in CACHE_SOURCE |
|
|
||
|
|
||
| def test_wait_loop_polls_cached_value_with_deadline(): | ||
| assert "deadline = asyncio.get_running_loop().time() + _REFRESH_CLAIM_SECONDS" in CACHE_SOURCE |
|
|
||
| def test_wait_loop_polls_cached_value_with_deadline(): | ||
| assert "deadline = asyncio.get_running_loop().time() + _REFRESH_CLAIM_SECONDS" in CACHE_SOURCE | ||
| assert "await asyncio.sleep(_REFRESH_POLL_SECONDS)" in CACHE_SOURCE |
| def test_wait_loop_polls_cached_value_with_deadline(): | ||
| assert "deadline = asyncio.get_running_loop().time() + _REFRESH_CLAIM_SECONDS" in CACHE_SOURCE | ||
| assert "await asyncio.sleep(_REFRESH_POLL_SECONDS)" in CACHE_SOURCE | ||
| assert "cached = await _read_cached_value(client, key)" in CACHE_SOURCE No newline at end of file |
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 #236.
This adds TTL-bound cache stampede protection to get_cached_or_fetch:
I did not run local pytest in this workspace because we are intentionally avoiding dependency installation and project toolchain execution.