50
50
PubSubError ,
51
51
RedisError ,
52
52
ResponseError ,
53
- TimeoutError ,
54
53
WatchError ,
55
54
)
56
55
from redis .lock import Lock
@@ -601,18 +600,16 @@ def _send_command_parse_response(self, conn, command_name, *args, **options):
601
600
conn .send_command (* args , ** options )
602
601
return self .parse_response (conn , command_name , ** options )
603
602
604
- def _conditional_disconnect (self , conn , error ) -> None :
603
+ def _close_connection (self , conn ) -> None :
605
604
"""
606
- Close the connection if the error is not TimeoutError.
605
+ Close the connection before retrying.
606
+
607
607
The supported exceptions are already checked in the
608
608
retry object so we don't need to do it here.
609
+
609
610
After we disconnect the connection, it will try to reconnect and
610
611
do a health check as part of the send_command logic(on connection level).
611
612
"""
612
- if isinstance (error , TimeoutError ):
613
- # If the error is a TimeoutError, we don't want to
614
- # disconnect the connection. We want to retry the command.
615
- return
616
613
617
614
conn .disconnect ()
618
615
@@ -633,7 +630,7 @@ def _execute_command(self, *args, **options):
633
630
lambda : self ._send_command_parse_response (
634
631
conn , command_name , * args , ** options
635
632
),
636
- lambda error : self ._conditional_disconnect (conn , error ),
633
+ lambda _ : self ._close_connection (conn ),
637
634
)
638
635
finally :
639
636
if self ._single_connection_client :
@@ -892,19 +889,14 @@ def clean_health_check_responses(self) -> None:
892
889
)
893
890
ttl -= 1
894
891
895
- def _disconnect_raise_connect (self , conn , error ) -> None :
892
+ def _reconnect (self , conn ) -> None :
896
893
"""
897
- Close the connection and raise an exception
898
- if retry_on_error is not set or the error is not one
899
- of the specified error types. Otherwise, try to
900
- reconnect
894
+ The supported exceptions are already checked in the
895
+ retry object so we don't need to do it here.
896
+
897
+ In this error handler we are trying to reconnect to the server.
901
898
"""
902
899
conn .disconnect ()
903
- if (
904
- conn .retry_on_error is None
905
- or isinstance (error , tuple (conn .retry_on_error )) is False
906
- ):
907
- raise error
908
900
conn .connect ()
909
901
910
902
def _execute (self , conn , command , * args , ** kwargs ):
@@ -917,7 +909,7 @@ def _execute(self, conn, command, *args, **kwargs):
917
909
"""
918
910
return conn .retry .call_with_retry (
919
911
lambda : command (* args , ** kwargs ),
920
- lambda error : self ._disconnect_raise_connect (conn , error ),
912
+ lambda _ : self ._reconnect (conn ),
921
913
)
922
914
923
915
def parse_response (self , block = True , timeout = 0 ):
@@ -1375,36 +1367,37 @@ def execute_command(self, *args, **kwargs):
1375
1367
return self .immediate_execute_command (* args , ** kwargs )
1376
1368
return self .pipeline_execute_command (* args , ** kwargs )
1377
1369
1378
- def _disconnect_reset_raise (self , conn , error ) -> None :
1370
+ def _disconnect_reset_raise_on_watching (
1371
+ self ,
1372
+ conn : AbstractConnection ,
1373
+ error : Exception ,
1374
+ ) -> None :
1379
1375
"""
1380
- Close the connection, reset watching state and
1381
- raise an exception if we were watching,
1382
- if retry_on_error is not set or the error is not one
1383
- of the specified error types.
1376
+ Close the connection reset watching state and
1377
+ raise an exception if we were watching.
1378
+
1379
+ The supported exceptions are already checked in the
1380
+ retry object so we don't need to do it here.
1381
+
1382
+ After we disconnect the connection, it will try to reconnect and
1383
+ do a health check as part of the send_command logic(on connection level).
1384
1384
"""
1385
1385
conn .disconnect ()
1386
+
1386
1387
# if we were already watching a variable, the watch is no longer
1387
1388
# valid since this connection has died. raise a WatchError, which
1388
1389
# indicates the user should retry this transaction.
1389
1390
if self .watching :
1390
1391
self .reset ()
1391
1392
raise WatchError (
1392
- "A ConnectionError occurred on while watching one or more keys"
1393
+ f "A { type ( error ). __name__ } occurred while watching one or more keys"
1393
1394
)
1394
- # if retry_on_error is not set or the error is not one
1395
- # of the specified error types, raise it
1396
- if (
1397
- conn .retry_on_error is None
1398
- or isinstance (error , tuple (conn .retry_on_error )) is False
1399
- ):
1400
- self .reset ()
1401
- raise
1402
1395
1403
1396
def immediate_execute_command (self , * args , ** options ):
1404
1397
"""
1405
- Execute a command immediately, but don't auto-retry on a
1406
- ConnectionError if we're already WATCHing a variable. Used when
1407
- issuing WATCH or subsequent commands retrieving their values but before
1398
+ Execute a command immediately, but don't auto-retry on the supported
1399
+ errors for retry if we're already WATCHing a variable.
1400
+ Used when issuing WATCH or subsequent commands retrieving their values but before
1408
1401
MULTI is called.
1409
1402
"""
1410
1403
command_name = args [0 ]
@@ -1418,7 +1411,7 @@ def immediate_execute_command(self, *args, **options):
1418
1411
lambda : self ._send_command_parse_response (
1419
1412
conn , command_name , * args , ** options
1420
1413
),
1421
- lambda error : self ._disconnect_reset_raise (conn , error ),
1414
+ lambda error : self ._disconnect_reset_raise_on_watching (conn , error ),
1422
1415
)
1423
1416
1424
1417
def pipeline_execute_command (self , * args , ** options ) -> "Pipeline" :
@@ -1556,32 +1549,28 @@ def load_scripts(self):
1556
1549
if not exist :
1557
1550
s .sha = immediate ("SCRIPT LOAD" , s .script )
1558
1551
1559
- def _disconnect_raise_reset (
1552
+ def _disconnect_raise_on_watching (
1560
1553
self ,
1561
1554
conn : AbstractConnection ,
1562
1555
error : Exception ,
1563
1556
) -> None :
1564
1557
"""
1565
- Close the connection, raise an exception if we were watching,
1566
- and raise an exception if retry_on_error is not set or the
1567
- error is not one of the specified error types.
1558
+ Close the connection, raise an exception if we were watching.
1559
+
1560
+ The supported exceptions are already checked in the
1561
+ retry object so we don't need to do it here.
1562
+
1563
+ After we disconnect the connection, it will try to reconnect and
1564
+ do a health check as part of the send_command logic(on connection level).
1568
1565
"""
1569
1566
conn .disconnect ()
1570
1567
# if we were watching a variable, the watch is no longer valid
1571
1568
# since this connection has died. raise a WatchError, which
1572
1569
# indicates the user should retry this transaction.
1573
1570
if self .watching :
1574
1571
raise WatchError (
1575
- "A ConnectionError occurred on while watching one or more keys"
1572
+ f "A { type ( error ). __name__ } occurred while watching one or more keys"
1576
1573
)
1577
- # if retry_on_error is not set or the error is not one
1578
- # of the specified error types, raise it
1579
- if (
1580
- conn .retry_on_error is None
1581
- or isinstance (error , tuple (conn .retry_on_error )) is False
1582
- ):
1583
- self .reset ()
1584
- raise error
1585
1574
1586
1575
def execute (self , raise_on_error : bool = True ) -> List [Any ]:
1587
1576
"""Execute all the commands in the current pipeline"""
@@ -1605,7 +1594,7 @@ def execute(self, raise_on_error: bool = True) -> List[Any]:
1605
1594
try :
1606
1595
return conn .retry .call_with_retry (
1607
1596
lambda : execute (conn , stack , raise_on_error ),
1608
- lambda error : self ._disconnect_raise_reset (conn , error ),
1597
+ lambda error : self ._disconnect_raise_on_watching (conn , error ),
1609
1598
)
1610
1599
finally :
1611
1600
self .reset ()
0 commit comments