Skip to content

Add allow_unsafe_throttle_cache setting for cache backend safety checks #998#998

Merged
sobolevn merged 16 commits intowemake-services:masterfrom
n05tr0m0:master
May 6, 2026
Merged

Add allow_unsafe_throttle_cache setting for cache backend safety checks #998#998
sobolevn merged 16 commits intowemake-services:masterfrom
n05tr0m0:master

Conversation

@n05tr0m0
Copy link
Copy Markdown
Contributor

@n05tr0m0 n05tr0m0 commented May 3, 2026

AI Policy

  • I have read and agree to the AI Policy, removed any "Co-Authored-By" lines attributing coding agents, and manually reviewed the final result

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made

Related issues

Closes #978

LocMemCache and DummyCache don't share state between processes, so using them for throttling silently breaks rate limiting in production. This PR makes that configuration fail loudly.

By default, an ImproperlyConfigured exception is raised when an unsafe backend is detected. Setting throttle_allow_unsafe_cache = True in DMR_SETTINGS opts in to unsafe behavior – in that case an UnsafeCacheBackendWarning is emitted so at least you know it's on.

The check runs once in __post_init__ when the cache backend is constructed, so there's no overhead on every request.

n05tr0m0 added 4 commits May 4, 2026 01:02
…ecks

<!--
Hi, thanks for submitting a Pull Request. We appreciate it.

Please, fill in all the required information
to make our review and merging processes easier.

Cheers!
-->

## AI Policy

- [x] I have read and agree to the [AI Policy](https://github.com/wemake-services/django-modern-rest/blob/master/.github/AI_POLICY.md), removed any "Co-Authored-By" lines attributing coding agents, and manually reviewed the final result

## Checklist

<!-- Please check everything that applies: -->

- [x] I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
- [x] I have created at least one test case for the changes I have made
- [x] I have updated the documentation for the changes I have made

## Related issues
Closes wemake-services#978

<!--
Mark what issues this Pull Request closes or references.

Format is:
- Closes #issue-number
- Refs #issue-number

Example. Refs #0
Documentation: https://blog.github.com/2013-05-14-closing-issues-via-pull-requests/
-->

<!--
If you have any feedback, just write it here.

It can be whatever you want!
-->

`LocMemCache` and `DummyCache` don't share state between processes, so using them for throttling silently breaks rate limiting in production. This PR makes that configuration fail loudly.

By default, an `ImproperlyConfigured` exception is raised when an unsafe backend is detected. Setting `allow_unsafe_throttle_cache = True` in `DMR_SETTINGS` opts in to unsafe behavior – in that case a UserWarning is emitted on the first throttling operation so at least you know it's on.

The check is lazy (fires on first incr/get, not at instantiation) to avoid breaking tests that create backend instances early. `_CHECKED_CACHES` is module-level so the check doesn't repeat on every request, and it's intentionally not tied to `clear_settings_cache()`.
Fix invalid cross-reference in throttling docs
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks a lot!

Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/django_cache.py Outdated
Comment thread docs/pages/throttling.rst Outdated
Comment thread tests/test_unit/test_throttling/test_unsafe_cache_warning.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
n05tr0m0 added 3 commits May 4, 2026 16:13
…make-services#978)

- Final annotations on all constants
- Drop mutable module state, move check to post_init
- Add UnsafeCacheBackendWarning subclass
- Fix test ordering issue with DMR_SETTINGS + LRU cache
- Switch to dmr_clean_settings fixture
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final round of review :)

Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread dmr/throttling/backends/_cache_safety.py Outdated
Comment thread tests/test_unit/test_throttling/test_unsafe_cache_warning.py Outdated
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 4, 2026

Merging this PR will not alter performance

✅ 10 untouched benchmarks


Comparing n05tr0m0:master (6b11d9b) with master (0320909)

Open in CodSpeed

Comment thread pyproject.toml Outdated
Comment thread pyproject.toml Outdated
Comment thread tests/test_unit/test_throttling/test_unsafe_cache_warning.py Outdated
@n05tr0m0 n05tr0m0 requested a review from sobolevn May 5, 2026 22:22
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will take it from here ;)

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (bd9fe84) to head (6b11d9b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #998   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          380       381    +1     
  Lines        16290     16386   +96     
  Branches       518       524    +6     
=========================================
+ Hits         16290     16386   +96     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sobolevn sobolevn merged commit 849dacb into wemake-services:master May 6, 2026
26 checks passed
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.

Warn unsafe cache deployments

2 participants