@@ -159,11 +159,11 @@ def get_protocol(self):
159159 pass
160160
161161 @abstractmethod
162- def connect (self ):
162+ def connect (self , check_health = True ):
163163 pass
164164
165165 @abstractmethod
166- def on_connect (self ):
166+ def on_connect (self , check_health = True ):
167167 pass
168168
169169 @abstractmethod
@@ -370,13 +370,14 @@ def set_parser(self, parser_class):
370370 """
371371 self ._parser = parser_class (socket_read_size = self ._socket_read_size )
372372
373- def connect (self ):
373+ def connect (self , check_health : bool = True ):
374374 "Connects to the Redis server if not already connected"
375375 if self ._sock :
376376 return
377377 try :
378378 sock = self .retry .call_with_retry (
379- lambda : self ._connect (), lambda error : self .disconnect (error )
379+ lambda : self ._connect (),
380+ lambda error : self .disconnect (error ),
380381 )
381382 except socket .timeout :
382383 raise TimeoutError ("Timeout connecting to server" )
@@ -387,7 +388,7 @@ def connect(self):
387388 try :
388389 if self .redis_connect_func is None :
389390 # Use the default on_connect function
390- self .on_connect ()
391+ self .on_connect (check_health = check_health )
391392 else :
392393 # Use the passed function redis_connect_func
393394 self .redis_connect_func (self )
@@ -416,7 +417,7 @@ def _host_error(self):
416417 def _error_message (self , exception ):
417418 return format_error_message (self ._host_error (), exception )
418419
419- def on_connect (self ):
420+ def on_connect (self , check_health : bool = True ):
420421 "Initialize the connection, authenticate and select a database"
421422 self ._parser .on_connect (self )
422423 parser = self ._parser
@@ -475,7 +476,7 @@ def on_connect(self):
475476 # update cluster exception classes
476477 self ._parser .EXCEPTION_CLASSES = parser .EXCEPTION_CLASSES
477478 self ._parser .on_connect (self )
478- self .send_command ("HELLO" , self .protocol )
479+ self .send_command ("HELLO" , self .protocol , check_health = check_health )
479480 self .handshake_metadata = self .read_response ()
480481 if (
481482 self .handshake_metadata .get (b"proto" ) != self .protocol
@@ -485,28 +486,45 @@ def on_connect(self):
485486
486487 # if a client_name is given, set it
487488 if self .client_name :
488- self .send_command ("CLIENT" , "SETNAME" , self .client_name )
489+ self .send_command (
490+ "CLIENT" ,
491+ "SETNAME" ,
492+ self .client_name ,
493+ check_health = check_health ,
494+ )
489495 if str_if_bytes (self .read_response ()) != "OK" :
490496 raise ConnectionError ("Error setting client name" )
491497
492498 try :
493499 # set the library name and version
494500 if self .lib_name :
495- self .send_command ("CLIENT" , "SETINFO" , "LIB-NAME" , self .lib_name )
501+ self .send_command (
502+ "CLIENT" ,
503+ "SETINFO" ,
504+ "LIB-NAME" ,
505+ self .lib_name ,
506+ check_health = check_health ,
507+ )
496508 self .read_response ()
497509 except ResponseError :
498510 pass
499511
500512 try :
501513 if self .lib_version :
502- self .send_command ("CLIENT" , "SETINFO" , "LIB-VER" , self .lib_version )
514+ self .send_command (
515+ "CLIENT" ,
516+ "SETINFO" ,
517+ "LIB-VER" ,
518+ self .lib_version ,
519+ check_health = check_health ,
520+ )
503521 self .read_response ()
504522 except ResponseError :
505523 pass
506524
507525 # if a database is specified, switch to it
508526 if self .db :
509- self .send_command ("SELECT" , self .db )
527+ self .send_command ("SELECT" , self .db , check_health = check_health )
510528 if str_if_bytes (self .read_response ()) != "OK" :
511529 raise ConnectionError ("Invalid Database" )
512530
@@ -548,7 +566,7 @@ def check_health(self):
548566 def send_packed_command (self , command , check_health = True ):
549567 """Send an already packed command to the Redis server"""
550568 if not self ._sock :
551- self .connect ()
569+ self .connect (check_health = False )
552570 # guard against health check recursion
553571 if check_health :
554572 self .check_health ()
@@ -587,7 +605,7 @@ def can_read(self, timeout=0):
587605 """Poll the socket to see if there's data that can be read."""
588606 sock = self ._sock
589607 if not sock :
590- self .connect ()
608+ self .connect (check_health = True )
591609
592610 host_error = self ._host_error ()
593611
@@ -806,8 +824,8 @@ def deregister_connect_callback(self, callback):
806824 def set_parser (self , parser_class ):
807825 self ._conn .set_parser (parser_class )
808826
809- def connect (self ):
810- self ._conn .connect ()
827+ def connect (self , check_health : bool = True ):
828+ self ._conn .connect (check_health = check_health )
811829
812830 server_name = self ._conn .handshake_metadata .get (b"server" , None )
813831 if server_name is None :
@@ -829,8 +847,8 @@ def connect(self):
829847 "To maximize compatibility with all Redis products, client-side caching is supported by Redis 7.4 or later" # noqa: E501
830848 )
831849
832- def on_connect (self ):
833- self ._conn .on_connect ()
850+ def on_connect (self , check_health : bool = True ):
851+ self ._conn .on_connect (check_health = check_health )
834852
835853 def disconnect (self , * args ):
836854 with self ._cache_lock :
@@ -1482,7 +1500,7 @@ def get_connection(self, command_name=None, *keys, **options) -> "Connection":
14821500
14831501 try :
14841502 # ensure this connection is connected to Redis
1485- connection .connect ()
1503+ connection .connect (check_health = True )
14861504 # connections that the pool provides should be ready to send
14871505 # a command. if not, the connection was either returned to the
14881506 # pool before all data has been read or the socket has been
@@ -1492,7 +1510,7 @@ def get_connection(self, command_name=None, *keys, **options) -> "Connection":
14921510 raise ConnectionError ("Connection has data" )
14931511 except (ConnectionError , OSError ):
14941512 connection .disconnect ()
1495- connection .connect ()
1513+ connection .connect (check_health = True )
14961514 if connection .can_read ():
14971515 raise ConnectionError ("Connection not ready" )
14981516 except BaseException :
@@ -1729,7 +1747,7 @@ def get_connection(self, command_name=None, *keys, **options):
17291747
17301748 try :
17311749 # ensure this connection is connected to Redis
1732- connection .connect ()
1750+ connection .connect (check_health = True )
17331751 # connections that the pool provides should be ready to send
17341752 # a command. if not, the connection was either returned to the
17351753 # pool before all data has been read or the socket has been
@@ -1739,7 +1757,7 @@ def get_connection(self, command_name=None, *keys, **options):
17391757 raise ConnectionError ("Connection has data" )
17401758 except (ConnectionError , OSError ):
17411759 connection .disconnect ()
1742- connection .connect ()
1760+ connection .connect (check_health = True )
17431761 if connection .can_read ():
17441762 raise ConnectionError ("Connection not ready" )
17451763 except BaseException :
0 commit comments