MCPcopy
hub / github.com/redis/redis-py / connect_check_health

Method connect_check_health

redis/connection.py:1014–1078  ·  view source on GitHub ↗
(
        self, check_health: bool = True, retry_socket_connect: bool = True
    )

Source from the content-addressed store, hash-verified

1012 )
1013
1014 def connect_check_health(
1015 self, check_health: bool = True, retry_socket_connect: bool = True
1016 ):
1017 if self._sock:
1018 return
1019 # Track actual retry attempts for error reporting
1020 actual_retry_attempts = [0]
1021
1022 def failure_callback(error, failure_count):
1023 actual_retry_attempts[0] = failure_count
1024 self.disconnect(error=error, failure_count=failure_count)
1025
1026 try:
1027 if retry_socket_connect:
1028 sock = self.retry.call_with_retry(
1029 self._connect,
1030 failure_callback,
1031 with_failure_count=True,
1032 )
1033 else:
1034 sock = self._connect()
1035 except socket.timeout:
1036 e = TimeoutError("Timeout connecting to server")
1037 record_error_count(
1038 server_address=self.host,
1039 server_port=self.port,
1040 network_peer_address=self.host,
1041 network_peer_port=self.port,
1042 error_type=e,
1043 retry_attempts=actual_retry_attempts[0],
1044 )
1045 raise e
1046 except OSError as e:
1047 e = ConnectionError(self._error_message(e))
1048 record_error_count(
1049 server_address=getattr(self, "host", None),
1050 server_port=getattr(self, "port", None),
1051 network_peer_address=getattr(self, "host", None),
1052 network_peer_port=getattr(self, "port", None),
1053 error_type=e,
1054 retry_attempts=actual_retry_attempts[0],
1055 )
1056 raise e
1057
1058 self._sock = sock
1059 try:
1060 if self.redis_connect_func is None:
1061 # Use the default on_connect function
1062 self.on_connect_check_health(check_health=check_health)
1063 else:
1064 # Use the passed function redis_connect_func
1065 self.redis_connect_func(self)
1066 except RedisError:
1067 # clean up after any error in on_connect
1068 self.disconnect()
1069 raise
1070
1071 # run any user callbacks. right now the only internal callback

Callers 3

connectMethod · 0.95
send_packed_commandMethod · 0.95
connect_toMethod · 0.45

Calls 8

_connectMethod · 0.95
_error_messageMethod · 0.95
disconnectMethod · 0.95
TimeoutErrorClass · 0.85
ConnectionErrorClass · 0.85
record_error_countFunction · 0.50
call_with_retryMethod · 0.45

Tested by

no test coverage detected