-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add Redis readiness verification (#3555) #3596
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,7 @@ def __init__( | |
encoding: str = "utf-8", | ||
encoding_errors: str = "strict", | ||
decode_responses: bool = False, | ||
check_ready: bool = False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add details about this setting to the pydocs. Also, I would suggest changing the name so that it would be easier to understand its purpose just by looking at the name, something like |
||
retry_on_timeout: bool = False, | ||
retry_on_error: Optional[List[Type[Exception]]] = None, | ||
ssl: bool = False, | ||
|
@@ -265,6 +266,7 @@ def __init__( | |
"encoding": encoding, | ||
"encoding_errors": encoding_errors, | ||
"decode_responses": decode_responses, | ||
"check_ready": check_ready, | ||
"retry_on_error": retry_on_error, | ||
"retry": copy.deepcopy(retry), | ||
"max_connections": max_connections, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,6 +236,7 @@ def __init__( | |
encoding: str = "utf-8", | ||
encoding_errors: str = "strict", | ||
decode_responses: bool = False, | ||
check_ready: bool = False, | ||
parser_class=DefaultParser, | ||
socket_read_size: int = 65536, | ||
health_check_interval: int = 0, | ||
|
@@ -302,6 +303,7 @@ def __init__( | |
self.redis_connect_func = redis_connect_func | ||
self.encoder = Encoder(encoding, encoding_errors, decode_responses) | ||
self.handshake_metadata = None | ||
self.check_ready = check_ready | ||
self._sock = None | ||
self._socket_read_size = socket_read_size | ||
self.set_parser(parser_class) | ||
|
@@ -378,12 +380,35 @@ def connect(self): | |
"Connects to the Redis server if not already connected" | ||
self.connect_check_health(check_health=True) | ||
|
||
def _connect_check_ready(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move this method after |
||
sock = self._connect() | ||
|
||
# Doing handshake since connect and send operations work even when Redis is not ready | ||
if self.check_ready: | ||
try: | ||
ping_parts = self._command_packer.pack("PING") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to replace this block with a call to |
||
for part in ping_parts: | ||
sock.sendall(part) | ||
|
||
response = str_if_bytes(sock.recv(1024)) | ||
if not (response.startswith("+PONG") or response.startswith("-NOAUTH")): | ||
raise ResponseError(f"Invalid PING response: {response}") | ||
except (ConnectionResetError, ResponseError) as err: | ||
try: | ||
sock.shutdown(socket.SHUT_RDWR) # ensure a clean close | ||
except OSError: | ||
pass | ||
sock.close() | ||
raise ConnectionError(self._error_message(err)) | ||
return sock | ||
|
||
def connect_check_health(self, check_health: bool = True): | ||
if self._sock: | ||
return | ||
try: | ||
sock = self.retry.call_with_retry( | ||
lambda: self._connect(), lambda error: self.disconnect(error) | ||
lambda: self._connect_check_ready(), | ||
lambda error: self.disconnect(error), | ||
) | ||
except socket.timeout: | ||
raise TimeoutError("Timeout connecting to server") | ||
|
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.
Why do you remove this call?