Skip to content

Conversation

brainix
Copy link
Contributor

@brainix brainix commented Nov 9, 2019

Currently, there's no __eq__() method defined on the Redis or
ConnectionPool classes. Therefore, no two instances of either of
these classes will ever be equal.

I have a use case where it would be nice for one Redis client instance
to be equal to another if they have the same connection kwargs; that is,
if they're connected to the same Redis database.

Pull Request check-list

Please make sure to review and check all of these items:

  • Does $ python setup.py test pass with this change (including linting)?
  • Does travis tests pass with this change (enable it first in your forked repo and wait for the travis 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)?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.

Description of change

Add an __eq__() method on the Redis and ConnectionPool classes that return true if they have the same connection kwargs; that is, if they're connected to the same Redis database.

Currently, there's no `__eq__()` method defined on the `Redis` or
`ConnectionPool` classes.  Therefore, no two instances of either of
these classes will ever be equal.

I have a use case where it would be nice for one Redis client instance
to be equal to another if they have the same connection kwargs; that is,
if they're connected to the same Redis database.
@codecov-io
Copy link

codecov-io commented Nov 9, 2019

Codecov Report

Merging #1240 into master will increase coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1240      +/-   ##
==========================================
+ Coverage   92.34%   92.41%   +0.06%     
==========================================
  Files          20       21       +1     
  Lines        5958     6010      +52     
==========================================
+ Hits         5502     5554      +52     
  Misses        456      456
Impacted Files Coverage Δ
redis/connection.py 82.38% <100%> (+0.04%) ⬆️
tests/test_connection_pool.py 97.76% <100%> (+0.16%) ⬆️
redis/client.py 85.32% <100%> (+0.01%) ⬆️
tests/test_client.py 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 29f4aa4...2f4e2ad. Read the comment docs.

As of this change, `connection_pool != None` and `redis != None` both
work, and both return `False`.
brainix added a commit to brainix/pottery that referenced this pull request Nov 9, 2019
This is a more natural place to compare Redis connections.  Then we can
use `ConnectionPool.__eq__()` to implement `Redis.__eq__()`.  This feels
less hacky.

I've submitted this change to `redis-py`:
redis/redis-py#1240

If it gets merged upstream, then I'll be able to delete this monkey
patch.
brainix added a commit to brainix/pottery that referenced this pull request Nov 9, 2019
This is a more natural place to compare Redis connections.  Then we can
use `ConnectionPool.__eq__()` to implement `Redis.__eq__()`.  This feels
less hacky.

I've submitted this change to `redis-py`:
redis/redis-py#1240

If it gets merged upstream, then I'll be able to delete this monkey
patch.
brainix added a commit to brainix/pottery that referenced this pull request Nov 11, 2019
* Add __eq__() method on ConnectionPool class

This is a more natural place to compare Redis connections.  Then we can
use `ConnectionPool.__eq__()` to implement `Redis.__eq__()`.  This feels
less hacky.

I've submitted this change to `redis-py`:
redis/redis-py#1240

If it gets merged upstream, then I'll be able to delete this monkey
patch.

* Comment on PR submitted to redis-py
@andymccurdy
Copy link
Contributor

Looks good to me, thanks!

@andymccurdy andymccurdy merged commit 74b044c into redis:master Nov 11, 2019
@brainix brainix deleted the eq-test-on-pools branch November 11, 2019 23:35
@andymccurdy
Copy link
Contributor

andymccurdy commented Feb 2, 2020

@brainix After thinking about this a bit more, I now believe this isn't the right way to go. Just because two connection pools share the same configuration doesn't make them identical. Clearly they manage a separate set of connections (to the same server). It seems wrong to say pool_a == pool_b when pool_a will never give me one of pool_b's connections or when I can call pool_a.disconnect() but pool_b's connections are still active.

I'd tentatively be OK with saying client_a == client_b if they share the same connection pool instance. I'm not sure if that helps you at all.

I'm going to revert this change for now. If you really need this functionality it's trivial to compare r1.connection_pool.connection_kwargs == r2.connection_pool.connection_kwargs.

andymccurdy added a commit that referenced this pull request Feb 2, 2020
After further thought this was a bad idea. Just because two connection
pools share the same connection arguments does not make them equal.
It would seem quite odd if pool_a == pool_b yet pool_a.disconnect() doesn't
close all of pool_b's connections.

Ref #1240
Fixes #1277
Fixes #1275
Fixes #1267
Fixes #1273
@brainix
Copy link
Contributor Author

brainix commented Feb 2, 2020

@andymccurdy - Thanks for the heads up! That works for me.

brainix added a commit to brainix/pottery that referenced this pull request Aug 8, 2020
Apparently, this broke Celery when I try to contribute it upstream, heh.
Instead, monkey patch `__eq__()` on the `ConnectionPool` class.

More context:
redis/redis-py#1240 (comment)
brainix added a commit to brainix/pottery that referenced this pull request Aug 8, 2020
Apparently, this broke Celery when I try to contribute it upstream, heh.
Instead, monkey patch `__eq__()` on the `ConnectionPool` class.

More context:
redis/redis-py#1240 (comment)
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.

3 participants