Skip to content

Commit 1388cb9

Browse files
committed
Fixing a check if we should send hitless handshake. Fixing merge issues.
1 parent a7dd150 commit 1388cb9

File tree

3 files changed

+6
-60
lines changed

3 files changed

+6
-60
lines changed

redis/connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,13 @@ def on_connect_check_health(self, check_health: bool = True):
685685
raise ConnectionError("Invalid RESP version")
686686

687687
# Send maintenance notifications handshake if RESP3 is active and maintenance events are enabled
688+
# and we have a host to determine the endpoint type from
688689
if (
689690
self.protocol not in [2, "2"]
690691
and self.maintenance_events_config
691692
and self.maintenance_events_config.enabled
692-
and hasattr(self, "_maintenance_event_connection_handler")
693+
and self._maintenance_event_connection_handler
694+
and hasattr(self, "host")
693695
):
694696
try:
695697
endpoint_type = self.maintenance_events_config.get_endpoint_type(

redis/maintenance_events.py

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MaintenanceState(enum.Enum):
1515
MOVING = "moving"
1616
MAINTENANCE = "maintenance"
1717

18-
18+
1919
class EndpointType:
2020
"""Constants for valid endpoint types used in CLIENT MAINT_NOTIFICATIONS command."""
2121

@@ -25,7 +25,6 @@ class EndpointType:
2525
EXTERNAL_FQDN = "external-fqdn"
2626
NONE = "none"
2727

28-
2928
@classmethod
3029
def get_valid_types(cls):
3130
"""Return a set of all valid endpoint types."""
@@ -37,7 +36,7 @@ def get_valid_types(cls):
3736
cls.NONE,
3837
}
3938

40-
39+
4140
if TYPE_CHECKING:
4241
from redis.connection import (
4342
BlockingConnectionPool,
@@ -424,60 +423,6 @@ def _is_private_fqdn(host: str) -> bool:
424423
return False
425424

426425

427-
def _get_resolved_ip_from_connection(
428-
connection: "ConnectionInterface",
429-
) -> Optional[str]:
430-
"""
431-
Extract the resolved IP address from an established connection.
432-
433-
First tries to get the actual IP from the socket (most accurate),
434-
then falls back to DNS resolution if needed.
435-
436-
Args:
437-
connection: The connection object to extract the IP from
438-
439-
Returns:
440-
str: The resolved IP address, or None if it cannot be determined
441-
"""
442-
import socket
443-
444-
# Method 1: Try to get the actual IP from the established socket connection
445-
# This is most accurate as it shows the exact IP being used
446-
try:
447-
sock = getattr(connection, "_sock", None)
448-
if sock is not None:
449-
peer_addr = sock.getpeername()
450-
if peer_addr and len(peer_addr) >= 1:
451-
# For TCP sockets, peer_addr is typically (host, port) tuple
452-
# Return just the host part
453-
return peer_addr[0]
454-
except (AttributeError, OSError):
455-
# Socket might not be connected or getpeername() might fail
456-
pass
457-
458-
# Method 2: Fallback to DNS resolution of the host
459-
# This is less accurate but works when socket is not available
460-
try:
461-
host = getattr(connection, "host", None)
462-
port = getattr(connection, "port", 6379)
463-
if host:
464-
# Use getaddrinfo to resolve the hostname to IP
465-
# This mimics what the connection would do during _connect()
466-
addr_info = socket.getaddrinfo(
467-
host, port, socket.AF_UNSPEC, socket.SOCK_STREAM
468-
)
469-
if addr_info:
470-
# Return the IP from the first result
471-
# addr_info[0] is (family, socktype, proto, canonname, sockaddr)
472-
# sockaddr[0] is the IP address
473-
return addr_info[0][4][0]
474-
except (AttributeError, OSError, socket.gaierror):
475-
# DNS resolution might fail
476-
pass
477-
478-
return None
479-
480-
481426
class MaintenanceEventsConfig:
482427
"""
483428
Configuration class for maintenance events handling behaviour. Events are received through
@@ -531,7 +476,6 @@ def __init__(
531476

532477
self.endpoint_type = endpoint_type
533478

534-
535479
def __repr__(self) -> str:
536480
return (
537481
f"{self.__class__.__name__}("

tests/test_maintenance_events_handling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ def test_migrating_after_moving_multiple_proxies(self, pool_class):
18101810
conn_event_handler.handle_event(NodeMigratingEvent(id=3, ttl=1))
18111811
# validate connection is in MIGRATING state
18121812
assert conn.maintenance_state == MaintenanceState.MAINTENANCE
1813-
1813+
18141814
assert conn.socket_timeout == self.config.relax_timeout
18151815

18161816
# Send MIGRATED event to con with ip = key3

0 commit comments

Comments
 (0)