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

Use hiredis::pack_command to serialized the commands. #2570

Merged
merged 12 commits into from
Feb 6, 2023

Conversation

prokazov
Copy link
Contributor

@prokazov prokazov commented Feb 3, 2023

@chayim @dvora-h

Pull Request check-list

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

  • [x ] Does $ tox pass with this change (including linting)?

  • [ x] Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?

  • [ x] Is the new or changed code fully tested?

  • [ ?] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

  • [n/a ] Is there an example added to the examples folder (if applicable)?

  • [ x] Was the change added to CHANGES file?

    Historically, hiredis-py (Python extension module written in C and utilizing hiredis library) has been used only for deserialization of the Redis command's replies. With the new hiredis-py 2.2.1 release it's possible to do the opposite operation - serialization of the Python tuples, representing commands in the redis-py into a RESP-encoded bytes buffer. This PR:

  1. Extracts the pack command functionality form the Connection class and delegates it to an external object.
  2. PythonRespSerializer class that implements the extracted functionality.
  3. HiredisRespSerializer class that uses hiredis-py to serialize a redis-py command.

@codecov-commenter
Copy link

codecov-commenter commented Feb 4, 2023

Codecov Report

Base: 92.28% // Head: 92.27% // Decreases project coverage by -0.02% ⚠️

Coverage data is based on head (566712e) compared to base (428d609).
Patch coverage: 90.00% of modified lines in pull request are covered.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2570      +/-   ##
==========================================
- Coverage   92.28%   92.27%   -0.02%     
==========================================
  Files         115      115              
  Lines       29660    29691      +31     
==========================================
+ Hits        27373    27396      +23     
- Misses       2287     2295       +8     
Impacted Files Coverage Δ
redis/connection.py 86.74% <89.13%> (+0.06%) ⬆️
redis/utils.py 92.85% <100.00%> (+0.26%) ⬆️
tests/test_encoding.py 98.78% <100.00%> (+0.03%) ⬆️
redis/asyncio/cluster.py 91.68% <0.00%> (-0.33%) ⬇️
tests/test_asyncio/test_pubsub.py 99.37% <0.00%> (-0.16%) ⬇️
tests/test_cluster.py 96.75% <0.00%> (-0.11%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +688 to +693
if packer is not None:
return packer
elif HIREDIS_PACK_AVAILABLE:
return HiredisRespSerializer()
else:
return PythonRespSerializer(self._buffer_cutoff, self.encoder.encode)

Choose a reason for hiding this comment

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

seems that elif and else are not needed:

if packer is not None:
    return packer

if HIREDIS_PACK_AVAILABLE:
    return HiredisRespSerializer()

return PythonRespSerializer(self._buffer_cutoff, self.encoder.encode)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically - yes, we can get rid of else but , my understanding of general sentiment regarding elif in Python is: it's cleaner way to show that conditions intended to be mutually exclusive.

@chayim
Copy link
Contributor

chayim commented Feb 5, 2023

@prokazov given that hiredis isn't a dependency, but is an extra (i.e. pip install redis[hiredis] we can't enforce a hiredis version on existing users. This means that the hiredis check now becomes more complicated.

We need to wrap the pack_command call, with a check to ensure that hiredis is >= 2.2.1, specifically due to this change, otherwise we'll break the community.

Similarly @dvora-h this becomes 4.5.0 as discussed.

@zalmane
Copy link

zalmane commented Feb 5, 2023

@chayim Originally the check was checking for a version. It was later changed to check for the existence of the pack_command exposed in the hiredis. So effectively this validates if the hiredis-py contains this new method so it is backwards compatible.

Copy link
Collaborator

@dvora-h dvora-h left a comment

Choose a reason for hiding this comment

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

LGTM!

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

Successfully merging this pull request may close these issues.

7 participants