-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
automatically reconnect pubsub when reading messages in blocking mode #2281
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
redis/connection.py
Outdated
@@ -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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
646df07
to
049919b
Compare
@kristjanvalur Thanks, great PR! |
@kristjanvalur Can you resolve the conflicts? |
Makes test discovery work, even without a redis connection.
049919b
to
1d5f029
Compare
Why is the async reconnection method different from the sync one? Is either one documented anywhere? |
Not sure what you mean. Both code paths now contain an extra |
Pull Request check-list
Please make sure to review and check all of these items:
$ tox
pass with this change (including linting)?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 thecan_read()
method, which is only invoked if the operation to read a message has a timeout. In particular, usinglisten()
to get the message, instead of theget_message()
with a timeout, thecan_read()
method isn't called, and so aconnect()
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 theConnection
object.