Fix ConnectionPool to raise MaxConnectionsError instead of Connection… by ngabhanenetskope · Pull Request #3698 · redis/redis-py (original) (raw)
Pull Request check-list
Please make sure to review and check all of these items:
- [✅] Do tests and lints pass with this change?
- [✅] Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
- [✅] Is the new or changed code fully tested?
- [✅] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
- Is there an example added to the examples folder (if applicable)? (N/A - This is an error class enhancement)
Description of change
This PR addresses issue #3684 by introducing a more specific MaxConnectionsError exception that is raised when a connection pool reaches its maximum connections limit.
Current behavior:
When a ConnectionPool reaches its maximum connections limit, it raises a generic ConnectionError. This causes RedisCluster to treat the error as a node failure and trigger an unnecessary cluster reinitialization, leading to increased resource usage.
Changes:
- Added a new
MaxConnectionsErrorclass inredis/exceptions.pyas a subclass ofConnectionError - Modified
ConnectionPool.make_connection()inredis/connection.pyto raise the newMaxConnectionsErrorinstead of a genericConnectionError - Updated
RedisCluster._execute_command()inredis/cluster.pyto handleMaxConnectionsErrorseparately, preventing unnecessary cluster reinitialization - Updated tests to verify the new behavior in both standalone and cluster modes
- Added exports in
__init__.pyto make the new exception available
Testing:
- Updated
test_max_connectionsintest_connection_pool.pyto expectMaxConnectionsError - Added a new test file
test_max_connections_error.pywith:- Inheritance test to verify
MaxConnectionsErrorextendsConnectionError - Connection pool test to verify the correct error is raised
- Mock-based cluster test to verify cluster doesn't reinitialize on
MaxConnectionsError
- Inheritance test to verify
All tests pass locally, including the new tests specifically for this behavior.
Note: The issue was previously assigned but appears to be inactive. I implemented this fix because it was affecting our production environment.