Skip to content

Commit 1dd21d6

Browse files
committed
client: fix retry_on_error for pipeline and pubsub
Extend the fix from bea72995fd39b01e2f0a1682b16b6c7690933f36 to apply to pipeline and pubsub as well. Fixes redis/redis-py#2973
1 parent c4faf80 commit 1dd21d6

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

redis/asyncio/client.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -866,11 +866,15 @@ async def connect(self):
866866
async def _disconnect_raise_connect(self, conn, error):
867867
"""
868868
Close the connection and raise an exception
869-
if retry_on_timeout is not set or the error
870-
is not a TimeoutError. Otherwise, try to reconnect
869+
if retry_on_error is not set or the error is not one
870+
of the specified error types. Otherwise, try to
871+
reconnect
871872
"""
872873
await conn.disconnect()
873-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
874+
if (
875+
conn.retry_on_error is None
876+
or isinstance(error, tuple(conn.retry_on_error)) is False
877+
):
874878
raise error
875879
await conn.connect()
876880

@@ -1282,8 +1286,8 @@ async def _disconnect_reset_raise(self, conn, error):
12821286
"""
12831287
Close the connection, reset watching state and
12841288
raise an exception if we were watching,
1285-
retry_on_timeout is not set,
1286-
or the error is not a TimeoutError
1289+
if retry_on_error is not set or the error is not one
1290+
of the specified error types.
12871291
"""
12881292
await conn.disconnect()
12891293
# if we were already watching a variable, the watch is no longer
@@ -1294,9 +1298,12 @@ async def _disconnect_reset_raise(self, conn, error):
12941298
raise WatchError(
12951299
"A ConnectionError occurred on while watching one or more keys"
12961300
)
1297-
# if retry_on_timeout is not set, or the error is not
1298-
# a TimeoutError, raise it
1299-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1301+
# if retry_on_error is not set or the error is not one
1302+
# of the specified error types, raise it
1303+
if (
1304+
conn.retry_on_error is None
1305+
or isinstance(error, tuple(conn.retry_on_error)) is False
1306+
):
13001307
await self.aclose()
13011308
raise
13021309

@@ -1471,8 +1478,8 @@ async def load_scripts(self):
14711478
async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
14721479
"""
14731480
Close the connection, raise an exception if we were watching,
1474-
and raise an exception if retry_on_timeout is not set,
1475-
or the error is not a TimeoutError
1481+
and raise an exception if retry_on_error is not set or the
1482+
error is not one of the specified error types.
14761483
"""
14771484
await conn.disconnect()
14781485
# if we were watching a variable, the watch is no longer valid
@@ -1482,9 +1489,12 @@ async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
14821489
raise WatchError(
14831490
"A ConnectionError occurred on while watching one or more keys"
14841491
)
1485-
# if retry_on_timeout is not set, or the error is not
1486-
# a TimeoutError, raise it
1487-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1492+
# if retry_on_error is not set or the error is not one
1493+
# of the specified error types, raise it
1494+
if (
1495+
conn.retry_on_error is None
1496+
or isinstance(error, tuple(conn.retry_on_error)) is False
1497+
):
14881498
await self.reset()
14891499
raise
14901500

redis/client.py

+34-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
SentinelCommands,
2020
list_or_args,
2121
)
22-
from redis.connection import ConnectionPool, SSLConnection, UnixDomainSocketConnection
22+
from redis.connection import (
23+
ConnectionPool,
24+
SSLConnection,
25+
UnixDomainSocketConnection,
26+
AbstractConnection,
27+
)
2328
from redis.credentials import CredentialProvider
2429
from redis.exceptions import (
2530
ConnectionError,
@@ -781,11 +786,15 @@ def clean_health_check_responses(self) -> None:
781786
def _disconnect_raise_connect(self, conn, error) -> None:
782787
"""
783788
Close the connection and raise an exception
784-
if retry_on_timeout is not set or the error
785-
is not a TimeoutError. Otherwise, try to reconnect
789+
if retry_on_error is not set or the error is not one
790+
of the specified error types. Otherwise, try to
791+
reconnect
786792
"""
787793
conn.disconnect()
788-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
794+
if (
795+
conn.retry_on_error is None
796+
or isinstance(error, tuple(conn.retry_on_error)) is False
797+
):
789798
raise error
790799
conn.connect()
791800

@@ -1261,8 +1270,8 @@ def _disconnect_reset_raise(self, conn, error) -> None:
12611270
"""
12621271
Close the connection, reset watching state and
12631272
raise an exception if we were watching,
1264-
retry_on_timeout is not set,
1265-
or the error is not a TimeoutError
1273+
if retry_on_error is not set or the error is not one
1274+
of the specified error types.
12661275
"""
12671276
conn.disconnect()
12681277
# if we were already watching a variable, the watch is no longer
@@ -1273,9 +1282,12 @@ def _disconnect_reset_raise(self, conn, error) -> None:
12731282
raise WatchError(
12741283
"A ConnectionError occurred on while watching one or more keys"
12751284
)
1276-
# if retry_on_timeout is not set, or the error is not
1277-
# a TimeoutError, raise it
1278-
if not (conn.retry_on_timeout and isinstance(error, TimeoutError)):
1285+
# if retry_on_error is not set or the error is not one
1286+
# of the specified error types, raise it
1287+
if (
1288+
conn.retry_on_error is None
1289+
or isinstance(error, tuple(conn.retry_on_error)) is False
1290+
):
12791291
self.reset()
12801292
raise
12811293

@@ -1433,11 +1445,15 @@ def load_scripts(self):
14331445
if not exist:
14341446
s.sha = immediate("SCRIPT LOAD", s.script)
14351447

1436-
def _disconnect_raise_reset(self, conn: Redis, error: Exception) -> None:
1448+
def _disconnect_raise_reset(
1449+
self,
1450+
conn: AbstractConnection,
1451+
error: Exception,
1452+
) -> None:
14371453
"""
14381454
Close the connection, raise an exception if we were watching,
1439-
and raise an exception if TimeoutError is not part of retry_on_error,
1440-
or the error is not a TimeoutError
1455+
and raise an exception if retry_on_error is not set or the
1456+
error is not one of the specified error types.
14411457
"""
14421458
conn.disconnect()
14431459
# if we were watching a variable, the watch is no longer valid
@@ -1447,11 +1463,13 @@ def _disconnect_raise_reset(self, conn: Redis, error: Exception) -> None:
14471463
raise WatchError(
14481464
"A ConnectionError occurred on while watching one or more keys"
14491465
)
1450-
# if TimeoutError is not part of retry_on_error, or the error
1451-
# is not a TimeoutError, raise it
1452-
if not (
1453-
TimeoutError in conn.retry_on_error and isinstance(error, TimeoutError)
1466+
# if retry_on_error is not set or the error is not one
1467+
# of the specified error types, raise it
1468+
if (
1469+
conn.retry_on_error is None
1470+
or isinstance(error, tuple(conn.retry_on_error)) is False
14541471
):
1472+
14551473
self.reset()
14561474
raise error
14571475

0 commit comments

Comments
 (0)