feat: add asynchronous support to IP restriction middleware with comprehensive tests #4604
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.
Overview
Re-implements the async support for IP restriction middleware that was originally introduced in PR #3831, now with comprehensive test coverage to ensure correctness and reliability.
Problem
PR #3831 added asynchronous support to the IP restriction middleware but did not include tests to verify the functionality. This PR re-implements the same async capabilities with a complete test suite covering both synchronous and asynchronous code paths.
Changes
Middleware Enhancements (
blt/middleware/ip_restrict.py
)Async Support:
__acall__
method to handle asynchronous requestsincrement_block_count_async
as an async wrapper usingsync_to_async
record_ip_async
for async IP recordingCode Refactoring:
_record_ip
helper method for code reuse__call__
to useprocess_request_sync
internallyBug Fixes:
blocked_agents()
method to prevent false positives when Blocked entries have empty user_agent_string fieldsincrement_block_count()
to prevent matching all user agents when empty strings are presentTest Coverage (
blt/middleware/test_ip_restrict.py
)Created comprehensive test suite with 13 tests covering:
Synchronous Middleware:
Edge Cases:
Helper Methods:
_record_ip
method testingAsync Support:
Test Results
All tests pass successfully. The async test is skipped when using SQLite due to database locking limitations during concurrent transactions, but works with PostgreSQL.
Technical Details
The async implementation uses Django's
sync_to_async
to properly wrap ORM operations for use in async contexts:Backward Compatibility
The synchronous
__call__
method continues to work exactly as before, ensuring no breaking changes for existing deployments.How to Test
python manage.py test blt.middleware.test_ip_restrict
Related
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com
/usr/lib/apt/methods/https
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.