Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically reconnect pubsub when reading messages in blocking mode #2281

Merged

Conversation

kristjanvalur
Copy link
Contributor

@kristjanvalur kristjanvalur commented Jul 17, 2022

Pull Request check-list

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

  • Does $ tox pass with this change (including linting)?
  • 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)?
  • Was the change added to CHANGES file?

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

Description of change

Issue #2089 demonstrates how a pub-sub connection doesn't automatically reconnect. Further investigation showed that this was due to the fact that the connection only performed a just-in-time connect in the can_read() method, which is only invoked if the operation to read a message has a timeout. In particular, using listen() to get the message, instead of the get_message() with a timeout, the can_read() method isn't called, and so a connect() call is not made.

This issue is present in both non-async and async versions of the library as demonstrated with the unit-test code.

The PR fixes this by adding a just-in-time connect to the read_response call of both the Connection object.

@codecov-commenter
Copy link

codecov-commenter commented Jul 17, 2022

Codecov Report

Merging #2281 (646df07) into master (99b4df4) will increase coverage by 0.05%.
The diff coverage is 95.83%.

❗ Current head 646df07 differs from pull request most recent head 049919b. Consider uploading reports for the commit 049919b to get more accurate results

@@            Coverage Diff             @@
##           master    #2281      +/-   ##
==========================================
+ Coverage   91.91%   91.97%   +0.05%     
==========================================
  Files         109      109              
  Lines       27991    28171     +180     
==========================================
+ Hits        25729    25911     +182     
+ Misses       2262     2260       -2     
Impacted Files Coverage Δ
tests/conftest.py 84.94% <58.33%> (-1.57%) ⬇️
tests/test_asyncio/test_pubsub.py 99.13% <97.50%> (-0.27%) ⬇️
tests/test_pubsub.py 99.59% <98.75%> (-0.17%) ⬇️
redis/asyncio/client.py 92.78% <100.00%> (+0.04%) ⬆️
redis/client.py 89.06% <100.00%> (+0.48%) ⬆️
tests/test_asyncio/compat.py 100.00% <100.00%> (ø)
tests/test_asyncio/test_cluster.py 97.44% <0.00%> (-0.14%) ⬇️
tests/test_cluster.py 96.85% <0.00%> (ø)
redis/asyncio/connection.py 84.58% <0.00%> (+0.44%) ⬆️
... and 1 more

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 99b4df4...049919b. Read the comment docs.

@@ -815,6 +815,8 @@ def can_read(self, timeout=0):

def read_response(self, disable_decoding=False):
"""Read the response from a previously sent command"""
if not self._sock:
self.connect()
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be added to the pub/sub code instead of adding this check for every redis request?

Copy link
Contributor Author

@kristjanvalur kristjanvalur Jul 24, 2022

Choose a reason for hiding this comment

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

Possibly. read_response() is after all used in other places than the pub-sub. On the other hand, can_read() has the connect internally, which is the reason it works for non-blocking cases.`
I'll see if I can do this differently.

Copy link
Contributor Author

@kristjanvalur kristjanvalur Jul 25, 2022

Choose a reason for hiding this comment

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

Thanks for the suggestion. Now the can_read/read_response interaction, along with an extra connect(), is wrapped in a closure passed to PubSub._execute()

@kristjanvalur kristjanvalur force-pushed the kristjan/async-pubsub-auto-reconnect branch from 646df07 to 049919b Compare July 24, 2022 23:28
@dvora-h dvora-h added the bug Bug label Jul 27, 2022
@dvora-h
Copy link
Collaborator

dvora-h commented Jul 27, 2022

@kristjanvalur Thanks, great PR!

@dvora-h
Copy link
Collaborator

dvora-h commented Jul 27, 2022

@kristjanvalur Can you resolve the conflicts?

@kristjanvalur kristjanvalur force-pushed the kristjan/async-pubsub-auto-reconnect branch from 049919b to 1d5f029 Compare July 27, 2022 15:35
@dvora-h dvora-h merged commit f9f9d06 into redis:master Jul 27, 2022
@kristjanvalur kristjanvalur deleted the kristjan/async-pubsub-auto-reconnect branch July 27, 2022 18:28
@agronholm
Copy link
Contributor

Why is the async reconnection method different from the sync one? Is either one documented anywhere?

@kristjanvalur
Copy link
Contributor Author

Not sure what you mean. Both code paths now contain an extra conn.connect() in the PubSub read method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants