(
self, check_health: bool = True, retry_socket_connect: bool = True
)
| 359 | ) |
| 360 | |
| 361 | async def connect_check_health( |
| 362 | self, check_health: bool = True, retry_socket_connect: bool = True |
| 363 | ): |
| 364 | if self.is_connected: |
| 365 | return |
| 366 | # Track actual retry attempts for error reporting |
| 367 | actual_retry_attempts = 0 |
| 368 | |
| 369 | def failure_callback(error, failure_count): |
| 370 | nonlocal actual_retry_attempts |
| 371 | actual_retry_attempts = failure_count |
| 372 | return self.disconnect(error=error, failure_count=failure_count) |
| 373 | |
| 374 | try: |
| 375 | if retry_socket_connect: |
| 376 | await self.retry.call_with_retry( |
| 377 | lambda: self._connect(), |
| 378 | failure_callback, |
| 379 | with_failure_count=True, |
| 380 | ) |
| 381 | else: |
| 382 | await self._connect() |
| 383 | except asyncio.CancelledError: |
| 384 | raise # in 3.7 and earlier, this is an Exception, not BaseException |
| 385 | except (socket.timeout, asyncio.TimeoutError): |
| 386 | e = TimeoutError("Timeout connecting to server") |
| 387 | await record_error_count( |
| 388 | server_address=getattr(self, "host", None), |
| 389 | server_port=getattr(self, "port", None), |
| 390 | network_peer_address=getattr(self, "host", None), |
| 391 | network_peer_port=getattr(self, "port", None), |
| 392 | error_type=e, |
| 393 | retry_attempts=actual_retry_attempts, |
| 394 | is_internal=False, |
| 395 | ) |
| 396 | raise e |
| 397 | except OSError as e: |
| 398 | e = ConnectionError(self._error_message(e)) |
| 399 | await record_error_count( |
| 400 | server_address=getattr(self, "host", None), |
| 401 | server_port=getattr(self, "port", None), |
| 402 | network_peer_address=getattr(self, "host", None), |
| 403 | network_peer_port=getattr(self, "port", None), |
| 404 | error_type=e, |
| 405 | retry_attempts=actual_retry_attempts, |
| 406 | is_internal=False, |
| 407 | ) |
| 408 | raise e |
| 409 | except Exception as exc: |
| 410 | raise ConnectionError(exc) from exc |
| 411 | |
| 412 | try: |
| 413 | if not self.redis_connect_func: |
| 414 | # Use the default on_connect function |
| 415 | await self.on_connect_check_health(check_health=check_health) |
| 416 | else: |
| 417 | # Use the passed function redis_connect_func |
| 418 | ( |
no test coverage detected