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

Method on_connect_check_health

redis/asyncio/connection.py:465–578  ·  view source on GitHub ↗
(self, check_health: bool = True)

Source from the content-addressed store, hash-verified

463 await self.on_connect_check_health(check_health=True)
464
465 async def on_connect_check_health(self, check_health: bool = True) -> None:
466 self._parser.on_connect(self)
467 parser = self._parser
468
469 auth_args = None
470 # if credential provider or username and/or password are set, authenticate
471 if self.credential_provider or (self.username or self.password):
472 cred_provider = (
473 self.credential_provider
474 or UsernamePasswordCredentialProvider(self.username, self.password)
475 )
476 auth_args = await cred_provider.get_credentials_async()
477
478 # if resp version is specified and we have auth args,
479 # we need to send them via HELLO
480 if auth_args and self.protocol not in [2, "2"]:
481 if isinstance(self._parser, _AsyncRESP2Parser):
482 self.set_parser(_AsyncRESP3Parser)
483 # update cluster exception classes
484 self._parser.EXCEPTION_CLASSES = parser.EXCEPTION_CLASSES
485 self._parser.on_connect(self)
486 if len(auth_args) == 1:
487 auth_args = ["default", auth_args[0]]
488 # avoid checking health here -- PING will fail if we try
489 # to check the health prior to the AUTH
490 await self.send_command(
491 "HELLO", self.protocol, "AUTH", *auth_args, check_health=False
492 )
493 response = await self.read_response()
494 if response.get(b"proto") != int(self.protocol) and response.get(
495 "proto"
496 ) != int(self.protocol):
497 raise ConnectionError("Invalid RESP version")
498 # avoid checking health here -- PING will fail if we try
499 # to check the health prior to the AUTH
500 elif auth_args:
501 await self.send_command("AUTH", *auth_args, check_health=False)
502
503 try:
504 auth_response = await self.read_response()
505 except AuthenticationWrongNumberOfArgsError:
506 # a username and password were specified but the Redis
507 # server seems to be < 6.0.0 which expects a single password
508 # arg. retry auth with just the password.
509 # https://github.com/andymccurdy/redis-py/issues/1274
510 await self.send_command("AUTH", auth_args[-1], check_health=False)
511 auth_response = await self.read_response()
512
513 if str_if_bytes(auth_response) != "OK":
514 raise AuthenticationError("Invalid Username or Password")
515
516 # if resp version is specified, switch to it
517 elif self.protocol not in [2, "2"]:
518 if isinstance(self._parser, _AsyncRESP2Parser):
519 self.set_parser(_AsyncRESP3Parser)
520 # update cluster exception classes
521 self._parser.EXCEPTION_CLASSES = parser.EXCEPTION_CLASSES
522 self._parser.on_connect(self)

Callers 2

connect_check_healthMethod · 0.95
on_connectMethod · 0.95

Calls 11

set_parserMethod · 0.95
send_commandMethod · 0.95
read_responseMethod · 0.95
ConnectionErrorClass · 0.90
str_if_bytesFunction · 0.90
AuthenticationErrorClass · 0.90
sumClass · 0.85
on_connectMethod · 0.45
get_credentials_asyncMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected