From e74ed1942a746683ece792cb1670c544f599246f Mon Sep 17 00:00:00 2001 From: Chayim Date: Wed, 17 Nov 2021 11:54:45 +0200 Subject: removing hiredis warning (#1721) --- redis/connection.py | 5 ----- 1 file changed, 5 deletions(-) (limited to 'redis/connection.py') diff --git a/redis/connection.py b/redis/connection.py index cb9acb4..35e9491 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -63,11 +63,6 @@ if HIREDIS_AVAILABLE: HIREDIS_SUPPORTS_ENCODING_ERRORS = \ hiredis_version >= LooseVersion('1.0.0') - if not HIREDIS_SUPPORTS_BYTE_BUFFER: - msg = ("redis-py works best with hiredis >= 0.1.4. You're running " - "hiredis %s. Please consider upgrading." % hiredis.__version__) - warnings.warn(msg) - HIREDIS_USE_BYTE_BUFFER = True # only use byte buffer if hiredis supports it if not HIREDIS_SUPPORTS_BYTE_BUFFER: -- cgit v1.2.1 From 791f482dcb320f48cf950c4b2c6047d1981a8f67 Mon Sep 17 00:00:00 2001 From: Alex Wu Date: Sat, 20 Nov 2021 23:47:44 -0800 Subject: Better removal of hiredis warning (#1726) Co-authored-by: Alex Wu --- redis/connection.py | 4 ---- 1 file changed, 4 deletions(-) (limited to 'redis/connection.py') diff --git a/redis/connection.py b/redis/connection.py index 35e9491..e01742d 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -9,7 +9,6 @@ import io import os import socket import threading -import warnings import weakref from redis.exceptions import ( @@ -67,9 +66,6 @@ if HIREDIS_AVAILABLE: # only use byte buffer if hiredis supports it if not HIREDIS_SUPPORTS_BYTE_BUFFER: HIREDIS_USE_BYTE_BUFFER = False -else: - msg = "redis-py works best with hiredis. Please consider installing" - warnings.warn(msg) SYM_STAR = b'*' SYM_DOLLAR = b'$' -- cgit v1.2.1 From 9db1eec71b443b8e7e74ff503bae651dc6edf411 Mon Sep 17 00:00:00 2001 From: Bar Shaul <88437685+barshaul@users.noreply.github.com> Date: Thu, 25 Nov 2021 14:15:24 +0200 Subject: Adding RedisCluster client to support Redis Cluster Mode (#1660) Co-authored-by: Chayim Co-authored-by: Anas --- redis/connection.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'redis/connection.py') diff --git a/redis/connection.py b/redis/connection.py index e01742d..eac9db3 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -11,6 +11,7 @@ import socket import threading import weakref +from redis.backoff import NoBackoff from redis.exceptions import ( AuthenticationError, AuthenticationWrongNumberOfArgsError, @@ -28,9 +29,9 @@ from redis.exceptions import ( TimeoutError, ModuleError, ) -from redis.utils import HIREDIS_AVAILABLE, str_if_bytes -from redis.backoff import NoBackoff + from redis.retry import Retry +from redis.utils import HIREDIS_AVAILABLE, str_if_bytes try: import ssl @@ -498,7 +499,7 @@ class Connection: encoding_errors='strict', decode_responses=False, parser_class=DefaultParser, socket_read_size=65536, health_check_interval=0, client_name=None, username=None, - retry=None): + retry=None, redis_connect_func=None): """ Initialize a new Connection. To specify a retry policy, first set `retry_on_timeout` to `True` @@ -528,8 +529,10 @@ class Connection: self.health_check_interval = health_check_interval self.next_health_check = 0 self.encoder = Encoder(encoding, encoding_errors, decode_responses) + self.redis_connect_func = redis_connect_func self._sock = None - self._parser = parser_class(socket_read_size=socket_read_size) + self._socket_read_size = socket_read_size + self.set_parser(parser_class) self._connect_callbacks = [] self._buffer_cutoff = 6000 @@ -559,6 +562,14 @@ class Connection: def clear_connect_callbacks(self): self._connect_callbacks = [] + def set_parser(self, parser_class): + """ + Creates a new instance of parser_class with socket size: + _socket_read_size and assigns it to the parser for the connection + :param parser_class: The required parser class + """ + self._parser = parser_class(socket_read_size=self._socket_read_size) + def connect(self): "Connects to the Redis server if not already connected" if self._sock: @@ -572,7 +583,12 @@ class Connection: self._sock = sock try: - self.on_connect() + if self.redis_connect_func is None: + # Use the default on_connect function + self.on_connect() + else: + # Use the passed function redis_connect_func + self.redis_connect_func(self) except RedisError: # clean up after any error in on_connect self.disconnect() @@ -903,7 +919,8 @@ class UnixDomainSocketConnection(Connection): self.next_health_check = 0 self.encoder = Encoder(encoding, encoding_errors, decode_responses) self._sock = None - self._parser = parser_class(socket_read_size=socket_read_size) + self._socket_read_size = socket_read_size + self.set_parser(parser_class) self._connect_callbacks = [] self._buffer_cutoff = 6000 -- cgit v1.2.1 From d7b56103ed4d0ba9c05c74ca5580c72fcb70c09c Mon Sep 17 00:00:00 2001 From: Chayim Date: Thu, 25 Nov 2021 14:18:33 +0200 Subject: Adding support for non-decodable commands (#1731) --- redis/connection.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'redis/connection.py') diff --git a/redis/connection.py b/redis/connection.py index eac9db3..2f91fae 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -315,7 +315,7 @@ class PythonParser(BaseParser): def can_read(self, timeout): return self._buffer and self._buffer.can_read(timeout) - def read_response(self): + def read_response(self, disable_decoding=False): raw = self._buffer.readline() if not raw: raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) @@ -355,8 +355,9 @@ class PythonParser(BaseParser): length = int(response) if length == -1: return None - response = [self.read_response() for i in range(length)] - if isinstance(response, bytes): + response = [self.read_response(disable_decoding=disable_decoding) + for i in range(length)] + if isinstance(response, bytes) and disable_decoding is False: response = self.encoder.decode(response) return response @@ -450,7 +451,7 @@ class HiredisParser(BaseParser): if custom_timeout: sock.settimeout(self._socket_timeout) - def read_response(self): + def read_response(self, disable_decoding=False): if not self._reader: raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) @@ -758,10 +759,12 @@ class Connection: self.connect() return self._parser.can_read(timeout) - def read_response(self): + def read_response(self, disable_decoding=False): """Read the response from a previously sent command""" try: - response = self._parser.read_response() + response = self._parser.read_response( + disable_decoding=disable_decoding + ) except socket.timeout: self.disconnect() raise TimeoutError("Timeout reading from %s:%s" % -- cgit v1.2.1 From 884f7adc871fbae352dfea099a57a1534a8588c6 Mon Sep 17 00:00:00 2001 From: Chayim Date: Thu, 25 Nov 2021 16:32:28 +0200 Subject: Fixing deprecating distutils (PEP 632) (#1730) --- redis/connection.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'redis/connection.py') diff --git a/redis/connection.py b/redis/connection.py index 2f91fae..6ff3650 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -1,4 +1,4 @@ -from distutils.version import LooseVersion +from packaging.version import Version from itertools import chain from time import time from queue import LifoQueue, Empty, Full @@ -55,13 +55,13 @@ NONBLOCKING_EXCEPTIONS = tuple(NONBLOCKING_EXCEPTION_ERROR_NUMBERS.keys()) if HIREDIS_AVAILABLE: import hiredis - hiredis_version = LooseVersion(hiredis.__version__) + hiredis_version = Version(hiredis.__version__) HIREDIS_SUPPORTS_CALLABLE_ERRORS = \ - hiredis_version >= LooseVersion('0.1.3') + hiredis_version >= Version('0.1.3') HIREDIS_SUPPORTS_BYTE_BUFFER = \ - hiredis_version >= LooseVersion('0.1.4') + hiredis_version >= Version('0.1.4') HIREDIS_SUPPORTS_ENCODING_ERRORS = \ - hiredis_version >= LooseVersion('1.0.0') + hiredis_version >= Version('1.0.0') HIREDIS_USE_BYTE_BUFFER = True # only use byte buffer if hiredis supports it -- cgit v1.2.1